@guilhermesilveira/csv-rfc4180
v1.0.0
Published
Minimal RFC 4180 CSV parser and serializer. Zero dependencies.
Downloads
66
Maintainers
Readme
micro-csv-rfc4180
Minimal RFC 4180 CSV parser and serializer. Zero dependencies.
Three functions. ~70 lines. Works in any Node.js ≥ 18 environment.
Install
npm install micro-csv-rfc4180Usage
import { csvSplitLine, csvSerializeLine, csvEscapeField } from 'micro-csv-rfc4180';
// Parse a CSV line into fields
csvSplitLine('Alice,"New York, NY",42')
// → ['Alice', 'New York, NY', '42']
// Serialize fields into a CSV line (auto-quotes when needed)
csvSerializeLine(['Alice', 'New York, NY', '42'])
// → 'Alice,"New York, NY",42'
// Escape a single field
csvEscapeField('say "hi"')
// → '"say ""hi"""'API
csvSplitLine(line: string): string[]
Parse a single CSV line into an array of field values, respecting RFC 4180 quoting rules:
- Fields containing commas, double-quotes, or newlines must be wrapped in double-quotes
- Embedded double-quotes are escaped by doubling them (
"") - Does not handle multi-line quoted fields (newlines inside quotes split across multiple
csvSplitLinecalls)
csvSerializeLine(fields: string[]): string
Serialize an array of field values into a properly-escaped CSV line.
Fields that contain ,, ", \r, or \n are automatically wrapped in double-quotes with embedded quotes doubled.
csvEscapeField(value: string): string
Escape a single CSV field value. Returns the value unchanged if no escaping is needed.
RFC 4180 compliance
Handles:
- ✓ Quoted fields with embedded commas
- ✓ Quoted fields with embedded double-quotes (doubled)
- ✓ Quoted fields with embedded newlines (
\n,\r) - ✓ Empty fields
- ✓ Trailing commas (produce an empty last field)
Does not handle:
- Multi-line records across
csvSplitLinecalls (newlines inside quoted fields that span lines) - BOM markers
- Custom delimiters
License
MIT
