Comparing version 3.5.1 to 3.6.0
@@ -16,4 +16,4 @@ #!/usr/bin/env node | ||
.version(pkg.version) | ||
.option('-i, --input <input>', 'Path and name of the incoming json file.') | ||
.option('-o, --output [output]', 'Path and name of the resulting csv file. Defaults to console.') | ||
.option('-i, --input <input>', 'Path and name of the incoming json file. If not provided, will read from stdin.') | ||
.option('-o, --output [output]', 'Path and name of the resulting csv file. Defaults to stdout.') | ||
.option('-f, --fields <fields>', 'Specify the fields to convert.') | ||
@@ -83,3 +83,3 @@ .option('-l, --fieldList [list]', 'Specify a file with a list of fields to include. One field per line.') | ||
function logPretty(csv, callback) { | ||
function logPretty(csv) { | ||
var lines = csv.split(os.EOL); | ||
@@ -95,7 +95,4 @@ var table = new Table({ | ||
table.push(lines[i].split('","')); | ||
if (i === lines.length - 1) { | ||
callback(table.toString()); | ||
} | ||
} | ||
return table.toString(); | ||
} | ||
@@ -135,28 +132,21 @@ | ||
json2csv(opts, function (csvError, csv) { | ||
if (csvError) { | ||
debug(csvError); | ||
} | ||
var csv = json2csv(opts); | ||
if (program.output) { | ||
fs.writeFile(program.output, csv, function (writeError) { | ||
if (writeError) { | ||
throw new Error('Cannot save to ' + program.output + ': ' + writeError); | ||
} | ||
if (program.output) { | ||
fs.writeFile(program.output, csv, function (writeError) { | ||
if (writeError) { | ||
throw new Error('Cannot save to ' + program.output + ': ' + writeError); | ||
} | ||
debug(program.input + ' successfully converted to ' + program.output); | ||
}); | ||
debug(program.input + ' successfully converted to ' + program.output); | ||
}); | ||
} else { | ||
/*eslint-disable no-console */ | ||
if (program.pretty) { | ||
console.log(logPretty(csv)); | ||
} else { | ||
/*eslint-disable no-console */ | ||
if (program.pretty) { | ||
logPretty(csv, function (res) { | ||
console.log(res); | ||
}); | ||
} else { | ||
console.log(csv); | ||
} | ||
/*eslint-enable no-console */ | ||
console.log(csv); | ||
} | ||
}); | ||
/*eslint-enable no-console */ | ||
} | ||
}); | ||
}); |
@@ -0,1 +1,8 @@ | ||
## 3.6.0 / 2016-07-07 | ||
* Make callback optional | ||
* Make callback use `process.nextTick`, so it's not sync | ||
Thanks @STRML! | ||
## 3.5.1 / 2016-06-29 | ||
@@ -2,0 +9,0 @@ |
@@ -28,5 +28,6 @@ declare namespace json2csv { | ||
export function json2csv(options: IOptions, callback: ICallback): string; | ||
export function json2csv(options: IOptions, callback: ICallback): void; | ||
export function json2csv(options: IOptions): string; | ||
} | ||
export = json2csv.json2csv; |
@@ -9,3 +9,3 @@ /** | ||
/** | ||
* Main function that converts json to csv | ||
* Main function that converts json to csv. | ||
* | ||
@@ -15,3 +15,3 @@ * @param {Object} params Function parameters containing data, fields, | ||
* and default value (default is '') | ||
* @param {Function} callback(err, csv) - Callback function | ||
* @param {Function} [callback] Callback function | ||
* if error, returning error in call back. | ||
@@ -21,16 +21,25 @@ * if csv is created successfully, returning csv output to callback. | ||
module.exports = function (params, callback) { | ||
if (!callback || typeof callback !== 'function') { | ||
throw new Error('Callback is required'); | ||
} | ||
var hasCallback = typeof callback === 'function'; | ||
checkParams(params, function (err) { | ||
if (err) { | ||
return callback(err); | ||
var err; | ||
try { | ||
checkParams(params); | ||
} catch (err) { | ||
if (hasCallback) { | ||
return process.nextTick(function () { | ||
callback(err); | ||
}); | ||
} else { | ||
throw err; | ||
} | ||
var titles = createColumnTitles(params); | ||
var csv = createColumnContent(params, titles); | ||
callback(null, csv); | ||
}); | ||
} | ||
var titles = createColumnTitles(params); | ||
var csv = createColumnContent(params, titles); | ||
if (hasCallback) { | ||
return process.nextTick(function () { | ||
callback(null, csv); | ||
}); | ||
} else { | ||
return csv; | ||
} | ||
}; | ||
@@ -40,9 +49,10 @@ | ||
/** | ||
* Check passing params | ||
* Check passing params. | ||
* | ||
* Note that this modifies params. | ||
* | ||
* @param {Object} params Function parameters containing data, fields, | ||
* delimiter, default value, mark quotes and hasCSVColumnTitle | ||
* @param {Function} callback Callback function returning error when invalid field is found | ||
*/ | ||
function checkParams(params, callback) { | ||
function checkParams(params) { | ||
params.data = params.data || []; | ||
@@ -62,3 +72,3 @@ | ||
if (!params.fields && (params.data.length === 0 || typeof params.data[0] !== 'object')) { | ||
return callback(new Error('params should include "fields" and/or non-empty "data" array of objects')); | ||
throw new Error('params should include "fields" and/or non-empty "data" array of objects'); | ||
} | ||
@@ -70,3 +80,3 @@ params.fields = params.fields || Object.keys(params.data[0]); | ||
if (params.fieldNames && params.fieldNames.length !== params.fields.length) { | ||
return callback(new Error('fieldNames and fields should be of the same length, if fieldNames is provided.')); | ||
throw new Error('fieldNames and fields should be of the same length, if fieldNames is provided.'); | ||
} | ||
@@ -102,4 +112,2 @@ | ||
params.includeEmptyRows = params.includeEmptyRows || false; | ||
callback(null); | ||
} | ||
@@ -198,3 +206,3 @@ | ||
stringifiedElement = stringifiedElement.replace(/\\\\/g, '\\'); | ||
if (params.excelStrings && typeof val === 'string') { | ||
@@ -201,0 +209,0 @@ stringifiedElement = '"="' + stringifiedElement + '""'; |
{ | ||
"name": "json2csv", | ||
"preferGlobal": "true", | ||
"version": "3.5.1", | ||
"version": "3.6.0", | ||
"description": "Convert JSON to CSV", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -26,6 +26,10 @@ # json2csv | ||
json2csv({ data: myData, fields: fields }, function(err, csv) { | ||
if (err) console.log(err); | ||
console.log(csv); | ||
}); | ||
try { | ||
var result = json2csv({ data: myData, fields: fields }); | ||
console.log(result); | ||
} catch (err) { | ||
// Errors are thrown for bad options, or if the data is empty and no fields are provided. | ||
// Be sure to provide fields if it is possible that your data array will be empty. | ||
console.error(err); | ||
} | ||
``` | ||
@@ -66,3 +70,3 @@ | ||
- `includeEmptyRows` - Boolean, includes empty rows. Defaults to `false`. | ||
- `callback` - **Required**; `function (error, csvString) {}`. To create a promise, you can use `var toCSV = Bluebird.promisify(json2csv)`, see [Bluebird] docs. | ||
- `callback` - `function (error, csvString) {}`. If provided, will callback asynchronously. Only supported for compatibility reasons. | ||
@@ -117,9 +121,7 @@ #### Example `fields` option | ||
]; | ||
var csv = json2csv({ data: myCars, fields: fields }); | ||
json2csv({ data: myCars, fields: fields }, function(err, csv) { | ||
if (err) console.log(err); | ||
fs.writeFile('file.csv', csv, function(err) { | ||
if (err) throw err; | ||
console.log('file saved'); | ||
}); | ||
fs.writeFile('file.csv', csv, function(err) { | ||
if (err) throw err; | ||
console.log('file saved'); | ||
}); | ||
@@ -139,3 +141,4 @@ ``` | ||
Similarly to [mongoexport](http://www.mongodb.org/display/DOCS/mongoexport) you can choose which fields to export | ||
Similarly to [mongoexport](http://www.mongodb.org/display/DOCS/mongoexport) you can choose which fields to export. | ||
Note: this example uses the optional callback format. | ||
@@ -168,7 +171,5 @@ ```javascript | ||
var fields = ['car', 'price', 'color']; | ||
var tsv = json2csv({ data: myCars, fields: fields, del: '\t' }); | ||
json2csv({ data: myCars, fields: fields, del: '\t' }, function(err, tsv) { | ||
if (err) console.log(err); | ||
console.log(tsv); | ||
}); | ||
console.log(tsv); | ||
``` | ||
@@ -196,7 +197,5 @@ | ||
var fieldNames = ['Car Name', 'Price USD']; | ||
var csv = json2csv({ data: myCars, fields: fields, fieldNames: fieldNames }); | ||
json2csv({ data: myCars, fields: fields, fieldNames: fieldNames }, function(err, csv) { | ||
if (err) console.log(err); | ||
console.log(csv); | ||
}); | ||
console.log(csv); | ||
``` | ||
@@ -218,7 +217,5 @@ | ||
}; | ||
var csv = json2csv(opts); | ||
json2csv(opts, function(err, csv) { | ||
if (err) console.log(err); | ||
console.log(csv); | ||
}); | ||
console.log(csv); | ||
``` | ||
@@ -258,9 +255,7 @@ | ||
]; | ||
var csv = json2csv({ data: myCars, fields: fields }); | ||
json2csv({ data: myCars, fields: fields }, function(err, csv) { | ||
if (err) console.log(err); | ||
fs.writeFile('file.csv', csv, function(err) { | ||
if (err) throw err; | ||
console.log('file saved'); | ||
}); | ||
fs.writeFile('file.csv', csv, function(err) { | ||
if (err) throw err; | ||
console.log('file saved'); | ||
}); | ||
@@ -446,3 +441,2 @@ ``` | ||
[CHANGELOG]: CHANGELOG.md | ||
[Bluebird]: http://bluebirdjs.com/docs/api/promise.promisify.html | ||
[flat]: https://www.npmjs.com/package/flat |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
360
27921
434