Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

csvtojson

Package Overview
Dependencies
Maintainers
1
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csvtojson - npm Package Compare versions

Comparing version 1.1.12 to 2.0.0

.coveralls.yml

44

bin/csvtojson.js
function csvtojson() {
var web = require("../libs/interfaces").web;
var Converter = require("../libs/core/Converter.js");
var Converter = require("../v2").Converter;
var fs = require("fs");

@@ -10,2 +9,3 @@ var options = require("./options.json");

var pkg = require("../package.json");
var os = require("os");
/**

@@ -45,9 +45,10 @@ *{

}
function stringToRegExp(str) {
var lastSlash = str.lastIndexOf("/");
var source = str.substring(1, lastSlash);
var flag = str.substring(lastSlash + 1);
return new RegExp(source,flag);
}
function parse() {
var is = parsedCmd.inputStream;
parsedCmd.options.constructResult = false;
if (parsedCmd.options.toArrayString === undefined) {
parsedCmd.options.toArrayString = true;
}
if (parsedCmd.options.maxRowLength === undefined) {

@@ -60,6 +61,17 @@ parsedCmd.options.maxRowLength = 10240;

}
if (parsedCmd.options.delimiter === "\\t" ){
parsedCmd.options.delimiter="\t";
if (parsedCmd.options.delimiter === "\\t") {
parsedCmd.options.delimiter = "\t";
}
if (parsedCmd.options.ignoreColumns) {
parsedCmd.options.ignoreColumns=stringToRegExp(parsedCmd.options.ignoreColumns);
}
if (parsedCmd.options.includeColumns) {
parsedCmd.options.includeColumns=stringToRegExp(parsedCmd.options.includeColumns);
}
console.log(process.pid);
parsedCmd.options.fork=true;
var conv = new Converter(parsedCmd.options);
var isFirst = true;
conv.on("error", function (err, pos) {

@@ -74,4 +86,12 @@ if (!parsedCmd.options.quiet) {

process.exit(1);
});
is.pipe(conv).pipe(process.stdout);
})
.on("data",function (dataStr) {
process.stdout.write((isFirst ? "" : "," + os.EOL) + dataStr);
isFirst = false;
})
.on("done", function () {
console.log(os.EOL + "]");
})
console.log("[");
is.pipe(conv);
// is.pipe(conv);

@@ -83,4 +103,2 @@ }

parse();
} else if (cmd === "startserver") {
web.startWebServer(options);
} else if (cmd === "version") {

@@ -87,0 +105,0 @@ console.log(pkg.version);

@@ -7,2 +7,6 @@ {

"options": {
"--output":{
"desc": "The format to be converted to. \"json\" (default) -- convert csv to json. \"csv\" -- convert csv to csv row array. \"line\" -- convert csv to csv line string",
"type": "string"
},
"--delimiter": {

@@ -29,6 +33,2 @@ "desc": "delimiter to separate columns. Possible to give an array or just use 'auto'. default comma (,). e.g. --delimiter=# --delimiter='[\",\",\";\"]' --delimiter=auto",

},
"--workerNum": {
"desc": "Number of worker processes. The worker process will use multi-cores to help process CSV data. Set to number of Core to improve the performance of processing large csv file. Keep 1 for small csv files. Default 1.",
"type": "number"
},
"--noheader": {

@@ -58,6 +58,2 @@ "desc": "Indicating csv data has no header row and first row is data row. Default is false",

},
"--toArrayString": {
"desc": "Output result as JSON array. Default is true, set to false to output JSON objects (not array)",
"type": "boolean"
},
"--quiet": {

@@ -72,8 +68,8 @@ "desc": "If any error happens, quit the process quietly rather than log out the error. Default is false.",

"--ignoreColumns": {
"desc": "Columns to ignore on input. e.g. --ignoreColumns='[0,4,5, \"name\"]' ",
"type": "~object"
"desc": "RegExp matched columns to ignore from input. e.g. --ignoreColumns=/(name|age)/ ",
"type": "string"
},
"--includeColumns": {
"desc": "Columns to include on input. e.g. --includeColumns='[0,4,5, \"title\"]' ",
"type": "~object"
"desc": "RegExp matched columns to include from input. e.g. --includeColumns=/(name|age)/ ",
"type": "string"
},

@@ -83,2 +79,6 @@ "--colParser": {

"type": "~object"
},
"--alwaysSplitAtEOL":{
"desc": "Always interpret each line (as defined by eol) as a row. This will prevent eol characters from being used within a row (even inside a quoted field). This ensures that misplaced quotes only break on row, and not all ensuing rows.",
"type": "boolean"
}

@@ -85,0 +85,0 @@ },

@@ -1,1 +0,1 @@

module.exports = require("./libs/csv2json.js");
module.exports=require("./v2");

@@ -247,3 +247,3 @@ {

],
"version": "1.1.12",
"version": "2.0.0",
"keywords": [

@@ -266,29 +266,42 @@ "csv",

"engines": {
"node": ">=0.10"
"node": ">=4.0.0"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-browserify": "^4.0.1",
"grunt-contrib-jshint": "^0.11.2",
"grunt-contrib-uglify": "^0.11.0",
"grunt-contrib-watch": "^1.0.1",
"grunt-git": "^0.3.5",
"grunt-madge": "0.0.6",
"grunt-mocha-test": "^0.12.7",
"grunt-newer": "^1.1.0",
"imgur": "^0.1.5",
"load-grunt-tasks": "^3.4.0",
"@types/bluebird": "^3.5.20",
"@types/mocha": "^5.2.0",
"@types/node": "^10.0.1",
"coveralls": "^3.0.1",
"minimist": "^1.2.0",
"mocha": "^5.1.1",
"request": "^2.81.0",
"sinon": "^3.2.3"
"nyc": "^11.7.3",
"sinon": "^3.2.3",
"ts-node": "^6.0.3",
"typescript": "^2.8.3"
},
"dependencies": {
"bluebird": "^3.5.1",
"lodash": "^4.17.3",
"strip-bom": "^2.0.0"
},
"nyc": {
"extension": [
".ts",
".tsx"
],
"include": [
"./src/**/*.ts"
],
"all": true
},
"scripts": {
"test": "mocha ./test -R spec",
"test-debug": "mocha debug ./test -R spec",
"test-all": "mocha ./test -R spec && CSV_WORKER=3 mocha ./test -R spec "
"dev": "tsc -w",
"build": "rm -Rf ./v2 && tsc",
"test": "TS_NODE_CACHE=false mocha -r ts-node/register src/**/*.test.ts ./test/*.ts -R spec",
"travis": "nyc --reporter lcov mocha -r ts-node/register src/**/*.test.ts ./test/*.ts -R spec",
"test:debug": "mocha debug -r ts-node/register src/**/*.test.ts ./test/*.ts -R spec",
"test-all": "mocha ./test -R spec && CSV_WORKER=3 mocha ./test -R spec ",
"test:unit": "mocha -r ts-node/register src/**/*.test.ts",
"test:all:debug": "mocha debug ./testNew -R spec",
"coverage": "nyc --reporter html mocha -r ts-node/register src/**/*.test.ts ./test/*.ts -R spec",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
},

@@ -298,3 +311,4 @@ "browser": {

"fs": false
}
},
"main": "./v2/index.js"
}
[![Build Status](https://travis-ci.org/Keyang/node-csvtojson.svg?branch=master)](https://travis-ci.org/Keyang/node-csvtojson)
[![Coverage Status](https://coveralls.io/repos/github/Keyang/node-csvtojson/badge.svg?branch=master)](https://coveralls.io/github/Keyang/node-csvtojson?branch=master)
# CSVTOJSON
`csvtojson` module is a comprehensive nodejs csv parser to convert csv to json or column arrays. It can be used as node.js library / command line tool / or in browser with help of `browserify` or `webpack`. Below are some features:
`csvtojson` module is a comprehensive nodejs csv parser to convert csv to json or column arrays. It can be used as node.js library / command line tool / or in browser. Below are some features:
* Large csv file parsing with low memory (stream support)
* Node.JS / Browser (with WebPack) support
* Easy to use yet abundant API / parameters
* Commandline support
* Multiple output format support
* Multiple input source support
* Error catching
* Non-blocking parsing
* [Extremely fast](https://github.com/Keyang/node-csvtojson/blob/develop/docs/performance.md): **4 - 6 times faster** than other csv parsers on node.js
* Support node.js 0.10 + to latest
* Commandline tool
* Multiple output format -- json / csv / lines
* Error handling
* [Extremely fast](https://github.com/Keyang/csvbench) -- targeting on millions of lines csv data
* node.js 4+ to latest

@@ -30,2 +29,19 @@ # Donation

# Upgrade to V2
`csvtojson` has released version `2.0.0`.
* To upgrade to v2, please follow [upgrading guide](https://github.com/Keyang/node-csvtojson/blob/master/docs/csvtojson-v2.md)
* If you are looking for documentation for `v1`, open [this page](https://github.com/Keyang/node-csvtojson/blob/master/docs/readme.v1.md).
It is still able to use v1 with `csvtojson@2.0.0`
```js
// v1
const csvtojsonV1=require("csvtojson/v1");
// v2
const csvtojsonV2=require("csvtojson");
const csvtojsonV2=require("csvtojson/v2");
```
# Menu

@@ -51,25 +67,5 @@

### From CSV String
### From CSV File to JSON Array
```js
/**
csvStr:
1,2,3
4,5,6
7,8,9
*/
const csv=require('csvtojson')
csv({noheader:true})
.fromString(csvStr)
.on('csv',(csvRow)=>{ // this func will be called 3 times
console.log(csvRow) // => [1,2,3] , [4,5,6] , [7,8,9]
})
.on('done',()=>{
//parsing finished
})
```
### From CSV File
```js
/** csv file

@@ -84,27 +80,34 @@ a,b,c

.fromFile(csvFilePath)
.on('json',(jsonObj)=>{
// combine csv header row and csv line to a json object
// jsonObj.a ==> 1 or 4
.then((jsonObj)=>{
console.log(jsonObj);
/**
* [
* {a:"1", b:"2", c:"3"},
* {a:"4", b:"5". c:"6"}
* ]
*/
})
.on('done',(error)=>{
console.log('end')
})
// Async / await usage
const jsonArray=await csv().fromFile(csvFilePath);
```
Note that `.fromFile(filePath[ ,cb ,options])` takes an `options` parameter which will be passed to `fs.createReadStream()`. See [here](https://nodejs.org/dist/latest-v6.x/docs/api/fs.html#fs_fs_createreadstream_path_options) for docs.
### From CSV String to CSV Row
### From CSV Stream
```js
//const csvReadStream -- Readable stream for csv source
/**
csvStr:
1,2,3
4,5,6
7,8,9
*/
const csv=require('csvtojson')
csv()
.fromStream(csvReadStream)
.on('csv',(csvRow)=>{
// csvRow is an array
csv({
noheader:true,
output: "csv"
})
.on('done',(error)=>{
.fromString(csvStr)
.then((csvRow)=>{
console.log(csvRow) // => [["1","2","3"], ["4","5","6"], ["7","8","9"]]
})

@@ -114,4 +117,5 @@

### From CSV Url
### Asynchronously process each line from csv url
```js

@@ -123,12 +127,11 @@ const request=require('request')

.fromStream(request.get('http://mywebsite.com/mycsvfile.csv'))
.on('csv',(csvRow)=>{
// csvRow is an array
})
.on('done',(error)=>{
.subscribe((json)=>{
return new Promise((resolve,reject)=>{
// long operation for each json e.g. transform / write into database.
})
},onError,onComplete);
})
```
### Convert to CSV row arrays with csv header row
### Convert to CSV lines

@@ -144,34 +147,12 @@ ```js

const csv=require('csvtojson')
csv()
csv({output:"line"})
.fromString(csvStr)
.on('csv',(csvRow)=>{ //this func will be called twice. Header row will not be populated
// csvRow => [1,2,3] and [4,5,6]
.subscribe((csvLine)=>{
// csvLine => "1,2,3" and "4,5,6"
})
.on('done',()=>{
console.log('end')
})
```
### Convert to JSON without csv header row
```js
/**
csvStr:
1,2,3
4,5,6
7,8,9
*/
const csv=require('csvtojson')
csv({noheader:true})
.fromString(csvStr)
.on('json',(json)=>{ //this func will be called 3 times
// json.field1 => 1,4,7
// json.field2 => 2,5,8
// json.field3 => 3,6,9
})
.on('done',()=>{
console.log('end')
})
```
To find more detailed usage, please see [API](#api) section

@@ -201,8 +182,2 @@ ## Command Line Usage

Use multiple cpu-cores:
```
$ csvtojson --workerNum=4 source.csv > converted.json
```
Pipe in csv data:

@@ -222,11 +197,4 @@

```js
const csv=require('csvtojson')
const converter=csv(params) //params see below Parameters section
```
In above, `converter` is an instance of Converter which is a subclass of node.js `Transform` class.
* [Parameters](#parameters)
* [Asynchronouse Result Process](#asynchronouse-result-process)
* [Events](#events)

@@ -265,2 +233,3 @@ * [Hook / Transform](#hook--transform)

* **output**: The format to be converted to. "json" (default) -- convert csv to json. "csv" -- convert csv to csv row array. "line" -- convert csv to csv line string
* **delimiter**: delimiter used for seperating columns. Use "auto" if delimiter is unknown in advance, in this case, delimiter will be auto-detected (by best attempt). Use an array to give a list of potential delimiters e.g. [",","|","$"]. default: ","

@@ -270,5 +239,4 @@ * **quote**: If a column contains delimiter, it is able to use quote character to surround the column content. e.g. "hello, world" wont be split into two columns while parsing. Set to "off" will ignore all quotes. default: " (double quote)

* **checkType**: This parameter turns on and off whether check field type. Default is false. (The default is `true` if version < 1.1.4)
* **toArrayString**: Stringify the stream output to JSON array. This is useful when pipe output to a file which expects stringified JSON array. default is false and only stringified JSON (without []) will be pushed to downstream.
* **ignoreEmpty**: Ignore the empty value in CSV columns. If a column value is not given, set this to true to skip them. Default: false.
* **workerNum**: Number of worker processes. The worker process will use multi-cores to help process CSV data. Set to number of Core to improve the performance of processing large csv file. Keep 1 for small csv files. Default 1.
* **fork (experimental)**: Fork another process to parse the CSV stream. It is effective if many concurrent parsing sessions for large csv files. Default: false
* **noheader**:Indicating csv data has no header row and first row is data row. Default is false. See [header row](#header-row)

@@ -281,5 +249,5 @@ * **headers**: An array to specify the headers of CSV data. If --noheader is false, this value will override CSV header row. Default: null. Example: ["my field","name"]. See [header row](#header-row)

* **escape**: escape character used in quoted column. Default is double quote (") according to RFC4108. Change to back slash (\\) or other chars for your own case.
* **includeColumns**: This parameter instructs the parser to include only those columns as specified by an array of column indexes or header names. Example: [0,2,3,"name"] will parse and include only columns 0, 2, 3, and column with header "name" in the JSON output.
* **ignoreColumns**: This parameter instructs the parser to ignore columns as specified by an array of column indexes or header names. Example: [1,3,5,"title","age"] will ignore columns 1, 3, 5, title column and age column and will not return them in the JSON output.
* **colParser**: Allows override parsing logic for a specific column. It accepts a JSON object with fields like: `headName: <String | Function>` . e.g. {field1:'number'} will use built-in number parser to convert value of the `field1` column to number. Another example {"name":nameProcessFunc} will use specified function to parse the value. See [details below](#column-parser)
* **includeColumns**: This parameter instructs the parser to include only those columns as specified by the regular expression. Example: /(name|age)/ will parse and include columns whose header contains "name" or "age"
* **ignoreColumns**: This parameter instructs the parser to ignore columns as specified by the regular expression. Example: /(name|age)/ will ignore columns whose header contains "name" or "age"
* **colParser**: Allows override parsing logic for a specific column. It accepts a JSON object with fields like: `headName: <String | Function | ColParser>` . e.g. {field1:'number'} will use built-in number parser to convert value of the `field1` column to number. For more information See [details below](#column-parser)
* **alwaysSplitAtEOL**: Always interpret each line (as defined by `eol`) as a row. This will prevent `eol` characters from being used within a row (even inside a quoted field). This ensures that misplaced quotes only break on row, and not all ensuing rows.

@@ -289,56 +257,45 @@

## Asynchronouse Result Process
## Events
Since `v2.0.0`, asynchronouse processing has been fully supported.
`Converter` class defined a series of events.
e.g. Process each JSON result asynchronousely.
### header
`header` event is emitted for each CSV file. It passes an array object which contains the names of the header row.
```js
const csv=require('csvtojson')
csv()
.on('header',(header)=>{
//header=> [header1, header2, header3]
csv().fromFile(csvFile)
.subscribe((json)=>{
return new Promise((resolve,reject)=>{
// Async operation on the json
// dont forget to call resolve and reject
})
})
```
For more details please read:
`header` is always an array of strings without types.
* [Add Promise and Async / Await support](https://github.com/Keyang/node-csvtojson/blob/master/docs/csvtojson-v2.md#add-promise-and-async--await-support)
* [Add asynchronous line by line processing support](https://github.com/Keyang/node-csvtojson/blob/master/docs/csvtojson-v2.md#add-asynchronous-json-result-processing-support)
* [Async Hooks Support](https://github.com/Keyang/node-csvtojson/blob/master/docs/csvtojson-v2.md#async-hooks-support)
`header` event will be emitted regardless of the `noHeaders` parameter setting.
### json
## Events
`json` event is emitted for each parsed CSV line. It passes JSON object and the row number of the CSV line in its callback function.
`Converter` class defined a series of events.
```js
const csv=require('csvtojson')
csv()
.on('json',(jsonObj, rowIndex)=>{
//jsonObj=> {header1:cell1,header2:cell2}
//rowIndex=> number
})
```
### header
### csv
`header` event is emitted for each CSV file once. It passes an array object which contains the names of the header row.
`csv` event is emitted for each CSV line. It passes an array object which contains cells content of one csv row.
```js
const csv=require('csvtojson')
csv()
.on('csv',(csvRow, rowIndex)=>{
//csvRow=> [cell1, cell2, cell3]
//rowIndex=> number
.on('header',(header)=>{
//header=> [header1, header2, header3]
})
```
`csvRow` is always an array of strings without types.
`header` is always an array of strings without types.
`csv` event is the fastest parse event while `json` and `data` event is about 2 times slower. Thus if `csv` is enough, for best performance, just use it without `json` and `data` event.
### data
`data` event is emitted for each parsed CSV line. It passes buffer of strigified JSON unless `objectMode` is set true in stream option.
`data` event is emitted for each parsed CSV line. It passes buffer of strigified JSON in [ndjson format](http://ndjson.org/) unless `objectMode` is set true in stream option.

@@ -365,3 +322,3 @@ ```js

Note that if `error` being emitted, the process will stop as node.js will automatically `unpipe()` upper-stream and chained down-stream<sup>1</sup>. This will cause `end` / `end_parsed` event never being emitted because `end` event is only emitted when all data being consumed <sup>2</sup>.
Note that if `error` being emitted, the process will stop as node.js will automatically `unpipe()` upper-stream and chained down-stream<sup>1</sup>. This will cause `end` event never being emitted because `end` event is only emitted when all data being consumed <sup>2</sup>. If need to know when parsing finished, use `done` event instead of `end`.

@@ -371,30 +328,5 @@ 1. [Node.JS Readable Stream](https://github.com/nodejs/node/blob/master/lib/_stream_readable.js#L572-L583)

### record_parsed
`record_parsed` event is emitted for each parsed CSV line. It is combination of `json` and `csv` events. For better performance, try to use `json` and `csv` instead.
```js
const csv=require('csvtojson')
csv()
.on('record_parsed',(jsonObj, row, index)=>{
})
```
### end
`end` event is emitted when all CSV lines being parsed.
### end_parsed
`end_parsed` event is emitted when all CSV lines being parsed. The only difference between `end_parsed` and `end` events is `end_parsed` will pass in a JSON array which contains all JSON objects. For better performance, try to use `end` event instead.
```js
const csv=require('csvtojson')
csv()
.on('end_parsed',(jsonArrObj)=>{
})
```
### done
`done` event is emitted either after `end` or `error`. This indicates the processor has stopped.
`done` event is emitted either after parsing successfully finished or any error happens. This indicates the processor has stopped.

@@ -415,20 +347,31 @@ ```js

the hook -- `preRawData` will be called with csv string passed to parser.
```js
const csv=require('csvtojson')
// synchronouse
csv()
.preRawData((csvRawData,cb)=>{
var newData=csvRawData.replace('some value','another value')
cb(newData);
.preRawData((csvRawData)=>{
var newData=csvRawData.replace('some value','another value');
return newData;
})
.on('json',(jsonObj)=>{
});
// asynchronouse
csv()
.preRawData((csvRawData)=>{
return new Promise((resolve,reject)=>{
var newData=csvRawData.replace('some value','another value');
resolve(newData);
})
})
```
the function in `preRawData` will be called directly with the string from upper stream.
### CSV File Line Hook
the function is called each time a file line has been parsed in csv stream. the `lineIdx` is the file line number in the file starting with 0.
```js
const csv=require('csvtojson')
// synchronouse
csv()

@@ -441,8 +384,14 @@ .preFileLine((fileLineString, lineIdx)=>{

})
.on('json',(jsonObj)=>{
});
// asynchronouse
csv()
.preFileLine((fileLineString, lineIdx)=>{
return new Promise((resolve,reject)=>{
// async function processing the data.
})
})
```
the function is called each time a file line being found in csv stream. the `lineIdx` is the file line number in the file. The function should return a string to processor.

@@ -452,9 +401,16 @@

To transform result that is sent to downstream, use `.subscribe` method for each json populated.
```js
const csv=require('csvtojson')
csv()
.transf((jsonObj,csvRow,index)=>{
.subscribe((jsonObj,index)=>{
jsonObj.myNewKey='some value'
// OR asynchronousely
return new Promise((resolve,reject)=>{
jsonObj.myNewKey='some value';
resolve();
})
})
.on('json',(jsonObj)=>{
.on('data',(jsonObj)=>{
console.log(jsonObj.myNewKey) // some value

@@ -464,12 +420,6 @@ });

`Transform` happens after CSV being parsed before result being emitted or pushed to downstream. This means if `jsonObj` is changed, the corresponding field in `csvRow` will not change. Vice versa. The events will emit changed value and downstream will receive changed value.
`Transform` will cause some performance panelties because it voids optimisation mechanism. Try to use Node.js `Transform` class as downstream for transformation instead.
## Nested JSON Structure
One of the powerful feature of `csvtojson` is the ability to convert csv line to a nested JSON by correctly defining its csv header row. This is default out-of-box feature.
`csvtojson` is able to convert csv line to a nested JSON by correctly defining its csv header row. This is default out-of-box feature.

@@ -526,5 +476,5 @@ Here is an example. Original CSV:

### No nested JSON
### Flat Keys
In case to not produce nested JSON, simply set `flatKeys:true` in parameters.
In order to not produce nested JSON, simply set `flatKeys:true` in parameters.

@@ -539,3 +489,3 @@ ```js

.fromString(csvStr)
.on('json',(jsonObj)=>{
.subscribe((jsonObj)=>{
//{"a.b":1,"a.c":2} rather than {"a":{"b":1,"c":2}}

@@ -578,42 +528,10 @@ });

## Multi CPU Core Support
## Column Parser
This is an experimental feature.
`Column Parser` allows writing a custom parser for a column in CSV data.
`csvtojson` has built-in workers to allow CSV parsing happening on another process and leave Main Process non-blocked. This is very useful when dealing with large csv data on a webserver so that parsing CSV will not block the entire server due to node.js being single threaded.
**What is Column Parser**
It is also useful when dealing with tons of CSV data on command line. Multi-CPU core support will dramatically reduce the time needed.
When `csvtojson` walks through csv data, it converts value in a cell to something else. For example, if `checkType` is `true`, `csvtojson` will attempt to find a proper type parser according to the cell value. That is, if cell value is "5", a `numberParser` will be used and all value under that column will use the `numberParser` to transform data.
To enable multi-cpu core, simply do:
```js
csv({
workerNum:4 // workerNum>=1
})
```
or in command line:
```
$ csvtojson --workerNum=4
```
This will create 3 extra workers. Main process will only be used for delegating data / emitting result / pushing to downstream. Just keep in mind, those operations on Main process are not free and it will still take a certain amount CPU time.
See [here](https://github.com/Keyang/node-csvtojson/blob/develop/docs/performance.md#cpu-usage-leverage) for how `csvtojson` leverages CPU usage when using multi-cores.
### Limitations
There are some limitations when using multi-core feature:
* Does not support if a column contains line break.
* Cannot use `function` in `colParser` parameter as worker process wont be able to access the function.
## Column Parser
Although `csvtojson` has a bunch of built in parameters, it will not cover all the edge cases. `Column Parser` allows developers using specified parser for a specified column.
Differ from `transform` which works on output json of the parser, `colParser` will override existing parsing logic of your own to construct json result (which may be `transform` after that).
### Built-in parsers

@@ -644,3 +562,3 @@

.fromString(csvString)
.on("json",(jsonObj)=>{
.subscribe((jsonObj)=>{
//jsonObj: {column2:"1234"}

@@ -650,5 +568,5 @@ })

### Custom parsers
### Custom parsers function
Sometimes, developers need to define custom parser. It is able to pass a function to specific column in `colParser`.
Sometimes, developers want to define custom parser. It is able to pass a function to specific column in `colParser`.

@@ -680,23 +598,29 @@ Example:

the returned value will be used in result JSON object. returning `undefined` will not change result JSON object. You can do following:
the returned value will be used in result JSON object. returning `undefined` will not change result JSON object.
### Flat key column
It is also able to mark a column as `flat`:
```js
/*csv data
user.name, birthday
Joe, 1970-01-01
/*csv string
person.comment,person.number
hello,1234
*/
csv({
colParser:{
"user.name":function(item, head, resultRow, row , colIdx){
resultRow[head]=item;
"person.number":{
flat:true,
cellParser: "number" // string or a function
}
}
})
.fromString(csvString)
.subscribe((jsonObj)=>{
//jsonObj: {"person.number":1234,"person":{"comment":"hello"}}
})
```
without the parser the json is like {user:{name:Joe}}, with the parser the json is like {"user.name":Joe}
# Contribution

@@ -718,2 +642,4 @@

##
## 1.1.7

@@ -751,5 +677,5 @@

Version 1.1.0 has added new features and optimised lib performance. It also introduced simpler APIs to use. Thus readme is re-written to adapt the preferred new APIs. The lib will support old APIs. To review the old readme please [click here](https://github.com/Keyang/node-csvtojson/blob/develop/readme-old.md).
Version 1.1.0 has added new features and optimised lib performance. It also introduced simpler APIs to use. Thus readme is re-written to adapt the preferred new APIs. The lib will support old APIs. To review the old readme please [click here](https://github.com/Keyang/node-csvtojson/blob/master/readme-old.md).
* [Performance Optimisation](https://github.com/Keyang/node-csvtojson/blob/develop/docs/performance.md#performance-optimisation): V1.1.0 is 30%-50% faster
* [Performance Optimisation](https://github.com/Keyang/node-csvtojson/blob/master/docs/performance.md#performance-optimisation): V1.1.0 is 30%-50% faster
* Better error tolerance

@@ -756,0 +682,0 @@ * Simplified API (see below)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc