/csv-mapper-lite

A node.js library for parsing and mapping csv into pre-defined format

Primary LanguageJavaScriptMIT LicenseMIT

csv-mapper-lite

build Coverage Status node6.10.1 npm

A node.js library for parsing and mapping csv string into pre-defined format.

Installation

With yarn:

$ yarn add csv-mapper-lite

With npm:

$ npm install csv-mapper-lite

Tests

Tests are written using the futuristic JavaScript test runner, ava.

$ yarn test

API Documentation

process(input : String, mapping : Object) : String

const csvStr = 'col1,col2,col3,,\nworld,hello\n'
const Formatter = require('csv-mapper-lite')
const formatter = new Formatter(csvStr, {
  // required
  rules: {
    newCol1: { expr: 'this.B', defaultValue: '0' },
    newCol2: { expr: 'this.A', defaultValue: '0' },
    newCol3: { expr: 'this.B+this.A', defaultValue: '0' }
  },
  // optional. Additional options and their default values
  separator: ',',
  newLine: '\n',
  noHeader: false,
  shiftFirstRow: false,
  trimTrailing: false,
  dropLastRow: false,
  dropRow: '' // accepts an expression string which will be evaluated during formatting, if evaluation result returns true, current row will be dropped.
})

const output = formatter.process()
console.log(output);

Output:

'newCol1,newCol2,newCol3\nhello,world,helloworld'