Skip to content
A modern, fast, RFC 4180 compliant parser for JS
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.config Fix build:typings Script Jan 15, 2020
.github/workflows
.vscode
src Cleanup 2 Jan 21, 2020
.gitignore
.npmignore
CONTRIBUTING.md Add CONTRIBUTING.md Dec 30, 2019
LICENSE Add LICENSE May 7, 2019
README.md Cleanup Jan 21, 2020
index.cjs
index.d.ts
index.js Add Public API Dec 5, 2019
index.min.js Cleanup 2 Jan 21, 2020
package-lock.json 1.3.2 Jan 21, 2020
package.json 1.3.2 Jan 21, 2020

README.md

GitHub Releases NPM Release MIT License Latest Status Release Status

CSV-ES

A fast, simple, easy-to-use CSV parser

  • RFC Compliant
  • ECMAScript Module
  • CommonJS Bundle Included
  • Typescript Compatible
  • Tiny Footprint (2K minified)

This package combines the best of jQuery-CSV and Douglas Crockford's JSON-js.

It provides 2 functions

  • CSV.parse()
  • CSV.stringify()

Note: If you need a super configurable CSV parser, take a look at PapaParse.*

Installation

npm install csv-es
import CSV from 'csv-es';

CSV.parse()

Takes a string of CSV data and converts it to a 2 dimensional array of [entries][values]

Arguments

CSV.parse(csv, {options}, reviver(value, col, row)) : [entries][values]

  • csv - the CSV string to parse
  • options
    • typed - infer types (default false)
  • reviver - a custom function to modify the output (default (value) => value)

Example

import CSV from '/path/to/csv/index.js';
const csv = `
"header1,header2,header3"
"aaa,bbb,ccc"
"zzz,yyy,xxx"
`;
const parsed = CSV.parse(csv)
console.log(parsed);
> [
>   [ "header1", "header2", "header3" ],
>   [ "aaa", "bbb", "ccc" ],
>   [ "zzz", "yyy", "xxx" ]
> ]

CSV.stringify()

Takes a 2 dimensional array of [entries][values] and converts them to CSV

Arguments

CSV.stringify(array, {options}, replacer(value, col, row)) : string

  • array - the input array to stringify
  • options
    • eof - add a trailing newline at the end of file (default true)
  • replacer - a custom function to modify the values (default (value) => value)

Example

import CSV from '/path/to/csv/index.js';
const data = [
  [ "header1", "header2", "header3" ],
  [ "aaa", "bbb", "ccc" ],
  [ "zzz", "yyy", "xxx" ]
];
const stringified = CSV.stringify(data)
console.log(stringified);
> "header1,header2,header3"
> "aaa,bbb,ccc"
> "zzz,yyy,xxx"

CommonJS

A CommonJS bundle is included for backward compatible with node <= 13.2

CSV.parse()

const CSV = require('csv-es/index.cjs');
const csv = // the csv string
const data = CSV.parse(csv);

CSV.stringify()

const CSV = require('csv-es/index.cjs');
const data = // the a 2-dimensional array
const csv = CSV.stringify(data);

Typings

Typings are generated from JSDoc using Typescript. They are 100% compatible with VSCode Intellisense and will work seamlessly with Typescript.

You can’t perform that action at this time.