Options
All
  • Public
  • Public/Protected
  • All
Menu

@pinemach/csv

Coverage Status Build Status NPM version MIT License

@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/.

Installation

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

Configuration

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
});

Example Usage

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);

Index

Type aliases

ParserSource

ParserSource: Iterator<string>

Any iterator which enumerates characters one at a time from some serialized CSV data source can be recognized and handled by a Parser object.

ParserSourceParameter

ParserSourceParameter: string | Readable | Iterator<string> | Iterable<string>

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.

Row

Row: Iterable<any>

The library recognizes any iterable object as a row value.

RowsIterable

RowsIterable: Iterable<Row>

RowsIterator

RowsIterator: Iterator<Row>

Helpful reference type to use where some iterable containing rows - each row itself being an iterable containing columns - must be represented.

Functions

isIterable

  • isIterable<T>(value: any): boolean
  • Internal helper to determine if a value is an iterable object. The function checks only for objects, and won't return true for strings.

    internal

    Type parameters

    • T

    Parameters

    • value: any

      The value to check.

    Returns boolean

    True when the input value resembled an ES6 iterable object and false otherwise.

isIterator

  • isIterator<T>(value: any): boolean
  • Internal helper to determine if a value is an iterator object.

    internal

    Type parameters

    • T

    Parameters

    • value: any

      The value to check.

    Returns boolean

    True when the input value resembled an ES6 iterator object and false otherwise.

isNodeStream

  • isNodeStream(value: any): boolean
  • Internal helper to determine if an input value was a NodeJS readable stream.

    internal

    Parameters

    • value: any

      The value to check.

    Returns boolean

    True when the input value resembled a NodeJS readable stream and false otherwise.

parse

  • Parse some CSV data without needing to explicitly construct a Parser.

    Parameters

    • source: ParserSourceParameter

      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: Options

      Optional Options object to determine parsing behavior.

    Returns Parser

    A Parser object, which behaves as an iterable containing rows.

stream

  • Stream some serialized CSV data without needing to explicitly construct a Writer.

    Parameters

    • rows: RowsIterable

      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: Options

      Optional Options object to determine serialization behavior.

    Returns WriterNodeStream

    An object implementing the NodeJS readable stream interface, serializing rows as they are requested instead of all at once.

write

  • Serialize some CSV data without needing to explicitly construct a Writer.

    Parameters

    • rows: RowsIterable

      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: Options

      Optional Options object to determine serialization behavior.

    Returns string

    A string containing the fully serialized CSV data.

Object literals

Const DefaultOptions

DefaultOptions: object

Default Writer and Parser configuration object. When a configuration options object isn't provided, or when some fields are omitted from an options object, these values are used as defaults.

newline

newline: string = ""

Default to CRLF row terminators.

quote

quote: string = """

Default to the double quote character '"' for column quote-escaping.

quoteAll

quoteAll: false = false

Default to not quote-escaping columns that don't contain special characters.

separator

separator: string = ","

Default to comma ',' column separators.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc