@pinemach/csv is a minimal, zero-dependency JavaScript package for writing and parsing CSV files pursuant to RFC 4180, written in TypeScript.
The @pinemach/csv package natively supports parsing and writing string data either eagerly or lazily, as well as via NodeJS streams.
You can read the full API documentation at pineapplemachine.github.io/csv-js/.
You can install this package with the package manager of your choice. For example,
npm install @pinemach/csv
You can then import and use the module like so:
const csv = require("@pinemach/csv"); // CommonJS
import * as csv from "@pinemach/csv"; // ES6 modules
When parsing or writing CSV data, the library accepts an options object, either as the second argument to parse, write, or stream, or as the sole argument to the Parser or Writer constructor.
The CSV Parser class recognizes these configuration options:
const myCsvParser = new csv.Parser({
separator: ",", // Column value separator character
quote: "\"", // Column escaping/quoting character
});
The CSV Writer class recognizes these configuration options:
const myCsvWriter = new csv.Writer({
separator: ",", // Column value separator character
quote: "\"", // Column escaping/quoting character
newline: "\r\n", // Row separator string, normally either "\n" or "\r\n"
quoteAll: false, // Escape/quote all columns regardless of necessity
});
const assert = require("assert").strict;
const fs = require("fs");
const csv = require("@pinemach/csv");
// My table containing very important data
const data = [
["Continent", "Country", "Capital"],
["Africa", "Egypt", "Cairo"],
["Africa", "Morocco", "Rabat"],
["Asia", "China", "Beijing"],
["Asia", "Japan", "Tokyo"],
["Australia", "Australia", "Canberra"],
["Europe", "Britian", "London"],
["Europe", "Finland", "Helsinki"],
["North America", "Cuba", "Havana"],
["North America", "United States", "Washington"],
["South America", "Brazil", "Brasilia"],
["South America", "Ecuador", "Quito"],
];
// Write my data as a CSV file
const path = __dirname + "/basic-usage.csv";
fs.writeFileSync(path, csv.write(data));
// Load the data back from my CSV file
const content = fs.readFileSync(path, "utf8");
const parsedRows = csv.parse(content).rows();
// Parsed data is equivalent to the written data
assert.deepEqual(parsedRows, data);
Represents the source parameter types accepted by the Parser constructor and its Parser.parse method. Specifically this means either a string, a NodeJS readable stream, or an iterable or iterable for enumerating the characters of a CSV data source one-by-one.
The library recognizes any iterable object as a row value.
Helpful reference type to use where some iterable containing rows - each row itself being an iterable containing columns - must be represented.
Internal helper to determine if a value is an iterable object. The function checks only for objects, and won't return true for strings.
The value to check.
True when the input value resembled an ES6 iterable object and false otherwise.
Internal helper to determine if a value is an iterator object.
The value to check.
True when the input value resembled an ES6 iterator object and false otherwise.
Internal helper to determine if an input value was a NodeJS readable stream.
The value to check.
True when the input value resembled a NodeJS readable stream and false otherwise.
Parse some CSV data without needing to explicitly construct a Parser.
The CSV data input. This can be a string, a NodeJS readable stream, or any iterable or iterator which enumerates data one character at a time.
Optional Options object to determine parsing behavior.
A Parser object, which behaves as an iterable containing rows.
Stream some serialized CSV data without needing to explicitly construct a Writer.
An iterable enumerating rows, where the rows are themselves
iterables enumerating columns. Columns are serialized to CSV data strings
using the String()
function.
Optional Options object to determine serialization behavior.
An object implementing the NodeJS readable stream interface, serializing rows as they are requested instead of all at once.
Serialize some CSV data without needing to explicitly construct a Writer.
An iterable enumerating rows, where the rows are themselves
iterables enumerating columns. Columns are serialized to CSV data strings
using the String()
function.
Optional Options object to determine serialization behavior.
A string containing the fully serialized CSV data.
Default to CRLF row terminators.
Default to the double quote character '"' for column quote-escaping.
Default to not quote-escaping columns that don't contain special characters.
Default to comma ',' column separators.
Generated using TypeDoc
Any iterator which enumerates characters one at a time from some serialized CSV data source can be recognized and handled by a Parser object.