A tiny CSV parser and stringifier for TypeScript, with no dependencies. It follows RFC 4180, so quoted
fields, escaped quotes, embedded commas and newlines all work — the cases a naive split(',') gets
wrong.
import { parse, stringify, parseObjects } from 'tinycsv' parse('name,note\n"Smith, John","line1\nline2"') // [ ['name', 'note'], ['Smith, John', 'line1\nline2'] ] parseObjects('id,name\n1,Alice\n2,Bob') // [ { id: '1', name: 'Alice' }, { id: '2', name: 'Bob' } ] stringify([['a', 'b'], ['has,comma', 'ok']]) // 'a,b\n"has,comma",ok'
CSV looks trivial until a field contains a comma, a quote or a newline — then split(',') breaks, and
reaching for a big library feels like overkill. tinycsv is the small, correct middle: a proper RFC
4180 state-machine parser and a matching stringifier that quotes only what needs quoting, round-trip
safe, in a couple hundred lines with no dependencies.
- Parsing that handles quoted fields containing the delimiter, newlines and escaped quotes
(
""), with\n,\r\nand bare\rline endings. - Stringifying that quotes only the fields that need it (or all, on request), and renders
nullandundefinedas empty. - Object mapping:
parseObjectskeys rows by a header row;stringifyObjectswrites a header and rows, inferring columns or using an explicit order. - Options: custom
delimiter(so it reads TSV too),quote,trim,skipEmptyLines,newline,quoteAll. - Round-trip safe:
parse(stringify(rows))returns the original data.
import { parse, parseObjects, stringify, stringifyObjects } from 'tinycsv' // string[][] parse(text, { delimiter: ',', quote: '"', trim: false, skipEmptyLines: true }) // Record<string, string>[] parseObjects(text, options) // string stringify(rows, { delimiter: ',', quote: '"', newline: '\n', quoteAll: false }) stringifyObjects(records, { columns: ['a', 'b'], ...options })
parse('a\tb\n1\t2', { delimiter: '\t' }) // [['a','b'], ['1','2']]
npm install npm test # 19 tests across parse, stringify and round-tripping npm run build # ESM + CJS + .d.ts via tsup
Tests cover quoted fields with commas and newlines, doubled-quote escapes, CRLF/CR endings, empty fields and lines, custom delimiters, trimming, trailing fields, object mapping, selective quoting, and a parse/stringify round trip. CI runs on Linux and Windows across Node 18/20/22.
- Streaming parse for very large files.
- Type coercion hooks (numbers, booleans, dates).
- A header-rename map.
MIT