All Projects → jonschlinkert → parse-csv

jonschlinkert / parse-csv

Licence: MIT license
CSV parser for node.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to parse-csv

pixie
Tiny template functions.
Stars: ✭ 14 (-6.67%)
Mutual labels:  parse
python-fastimport
Git Fastimport parser and generator in Python
Stars: ✭ 19 (+26.67%)
Mutual labels:  parse
logstash-laravel-logs
Process Laravel Log files on Logstash and forward to ElasticSearch
Stars: ✭ 35 (+133.33%)
Mutual labels:  parse
extract-domain
Extract domain name from an URL
Stars: ✭ 22 (+46.67%)
Mutual labels:  parse
tparse
time parsing library for Go; supports more time units than standard library
Stars: ✭ 42 (+180%)
Mutual labels:  parse
golgi
A composable routing library for Haxe.
Stars: ✭ 37 (+146.67%)
Mutual labels:  parse
parse-git-config
Parse `.git/config` into a JavaScript object. sync or async.
Stars: ✭ 55 (+266.67%)
Mutual labels:  parse
color-math
Expressions to manipulate colors.
Stars: ✭ 18 (+20%)
Mutual labels:  parse
eslump
Fuzz testing JavaScript parsers and suchlike programs.
Stars: ✭ 56 (+273.33%)
Mutual labels:  parse
BaaSDelphiSamples
💾 Code samples for BaaS and PaaS using Delphi
Stars: ✭ 30 (+100%)
Mutual labels:  parse
parse-react
[EXPERIMENTAL] React, React Native, and React with SSR (e.g. Next.js) packages to interact with Parse Server backend
Stars: ✭ 58 (+286.67%)
Mutual labels:  parse
node-red-contrib-string
Provides a string manipulation node with a chainable UI based on the concise and lightweight stringjs.com.
Stars: ✭ 15 (+0%)
Mutual labels:  parse
MarknoteParser
A high performance markdown parser in Swift.
Stars: ✭ 29 (+93.33%)
Mutual labels:  parse
postcss-styl
PostCSS parser plugin for converting Stylus syntax to PostCSS AST.
Stars: ✭ 15 (+0%)
Mutual labels:  parse
ujson drf
JSON Renderer and Parser for Django Rest Framework using the ultra fast json (in C).
Stars: ✭ 14 (-6.67%)
Mutual labels:  parse
SwiftDomainParser
A Full Swift Lightweight Framework that uses the Public Suffix list to Parse URLs
Stars: ✭ 48 (+220%)
Mutual labels:  parse
twkb
A small GO parser for the TWKB format
Stars: ✭ 17 (+13.33%)
Mutual labels:  parse
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (+53.33%)
Mutual labels:  parse
rasn1
Ruby ASN.1 library
Stars: ✭ 14 (-6.67%)
Mutual labels:  parse
rpgdice
A generic RPG dice roller syntax and library.
Stars: ✭ 24 (+60%)
Mutual labels:  parse

parse-csv NPM version NPM downloads Build Status

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 = require('parse-csv');

var str = [
  'id,fruit,vegetable',
  '1,apple,carrot',
  '2,orange,corn',
  '3,banana,potato'
].join('\n');

var obj = csv.toJSON(str, {headers: {included: true}});
console.log(obj);

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,vegetable
1,apple,carrot
2,orange,corn
3,banana,potato`;

var res = csv(str, {headers: {included: true}});
console.log(res);
// 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,vegetable
1,apple,carrot
2,orange,corn
3,banana,potato`;

var res = csv.toJSON('jsonDict', str, {headers: {included: true}});
console.log(res);
// 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 = require('parse-csv');
var parser = new csv.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 = new Parser();

var str = `
id,fruit,vegetable
1,apple,carrot
2,orange,corn
3,banana,potato`;

var datagrid = parser.parse(str);

// 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 = require('parse-csv');
var renderer = new csv.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 = require('parse-csv');
var renderer = new csv.Renderer();

var str = `
id,fruit,vegetable
1,apple,carrot
2,orange,corn
3,banana,potato`;

var html = renderer.html(str, {headers: {included: true}});
console.log(html);

Results in:

<table>
  <thead>
    <tr>
      <th class="id-cell">id</th>
      <th class="fruit-cell">fruit</th>
      <th class="vegetable-cell">vegetable</th>
    </tr>
  </thead>
  <tbody>
    <tr class="firstRow">
      <td class="id-cell">1</td>
      <td class="fruit-cell">apple</td>
      <td class="vegetable-cell">carrot</td>
    </tr>
    <tr>
      <td class="id-cell">2</td>
      <td class="fruit-cell">orange</td>
      <td class="vegetable-cell">corn</td>
    </tr>
    <tr class="lastRow">
      <td class="id-cell">3</td>
      <td class="fruit-cell">banana</td>
      <td class="vegetable-cell">potato</td>
    </tr>
  </tbody>
</table>

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:

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].