JSPM

  • Created
  • Published
  • Downloads 120321
  • Score
    100M100P100Q154468F
  • License MIT

It's a collection of javascript tools (parse/stringify) for CSV strings. Unlike many other similar modules, it works correctly with fields containing newlines (including on the first line)

Package Exports

  • csv-string

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (csv-string) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Javascript CSV Strings

Build Status

It's a collection of javascript tools (parse/stringify) for CSV strings. It can work row by row. Unlike many other similar modules, it works correctly with fields containing newlines (including on the first line)

Contributors

Installation

With npm do:

$ npm install csv-string

Tests

Use mocha to run the tests.

$ npm install mocha
$ mocha test

API Documentation

parse(input : String, [separtor : String]) : Object

Parse input to convert to an array.

    var CSV = require('csv-string'),
     arr = CSV.parse('a,b,c\na,b,c');

    console.log(arr);

Output:

[ [ 'a', 'b', 'c' ], [ 'a', 'b', 'c' ] ]

stringify(input : Object, [separtor : String]) : String

Converts input to a CSV string.

    var CSV = require('csv-string');
    
    console.log(CSV.stringify(['a', 'b', 'c']));
    console.log(CSV.stringify([['c', 'd', 'e'], ['c','d','e']]));
    console.log(CSV.stringify({a:'e', b:'f', c:'g'}));

Output:

a,b,c

c,d,e
c,d,e

e,f,g

detect(input : String) : String

Detects the best separator.

    var CSV = require('csv-string');
    
    console.log(CSV.detect('a,b,c'));
    console.log(CSV.detect('a;b;c'));
    console.log(CSV.detect('a|b|c'));
    console.log(CSV.detect('a\tb\tc'));

Output:

,
;
|
\t

forEach(input : String, sep : String, callback : Function)

forEach(input : String, callback : Function)

callback(row : Array, index : Number) : undefined

Calls callback for each CSV row/line. The Array passed to callback contains the fields of the current row.

    var CSV = require('csv-string');
    var data = 'a,b,c\nd,e,f';
    CSV.forEach(data, ',', function(row, index) {
        console.log('#' + index + ' : ', row);
    });

Output:

#0 :  [ 'a', 'b', 'c' ]
#1 :  [ 'd', 'e', 'f' ]

read(input : String, sep : String, callback : Function) : Number

read(input : String, callback : Function) : Number

callback(row : Array) : undefined

Calls callback when a CSV row is readed. The Array passed to callback contains the fields of the row.
Returns the first offset after the row.

    var CSV = require('csv-string');
    var data = 'a,b,c\nd,e,f';
    var index = CSV.read(data, ',', function(row) {
        console.log(row);
    });
    console.log(data.slice(index));

Output:

[ 'a', 'b', 'c' ]
d,e,f

Also

License

MIT/X11