Convert Figma logo to code with AI

CyrusOfEden logoCSV.js

A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.

1,543
95
1,543
13

Top Related Projects

12,413

Fast and powerful CSV (delimited text) parser that gracefully handles large files and malformed input

1,542

A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.

34,966

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Quick Overview

CSV.js is a lightweight JavaScript library for parsing and stringifying CSV (Comma-Separated Values) data. It provides a simple and efficient way to work with CSV files in both browser and Node.js environments, offering support for various CSV formats and customization options.

Pros

  • Easy to use with a straightforward API
  • Supports both parsing CSV strings to arrays and stringifying arrays to CSV
  • Handles various CSV formats, including custom delimiters and line endings
  • Works in both browser and Node.js environments

Cons

  • Limited advanced features compared to some larger CSV libraries
  • Documentation could be more comprehensive
  • Lacks built-in streaming support for large files
  • No TypeScript definitions provided out of the box

Code Examples

Parsing a CSV string:

const CSV = require('csv-js');

const csvString = 'name,age\nJohn,30\nJane,25';
const parsed = CSV.parse(csvString);
console.log(parsed);
// Output: [['name', 'age'], ['John', '30'], ['Jane', '25']]

Stringifying an array to CSV:

const CSV = require('csv-js');

const data = [
  ['name', 'age'],
  ['John', 30],
  ['Jane', 25]
];
const csvString = CSV.stringify(data);
console.log(csvString);
// Output: "name,age\nJohn,30\nJane,25"

Using custom options:

const CSV = require('csv-js');

const csvString = 'name;age\nJohn;30\nJane;25';
const parsed = CSV.parse(csvString, { delimiter: ';' });
console.log(parsed);
// Output: [['name', 'age'], ['John', '30'], ['Jane', '25']]

Getting Started

  1. Install the library using npm:
npm install csv-js
  1. Import and use the library in your JavaScript code:
const CSV = require('csv-js');

// Parse CSV string
const csvString = 'name,age\nJohn,30\nJane,25';
const parsed = CSV.parse(csvString);
console.log(parsed);

// Stringify array to CSV
const data = [['name', 'age'], ['John', 30], ['Jane', 25]];
const stringified = CSV.stringify(data);
console.log(stringified);

That's it! You can now start working with CSV data using CSV.js.

Competitor Comparisons

12,413

Fast and powerful CSV (delimited text) parser that gracefully handles large files and malformed input

Pros of PapaParse

  • More comprehensive feature set, including streaming and web worker support
  • Better performance for large CSV files
  • More active development and community support

Cons of PapaParse

  • Larger file size, which may impact load times for smaller projects
  • More complex API, potentially steeper learning curve for beginners

Code Comparison

PapaParse:

Papa.parse(file, {
  complete: function(results) {
    console.log(results);
  }
});

CSV.js:

CSV.parse(file, function(err, data) {
  console.log(data);
});

Both libraries offer simple parsing functionality, but PapaParse provides more configuration options and a callback-based approach, while CSV.js uses a Node.js-style error-first callback.

PapaParse generally offers more features and better performance, making it suitable for larger projects and complex CSV handling. CSV.js, on the other hand, is lighter and may be preferable for simpler use cases or when minimizing bundle size is a priority. The choice between the two depends on the specific requirements of your project, such as file size constraints, performance needs, and the complexity of CSV operations required.

1,542

A simple, blazing-fast CSV parser and encoder. Full RFC 4180 compliance.

Pros of CSV.js

  • Lightweight and easy to use
  • Supports both parsing and generating CSV data
  • Works in both browser and Node.js environments

Cons of CSV.js

  • Limited customization options for complex CSV structures
  • Lacks advanced features like streaming for large files
  • May not handle all edge cases in CSV formatting

Code Comparison

CSV.js:

const csv = require('csv.js');

csv.parse('a,b,c\n1,2,3', (err, data) => {
  console.log(data);
});

CSV.js:

const CSV = require('csv.js');

CSV.parse('a,b,c\n1,2,3', (err, data) => {
  console.log(data);
});

Both libraries use similar syntax for parsing CSV data, with minor differences in the import statement and function naming. The core functionality appears to be identical in this basic example.

Summary

CSV.js is a simple and versatile CSV parsing and generation library that works across different JavaScript environments. While it may lack some advanced features, it provides a straightforward solution for basic CSV handling tasks. The choice between CSV.js and CSV.js would depend on specific project requirements and the need for additional functionality beyond basic parsing and generation.

34,966

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Pros of SheetJS

  • Supports a wide range of spreadsheet formats (XLS, XLSX, CSV, etc.)
  • Handles complex spreadsheet features like formulas and styling
  • Actively maintained with frequent updates

Cons of SheetJS

  • Larger file size and more complex API
  • Steeper learning curve for basic CSV operations
  • Commercial use requires a paid license

Code Comparison

CSV.js (parsing CSV):

CSV.parse("a,b,c\n1,2,3", function(err, data) {
  console.log(data);
});

SheetJS (parsing CSV):

var workbook = XLSX.read("a,b,c\n1,2,3", {type: 'string'});
var sheet = workbook.Sheets[workbook.SheetNames[0]];
var data = XLSX.utils.sheet_to_json(sheet);
console.log(data);

Summary

SheetJS is a comprehensive library for working with various spreadsheet formats, offering advanced features and broad compatibility. It's well-suited for complex spreadsheet operations but may be overkill for simple CSV tasks. CSV.js, on the other hand, is a lightweight library focused specifically on CSV parsing and generation, making it easier to use for basic CSV operations but lacking the extensive features of SheetJS.

Choose SheetJS for projects requiring support for multiple spreadsheet formats or advanced spreadsheet functionality. Opt for CSV.js when working exclusively with CSV files and prioritizing simplicity and ease of use.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

CSV.js

Simple, blazing-fast CSV parsing/encoding in JavaScript. Full RFC 4180 compliance.

Compatible with browsers (>IE8), AMD, and NodeJS.

Installation

MASTER is currently under development. As such, csv.src.js and csv.js are both unusable. Make sure you download csv.min.js.

Download csv.min.js and reference to it using your preferred method.

If you use Bower, or npm, install the comma-separated-values package.

Instantiation

Create a CSV instance with var csv = new CSV(data);, where data is a plain-text CSV string. You can supply options with the format var csv = new CSV(data, { option: value });.

Options

  • cast: true to automatically cast numbers and booleans to their JavaScript equivalents. false otherwise. Supply your own array to override autocasting. Defaults to true.
  • lineDelimiter: The string that separates lines from one another. If parsing, defaults to autodetection. If encoding, defaults to '\r\n'.
  • cellDelimiter: A 1-character-long string that separates values from one another. If parsing, defaults to autodetection. If encoding, defaults to ','.
  • header: true if the first row of the CSV contains header values, or supply your own array. Defaults to false.

You can update an option's value any time after instantiation with csv.set(option, value).

Quickstart

For those accustomed to JavaScript, the CSV.js API:

// The instance will set itself up for parsing or encoding on instantiation,
// which means that each instance can only either parse or encode.
// The `options` object is optional
var csv = new CSV(data, [options]);

// If the data you've supplied is an array,
// CSV#encode will return the encoded CSV.
// It will otherwise fail silently.
var encoded = csv.encode();

// If the data you've supplied is a string,
// CSV#parse will return the parsed CSV.
// It will otherwise fail silently.
var parsed = csv.parse();

// The CSV instance can return the record immediately after
// it's been encoded or parsed to prevent storing the results
// in a large array by calling CSV#forEach and passing in a function.
csv.forEach(function(record) {
  // do something with the record
});

// CSV includes some convenience class methods:
CSV.parse(data, options); // identical to `new CSV(data, options).parse()`
CSV.encode(data, options); // identical to `new CSV(data, options).encode()`
CSV.forEach(data, options, callback); // identical to `new CSV(data, options).forEach(callback)`

// For overriding automatic casting, set `options.cast` to an array.
// For `parsing`, valid array values are: 'Number', 'Boolean', and 'String'.
CSV.parse(data, { cast: ['String', 'Number', 'Number', 'Boolean'] });
// For `encoding`, valid array values are 'Array', 'Object', 'String', 'Null', and 'Primitive'.
CSV.encode(data, { cast: ['Primitive', 'Primitive', 'String'] });

Parsing

By default CSV.js will return an array of arrays.

var data = '\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data).parse()
/*
Returns:
[
  [1850, 20, 0, 1, 1017281],
  [1850, 20, 0, 2, 1003841]
  ...
]
*/

If the CSV's first row is a header, set header to true, and CSV.js will return an array of objects.

var data = '\
year,age,status,sex,population\r\n\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data, { header: true }).parse();
/*
Returns:
[
  { year: 1850, age: 20, status: 0, sex: 1, population: 1017281 },
  { year: 1850, age: 20, status: 0, sex: 2, population: 1003841 }
  ...
]
*/

You may also supply your own header values, if the text does not contain them, by setting header to an array of field values.

var data = '\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data, {
  header: ['year', 'age', 'status', 'sex', 'population']
}).parse();
/*
Returns:
[
  { year: 1850, age: 20, status: 0, sex: 1, population: 1017281 },
  { year: 1850, age: 20, status: 0, sex: 2, population: 1003841 }
  ...
]
*/

Encoding

CSV.js accepts an array of arrays or an array of objects.

var data = [[1850, 20, 0, 1, 1017281], [1850, 20, 0, 2, 1003841]...];
new CSV(data).encode();
/*
Returns:
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
*/

To add headers to an array of arrays, set header to an array of header field values.

var data = [[1850, 20, 0, 1, 1017281], [1850, 20, 0, 2, 1003841]];
new CSV(data, { header: ["year", "age", "status", "sex", "population"] }).encode();
/*
Returns:
"year","age","status","sex","population"\r\n\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
*/

To add headers to an array of objects, just set header to true.

var data = [
  { year: 1850, age: 20, status: 0, sex: 1, population: 1017281 },
  { year: 1850, age: 20, status: 0, sex: 2, population: 1003841 }
];
new CSV(data, { header: true }).encode();
/*
Returns:
"year","age","status","sex","population"\r\n\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
*/

Streaming

If the dataset that you've provided is to be parsed, calling CSV.prototype.forEach and supplying a function will call your function and supply it with the parsed record immediately after it's been parsed.

var data = '\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data).forEach(function(array) {
  /*
   * do something with the incoming array
   * array example:
   *   [1850, 20, 0, 1, 1017281]
   */
});

Likewise, if you've requested an array of objects, you can still call CSV.prototype.forEach:

var data = '\
year,age,status,sex,population\r\n\
1850,20,0,1,1017281\r\n\
1850,20,0,2,1003841\r\n\
...
';
new CSV(data, { header: true }).forEach(function(object) {
  /*
   * do something with the incoming object
   * object example:
   *   { year: 1850, age: 20, status: 0, sex: 1, population: 1017281 }
   */
});

If you're dataset is to be encoded, CSV.prototype.forEach will call your function and supply the CSV-encoded line immediately after the line has been encoded:

var data = [[1850, 20, 0, 1, 1017281], [1850, 20, 0, 2, 1003841]];
new CSV(data).forEach(function(line) {
  /*
   * do something with the incoming line
   * line example:
   *   "1850,20,0,1,1017281\r\n\""
   */
});

Casting

// For overriding automatic casting, set `options.cast` to an array.
// For `parsing`, valid array values are: 'Number', 'Boolean', and 'String'.
CSV.parse(data, { cast: ['String', 'Number', 'Number', 'Boolean'] });
// For `encoding`, valid array values are 'Array', 'Object', 'String', 'Null', and 'Primitive'.
CSV.encode(data, { cast: ['Primitive', 'Primitive', 'String'] });

Convenience Methods

CSV.parse(data, options) // identical to `new CSV(data, options).parse()`
CSV.encode(data, options) // identical to `new CSV(data, options).encode()`
CSV.forEach(data, options, callback) // identical to `new CSV(data, options).forEach(callback)`

Special Thanks

NPM DownloadsLast 30 Days