Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always use lodash.get #74

Merged
merged 1 commit into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ or [use it from the CLI](https://github.com/zemirco/json2csv#command-line-interf
- `del` - String, delimiter of columns. Defaults to `,` if not specified.
- `defaultValue` - String, default value to use when missing data. Defaults to `` if not specified.
- `quotes` - String, quotes around cell values and column names. Defaults to `"` if not specified.
- `nested` - Boolean, enables nested fields for getting JSON data. Defaults to `false` if not specified.
- `eol` - String, it gets added to each row of data. Defaults to `` if not specified.
- `newLine` - String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
- `callback` - **Required**; `function (error, csvString) {}`.
Expand Down Expand Up @@ -217,7 +216,7 @@ var myCars = [
}
];

json2csv({ data: myCars, fields: fields, nested: true }, function(err, csv) {
json2csv({ data: myCars, fields: fields }, function(err, csv) {
if (err) console.log(err);
fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
Expand Down Expand Up @@ -256,7 +255,6 @@ Usage: json2csv [options]
-e, --eol [value] Specify an EOL value after each row.
-z, --newLine [value] Specify an new line value for separating rows.
-q, --quote [value] Specify an alternate quote value.
-x, --nested Allow fields to be nested via dot notation, e.g. 'car.make'.
-n, --no-header Disable the column name header
-L, --ldjson Treat the input as Line-Delimited JSON.
-p, --pretty Use only when printing to console. Logs output in pretty tables.
Expand Down
2 changes: 0 additions & 2 deletions bin/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ program
.option('-e, --eol [value]', 'Specify an EOL value after each row.')
.option('-z, --newLine [value]', 'Specify an new line value for separating rows.')
.option('-q, --quote [value]', 'Specify an alternate quote value.')
.option('-x, --nested', 'Allow fields to be nested via dot notation, e.g. \'car.make\'.')
.option('-n, --no-header', 'Disable the column name header')
.option('-L, --ldjson', 'Treat the input as Line-Delimited JSON.')
.option('-p, --pretty', 'Use only when printing to console. Logs output in pretty tables.')
Expand Down Expand Up @@ -112,7 +111,6 @@ getFields(function (err, fields) {

opts.hasCSVColumnTitle = program.header;
opts.quotes = program.quote;
opts.nested = program.nested;
opts.defaultValue = program.defaultValue;

if (program.delimiter) {
Expand Down
16 changes: 2 additions & 14 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Module dependencies.
*/
var os = require('os');
var get = require('lodash.get');
var lodashGet = require('lodash.get');

/**
* Main function that converts json to csv
Expand Down Expand Up @@ -74,9 +74,6 @@ function checkParams(params, callback) {
//#check quotation mark
params.quotes = typeof params.quotes === 'string' ? params.quotes : '"';

//#check nested option
params.nested = params.nested || false;

//#check default value
params.defaultValue = params.defaultValue;

Expand Down Expand Up @@ -150,13 +147,7 @@ function createColumnContent(params, str) {
var eol = params.newLine || os.EOL || '\n';

params.fields.forEach(function (fieldElement) {
var val;

if (params.nested) {
val = get(dataElement, fieldElement, '');
} else {
val = dataElement[fieldElement];
}
var val = lodashGet(dataElement, fieldElement, params.defaultValue);

if (val !== undefined) {
var stringifiedElement = JSON.stringify(val);
Expand All @@ -166,9 +157,6 @@ function createColumnContent(params, str) {
}

line += stringifiedElement;
} else if (params.defaultValue !== undefined) {
// Use default value if defined
line += JSON.stringify(params.defaultValue);
}

line += params.del;
Expand Down
3 changes: 1 addition & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,7 @@ async.parallel(loadFixtures(csvFixtures), function (err) {
json2csv({
data: jsonNested,
fields: ['car.make', 'car.model', 'price', 'color', 'car.ye.ar'],
fieldNames: ['Make', 'Model', 'Price', 'Color', 'Year'],
nested: true
fieldNames: ['Make', 'Model', 'Price', 'Color', 'Year']
}, function (error, csv) {
t.error(error);
t.equal(csv, csvFixtures.nested);
Expand Down