CSV parser for node.js.
TOC
Install
Install with npm:
$ npm install parse-csv --save
Based on mr-data-converter by @shancarter. Copyright (c) 2011 Shan Carter.
Usage
var csv = ; var str = 'id,fruit,vegetable' '1,apple,carrot' '2,orange,corn' '3,banana,potato'; var obj = csv;console;
API
csv
Parse a string of CSV to a datagrid and render it using the specified renderer. The .json
renderer is used by default.
Params
method
{String}: The name of the renderer method to use, or a string of CSV. If CSV, the.json
method will be used.str
{String|Object}: String of CSV or options.options
{Object}returns
{String}
Example
var str = `id,fruit,vegetable1,apple,carrot2,orange,corn3,banana,potato`; var res = ;console;// results in:// [{"id":"1","fruit":"apple","vegetable":"carrot"},// {"id":"2","fruit":"orange","vegetable":"corn"},// {"id":"3","fruit":"banana","vegetable":"potato"}]
.toJSON
Parse a string of CSV to a datagrid, format it using one of the .json*
renderer methods, then parse it back to JSON.
Params
method
{String}: The name of the renderer method to use, or a string of CSV. If CSV, the.json
method will be used.str
{String|Object}: String of CSV or options.options
{Object}returns
{String}
Example
var str = `id,fruit,vegetable1,apple,carrot2,orange,corn3,banana,potato`; var res = csv;console;// results in:// { '1': { fruit: 'apple', vegetable: 'carrot' },// '2': { fruit: 'orange', vegetable: 'corn' },// '3': { fruit: 'banana', vegetable: 'potato' } }
Parser
Create a new Parser
with the given options
.
Params
options
{Options}
Example
var csv = ;var parser = ;
.parse
Parse CSV or tab-delimited string into a data-grid formatted JavaScript object.
Params
str
{String}options
{Object}returns
{Object}
Example
var parser = ; var str = `id,fruit,vegetable1,apple,carrot2,orange,corn3,banana,potato`; var datagrid = parser; // results in:// { data:// [ [ '1', 'apple', 'carrot' ],// [ '2', 'orange', 'corn' ],// [ '3', 'banana', 'potato' ] ],// header:// { names: [ 'id', 'fruit', 'vegetable' ],// types: [ '-1': 'string' ] } }
Renderer
Create a new Renderer
with the given options
.
Params
options
{Object}
Example
var csv = ;var renderer = ;
Available renderers
The following render methods are available when the renderer is used directly. Or specify the renderer on options.renderer
when using the main export function.
.as
: Actionscript.asp
: ASP/VBScript.html
: HTML.json
: JSON - Properties.jsonArrayCols
: JSON - Column Arrays.jsonArrayRows
: JSON - Row Arrays.jsonDict
: JSON - Dictionary.mysql
: MySQL.php
: PHP.python
: Python - Dict.ruby
: Ruby.xmlProperties
: XML - Properties.xml
: XML - Nodes.xmlIllustrator
: XML - Illustrator
Example
To render CSV as HTML:
var csv = ;var renderer = ; var str = `id,fruit,vegetable1,apple,carrot2,orange,corn3,banana,potato`; var html = renderer;console;
Results in:
id fruit vegetable 1 apple carrot 2 orange corn 3 banana potato
Options
Parser options
Available parser options and the actual defaults used.
headers: included: false downcase: true upcase: true delimiter: 'tab' decimalSign: 'comma'
Renderer options
Available renderer options and the actual defaults used.
headers: included: true downcase: true upcase: true delimiter: 'tab' decimalSign: 'comma' outputDataType: 'json' columnDelimiter: "\t" rowDelimiter: '\n' inputHeader: {} outputHeader: {} dataSelect: {} outputText: '' newline: '\n' indent: ' ' commentLine: '//' commentLineEnd: '' tableName: 'converter' useUnderscores: true includeWhiteSpace: true useTabsForIndent: false
Related projects
You might also be interested in these projects:
- gulp-convert: Gulp plugin to convert to or from JSON, YAML, XML, PLIST or CSV. | homepage
- parser-csv: CSV parser, compatible with [parser-cache]. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Building docs
Generate readme and API documentation with verb:
$ npm install verb && npm run docs
Or, if verb is installed globally:
$ verb
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016, Jon Schlinkert. Released under the MIT license.
This file was generated by verb, v0.9.0, on May 09, 2016.