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

Fix flatten issue with toJSON #259

Merged
merged 1 commit into from
Feb 25, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const os = require('os');
const lodashGet = require('lodash.get');
const lodashSet = require('lodash.set');
const lodashCloneDeep = require('lodash.clonedeep');
const flatten = require('flat');

class JSON2CSVBase {
constructor(opts) {
Expand Down Expand Up @@ -64,7 +63,7 @@ class JSON2CSVBase {
? this.unwindData(row, this.opts.unwind)
: [row];
if (this.opts.flatten) {
return processedRow.map(flatten);
return processedRow.map(this.flatten);
}

return processedRow;
Expand Down Expand Up @@ -192,10 +191,42 @@ class JSON2CSVBase {
return stringifiedValue;
}

/**
* Performs the flattening of a data row recursively
*
* @param {Object} dataRow Original JSON object
* @returns {Object} Flattened object
*/
flatten(dataRow) {
function step (obj, flatDataRow, currentPath) {
Object.keys(obj).forEach((key) => {
const value = obj[key];

const newPath = currentPath
? `${currentPath}.${key}`
: key;

if (typeof value !== 'object'
|| Array.isArray(value)
|| Object.prototype.toString.call(value.toJSON) === '[object Function]'
|| !Object.keys(value).length) {
flatDataRow[newPath] = value;
return;
}

step(value, flatDataRow, newPath);
});

return flatDataRow;
}

return step(dataRow, {});
}

/**
* Performs the unwind recursively in specified sequence
*
* @param {Array} dataRow Original JSON object
* @param {Object} dataRow Original JSON object
* @param {String[]} unwindPaths The paths as strings to be used to deconstruct the array
* @returns {Array} Array of objects containing all rows after unwind of chosen paths
*/
Expand Down
14 changes: 4 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
"dependencies": {
"cli-table2": "^0.2.0",
"commander": "^2.8.1",
"flat": "^4.0.0",
"jsonparse": "^1.3.1",
"lodash.clonedeep": "^4.5.0",
"lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.0",
"lodash.set": "^4.3.0"
},
Expand Down
12 changes: 12 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should support flattenning JSON with toJSON', (t) => {
const opts = {
flatten: true
};

const parser = new Json2csvParser(opts);
const csv = parser.parse(jsonFixtures.flattenToJSON);

t.equal(csv, csvFixtures.flattenToJSON);
t.end();
});

testRunner.add('should unwind and flatten an object in the right order', (t) => {
const opts = {
unwind: ['items'],
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/csv/flattenToJSON.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"hello.world","lorem.ipsum.dolor"
"good afternoon","good evening"
13 changes: 13 additions & 0 deletions test/fixtures/json/flattenToJSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
hello: {
world: {
again: 'good morning',
toJSON: () =>'good afternoon'
}
},
lorem: {
ipsum: {
dolor: 'good evening'
}
}
}