CSV and object generator

Build Status

This package provides a flexible generator of CSV strings and Javascript objects implementing the Node.js stream.Readable API. It may be used to generate random or user driven data.

Source code for this project is available on GitHub.

Features includes:

  • random or pseudo-random seed based generation
  • stream.Readable implementation

Usage

Run npm install csv to install the full csv module or run npm install csv-generate if you are only interested by the CSV generator.

Use the callback style API for simplicity or the stream based API for scalability.

The source code uses modern JavaScript features and run natively in Node 7.6+. For older browsers or older versions of Node, use the module inside "./lib/es5".

Node.js Stream API

It implements the native Node.js readable stream API. This is the recommended approach if you need a maximum of power. It ensures scalability by treating your data as an input stream.

const generate = require('csv-generate')
const readable_stream = generate([options])

For additional usage and example, you may refer to the example page, the "samples" folder and the "test" folder.

Callback API

The generated output is passed to the callback in the second argument. This mode implies that the overall dataset will be stored in memory.

const generate = require('csv-generate')
generate([options], callback)

Sync API

The generated output is returned. Like with the callback API, this mode implies that the overall dataset will be stored in memory.

const generate = require('csv-generate/lib/sync')
const data = generate([options])

Options

Options may include:

  • duration
    Period to run in milliseconds, default to 4 minutes.
  • columns
    Define the number of generated fields and the generation method. If columns is an integer, it corresponds to the number of fields. If it is an array, each element correspond to a field. If the field is a function, the function is expected to return a value, if a string, it call the registered function of the same name (eg Generator.int for the value "int"), current values are "ascii", "int" and "bool", more could be added by the user or on demand by opending a pull request. Default to 8 ascii columns.
  • max_word_length
    Maximum number of characters per word. Default to 16.
  • seed
    Generate idempotent random characters if a number provided
  • length
    Number of lines or records to generate.
  • objectMode
    Whether this stream should behave as a stream of objects. Meaning that stream.read(n) returns a single value instead of a Buffer of size n. Default=false
  • highWaterMark
    The maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. Default=16kb

All options are optional.

Migration

Most of the generator is imported from its parent project CSV in a effort to split it between the generator, the parser, the transformer and the stringifier.


Fork me on GitHub