node-csvify

0.0.6 • Public • Published

node-csvify

Version 0.0.6

Build Status Dependency Status devDependencies

Require CSV/TSV files directly in Browserify

When bundling an app using Browserify, it's often convenient to be able to include your .csv (comma-delimited) or .tsv (tab-delimited) files just as you would .json files (which Node natively understands). This small script allows you to require() them directly.

Installation

npm install node-csvify

Example

Simply require your data files as you might anything else:

var states = require('./states.csv');
var countries = require('./countries.tsv');

Then, when running browserify, pass this module as a transformation on the command line.

browserify -t node-csvify script.js > bundle.js

Alternatively, you can specify this module as a transformation in your package.json file:

"browserify": {
    "transform": ["node-csvify"]
}

By doing so, you can include .csv or .tsv files without including the annoying -t node-csvify every time you run browserify.

How it works

This module uses D3's CSV and TSV parsing functions to convert the files to Javascript objects. (D3 itself is not a dependency, since it requires a C++ compiler downstream. Instead, I just ripped out the dsv parsing functions and repackaged them.) These functions are very smart about converting headers to property names. For example, this:

first,last
Chris,Wilson
Mike,Bostock
Miles,Davis

Becomes this:

[ { first: 'Chris', last: 'Wilson' },
  { first: 'Mike', last: 'Bostock' },
  { first: 'Miles', last: 'Davis' } ]

Reducing file size

By default, this transformation converts the delimited files to JSON before passing them back to browserify. For large CSV files, this can greatly reduce the size, since the headers are now repeated for each record. (In the small example above, the CSV file is about 50 bytes, which the JSON verision is over 100 bytes.) While this sort of redundancy can be compressed very efficiently by gzip compression, you may still encounter instances where you want your data passed to its final destination as a compact string. You then assume responsibility for unpacking that string into a useful data format for your application.

There is no good way to pass arguments to transformations from the code itself. Instead, if you want this "no JSON" mode, you have to specify this in the package.json file:

"browserify": {
    "transform": ["node-csvify"],
    "transform-options": {
        "node-csvify": "nojson"
    }
}

In "nojson" mode, the above example would pass a string to the application with the value:

first,last|Chris,Wilson|Mike,Bostock|Miles,Davis

Version history

  • v0.0.6: Updated dependencies
  • v0.0.3: Extracted D3's dsv parsing and removed library as a dependency

Package Sidebar

Install

npm i node-csvify

Weekly Downloads

0

Version

0.0.6

License

MIT

Last publish

Collaborators

  • wilson428