Socket
Socket
Sign inDemoInstall

json-2-csv

Package Overview
Dependencies
Maintainers
1
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-2-csv - npm Package Compare versions

Comparing version 2.4.0 to 3.0.0-beta.0

src/csv2json.js

5

package.json

@@ -5,3 +5,3 @@ {

"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
"version": "2.4.0",
"version": "3.0.0-beta.0",
"repository": {

@@ -36,4 +36,3 @@ "type": "git",

"istanbul": "0.4.5",
"should": "13.2.3",
"async": "2.6.1"
"should": "13.2.3"
},

@@ -40,0 +39,0 @@ "engines": {

47

README.md

@@ -8,2 +8,3 @@ # json-2-csv

[![NPM version](https://img.shields.io/npm/v/json-2-csv.svg)](https://www.npmjs.org/package/json-2-csv)
[![Maintainability](https://api.codeclimate.com/v1/badges/8c0cc3699d054fb77abe/maintainability)](https://codeclimate.com/github/mrodrig/json-2-csv/maintainability)
[![Known Vulnerabilities](https://snyk.io/test/npm/json-2-csv/badge.svg)](https://snyk.io/test/npm/json-2-csv)

@@ -29,2 +30,6 @@

## Upgrading?
Upgrading to v3 from v2? Check out the [upgrade guide](https://github.com/mrodrig/json-2-csv/blob/master/upgrade_guides/UPGRADE_2_to_3.md).
### API

@@ -40,9 +45,8 @@

* `field` - String - Field Delimiter.
* Default: `','`
* `array` - String - Array Value Delimiter.
* Default: `';'`
* Default: `,`
* `wrap` - String - Wrap values in the delimiter of choice (e.g. wrap values in quotes).
* Default: `''`
* Default: `"`
* `eol` - String - End of Line Delimiter.
* Default: `'\n'`
* Default: `\n`
* `excelBOM` - Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.
* `prependHeader` - Boolean - Should the auto-generated header be prepended as the first line in the CSV?

@@ -52,4 +56,2 @@ * Default: `true`

* Default: `false`
* `emptyFieldValue` - String - Value for fields without data _when not checking schemas_.
* Default: `'null'`
* `trimHeaderFields` - Boolean - Should the header fields be trimmed?

@@ -60,3 +62,3 @@ * Default: `false`

* `checkSchemaDifferences` - Boolean - Should all documents have the same schema?
* Default: `true`
* Default: `false`
* Note: Change this to `false` if some documents are missing certain fields and you still want to convert the data.

@@ -72,3 +74,3 @@ * `keys` - Array - Specify the keys (as strings) that should be converted.

#### Promisified Version: `converter.json2csvPromisified(array, options)`
#### Promisified Version: `converter.json2csvAsync(array, options)`

@@ -84,9 +86,9 @@ Available in version `2.2.0`, this functionality makes use of promises from the `bluebird` module.

* `field` - String - Field Delimiter.
* Default: `','`
* `array` - String - Array Value Delimiter.
* Default: `';'`
* Default: `,`
* `wrap` - String - The character that field values are wrapped in.
* Default: `''`
* Default: `"`
* `eol` - String - End of Line Delimiter.
* Default: `'\n'`
* Default: `\n`
* `excelBOM` - Boolean - Does the CSV contain a unicode character prepended in order to allow Excel to open a UTF-8 encoded file with non-ASCII characters present?
* Default: `false`
* `trimHeaderFields` - Boolean - Should the header fields be trimmed?

@@ -103,3 +105,3 @@ * Default: `false`

#### Promisified Version: `csv2jsonPromisified(csv, options)`
#### Promisified Version: `csv2jsonAsync(csv, options)`

@@ -114,3 +116,3 @@ Available in version `2.2.0`, this functionality makes use of promises from the `bluebird` module.

_Note_: This requires `mocha`, `should`, `async`, and `underscore`.
_Note_: This requires `mocha`, `should`, and `underscore`.

@@ -124,6 +126,6 @@ To see test coverage, please run:

```
Statements : 94.69% ( 196/207 )
Branches : 93.55% ( 145/155 )
Functions : 100% ( 34/34 )
Lines : 95.38% ( 186/195 )
Statements : 100% ( 245/245 )
Branches : 100% ( 122/122 )
Functions : 100% ( 48/48 )
Lines : 100% ( 243/243 )
```

@@ -136,3 +138,3 @@

* Header Generation (per document keys)
* Allows for conversion of specific keys in both json2csv and csv2json via the options.KEYS parameter (as of 1.1.2)
* Allows for conversion of specific keys in both json2csv and csv2json via the options.keys parameter (as of 1.1.2)
* Verifies all documents have same schema (schema field order does not matter as of 1.1.0)

@@ -147,3 +149,4 @@ * Supports sub-documents natively

* Support for multiple different schemas (as of 1.4.0)
* Promisified versions of the functions are now available by default: json2csvPromisified, csv2jsonPromisified (as of 2.2.0)
* Promisified versions of the functions are now available by default: json2csvAsync, csv2jsonAsync (as of 2.2.0)
* Nested quotes are escaped with an additional quote (per [RFC 4180](https://tools.ietf.org/html/rfc4180)) thanks to @eric-thelin (as of 2.3.0)
* RFC 4180 Compliance (as of 3.0.0)
{
"Errors" : {
"errors" : {
"delimitersMustDiffer": "The field and array delimiters must differ.",
"callbackRequired": "A callback is required!",

@@ -9,4 +8,4 @@ "optionsRequired": "Options were not passed and are required.",

"json2csv": {
"cannotCallJson2CsvOn": "Cannot call json2csv on ",
"dataNotArrayOfDocuments": "Data provided was not an array of documents.",
"cannotCallOn": "Cannot call json2csv on ",
"dataCheckFailure": "Data provided was not an array of documents.",
"notSameSchema": "Not all documents have the same schema."

@@ -16,5 +15,4 @@ },

"csv2json": {
"cannotCallCsv2JsonOn": "Cannot call csv2json on ",
"csvNotString": "CSV is not a string.",
"noDataRetrieveHeading": "No data provided to retrieve heading."
"cannotCallOn": "Cannot call csv2json on ",
"dataCheckFailure": "CSV is not a string."
}

@@ -24,18 +22,21 @@

"DefaultOptions" : {
"DELIMITER" : {
"FIELD" : ",",
"ARRAY" : ";",
"WRAP" : "",
"EOL" : "\n"
"defaultOptions" : {
"delimiter" : {
"field" : ",",
"wrap" : "\"",
"eol" : "\n"
},
"PREPEND_HEADER" : true,
"TRIM_HEADER_FIELDS": false,
"TRIM_FIELD_VALUES" : false,
"SORT_HEADER" : false,
"PARSE_CSV_NUMBERS" : false,
"KEYS" : null,
"CHECK_SCHEMA_DIFFERENCES": true,
"EMPTY_FIELD_VALUE": "null"
"excelBOM": false,
"prependHeader" : true,
"trimHeaderFields": false,
"trimFieldValues" : false,
"sortHeader" : false,
"parseCsvNumbers" : false,
"keys" : null,
"checkSchemaDifferences": false
},
"values" : {
"excelBOM": "\ufeff"
}
}
'use strict';
let json2Csv = require('./json-2-csv'), // Require our json-2-csv code
csv2Json = require('./csv-2-json'), // Require our csv-2-json code
constants = require('./constants.json'), // Require in constants
docPath = require('doc-path'),
promise = require('bluebird'),
_ = require('underscore');
let {Json2Csv} = require('./json2csv'), // Require our json-2-csv code
{Csv2Json} = require('./csv2json'), // Require our csv-2-json code
utilities = require('./utils'),
Promise = require('bluebird');

@@ -13,84 +11,20 @@ module.exports = {

csv2json : csv2json,
json2csvPromisified : promise.promisify(json2csv),
csv2jsonPromisified : promise.promisify(csv2json)
json2csvAsync : Promise.promisify(json2csv),
csv2jsonAsync : Promise.promisify(csv2json)
};
/**
* Default options
*/
let defaultOptions = constants.DefaultOptions;
function isDefined(val) {
return !_.isUndefined(val);
}
function copyOption(options, lowercasePath, uppercasePath) {
let lowerCaseValue = docPath.evaluatePath(options, lowercasePath);
if (isDefined(lowerCaseValue)) {
docPath.setPath(options, uppercasePath, lowerCaseValue);
}
}
/**
* Build the options to be passed to the appropriate function
* If a user does not provide custom options, then we use our default
* If options are provided, then we set each valid key that was passed
*/
function buildOptions(opts, cb) {
// PREVIOUS VERSION SUPPORT (so that future versions are backwards compatible)
// Issue #26: opts.EOL should be opts.DELIMITER.EOL -- this will move the option & provide backwards compatibility
if (docPath.evaluatePath(opts, 'EOL')) { docPath.setPath(opts, 'DELIMITER.EOL', opts.EOL); }
// #62: Allow for lower case option names
if (opts) {
copyOption(opts, 'prependHeader', 'PREPEND_HEADER');
copyOption(opts, 'trimHeaderFields', 'TRIM_HEADER_FIELDS');
copyOption(opts, 'trimFieldValues', 'TRIM_FIELD_VALUES');
copyOption(opts, 'sortHeader', 'SORT_HEADER');
copyOption(opts, 'parseCsvNumbers', 'PARSE_CSV_NUMBERS');
copyOption(opts, 'keys', 'KEYS');
copyOption(opts, 'checkSchemaDifferences', 'CHECK_SCHEMA_DIFFERENCES');
copyOption(opts, 'emptyFieldValue', 'EMPTY_FIELD_VALUE');
if (isDefined(opts.delimiter)) {
copyOption(opts, 'delimiter.field', 'DELIMITER.FIELD');
copyOption(opts, 'delimiter.array', 'DELIMITER.ARRAY');
copyOption(opts, 'delimiter.wrap', 'DELIMITER.WRAP');
copyOption(opts, 'delimiter.eol', 'DELIMITER.EOL');
}
}
opts = _.defaults(opts || {}, defaultOptions);
// Note: _.defaults does a shallow default, we need to deep copy the DELIMITER object
opts.DELIMITER = _.defaults(opts.DELIMITER || {}, defaultOptions.DELIMITER);
// If the delimiter fields are the same, report an error to the caller
if (opts.DELIMITER.FIELD === opts.DELIMITER.ARRAY) { return cb(new Error(constants.Errors.delimitersMustDiffer)); }
// Otherwise, send the options back
return cb(null, opts);
}
/**
* Client accessible json2csv function
* Takes an array of JSON documents to be converted, a callback that will be called with (err, csv)
* after processing is complete, and optional options
* @param array Object[] data to be converted
* @param callback Function callback
* @param opts Object options object
* @param documents {Array<Object>} data to be converted
* @param callback {Function} callback function
* @param options {Object} options object
*/
function json2csv(array, callback, opts) {
// If this was promisified (callback and opts are swapped) then fix the argument order.
if (_.isObject(callback) && !_.isFunction(callback)) {
let func = opts;
opts = callback;
callback = func;
}
buildOptions(opts, function (err, options) { // Build the options
if (err) {
return callback(err);
} else {
json2Csv.json2csv(options, array, callback); // Call our internal json2csv function
}
function json2csv(documents, callback, options) {
return utilities.convert({
data: documents,
callback,
options,
converter: Json2Csv
});

@@ -104,21 +38,13 @@ }

* with (err, json) after processing is complete, and optional options
* @param csv
* @param callback
* @param opts
* @param csv String csv data to be converted
* @param callback Function callback
* @param options Object options object
*/
function csv2json(csv, callback, opts) {
// If this was promisified (callback and opts are swapped) then fix the argument order.
if (_.isObject(callback) && !_.isFunction(callback)) {
let func = opts;
opts = callback;
callback = func;
}
buildOptions(opts, function (err, options) { // Build the options
if (err) {
return callback(err);
} else {
csv2Json.csv2json(options, csv, callback); // Call our internal csv2json function
}
function csv2json(csv, callback, options) {
return utilities.convert({
data: csv,
callback,
options,
converter: Csv2Json
});
}
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