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

Clean up the flattening separator feature #315

Merged
merged 1 commit into from
Aug 1, 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
54 changes: 26 additions & 28 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class JSON2CSVBase {
: [row];

if (this.opts.flatten) {
return processedRow.map(this.flatten());
return processedRow.map(row => this.flatten(row, this.opts.flattenSeparator));
}

return processedRow;
Expand Down Expand Up @@ -209,37 +209,35 @@ class JSON2CSVBase {
/**
* Performs the flattening of a data row recursively
*
* @returns {Function} Function that receives dataRow as input and outputs flattened object
* @param {Object} dataRow Original JSON object
* @param {String} separator Separator to be used as the flattened field name
* @returns {Object} Flattened object
*/
flatten() {
return (dataRow) => {
const separator = this.opts.flattenSeparator;

function step (obj, flatDataRow, currentPath) {
Object.keys(obj).forEach((key) => {
const value = obj[key];

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

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

step(value, flatDataRow, newPath);
});
flatten(dataRow, separator) {
function step (obj, flatDataRow, currentPath) {
Object.keys(obj).forEach((key) => {
const value = obj[key];

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

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

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

return step(dataRow, {});
return flatDataRow;
}

return step(dataRow, {});
}

/**
Expand Down
13 changes: 12 additions & 1 deletion test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should support flattenning deep JSON', (t) => {
testRunner.add('should support flattening deep JSON', (t) => {
const opts = ' --flatten';

child_process.exec(cli + '-i ' + getFixturePath('/json/deepJSON.json') + opts, (err, stdout, stderr) => {
Expand All @@ -324,6 +324,17 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should support custom flatten separator', (t) => {
const opts = ' --flatten --flatten-separator __';

child_process.exec(cli + '-i ' + getFixturePath('/json/deepJSON.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.flattenedCustomSeparatorDeepJSON);
t.end();
});
});

testRunner.add('should unwind and flatten an object in the right order', (t) => {
const opts = ' --unwind items --flatten';

Expand Down
17 changes: 15 additions & 2 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});


testRunner.add('should support flattenning deep JSON', (t) => {
testRunner.add('should support flattening deep JSON', (t) => {
const opts = {
flatten: true
};
Expand All @@ -302,7 +302,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should support flattenning JSON with toJSON', (t) => {
testRunner.add('should support flattening JSON with toJSON', (t) => {
const opts = {
flatten: true
};
Expand All @@ -314,6 +314,19 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should support custom flatten separator', (t) => {
const opts = {
flatten: true,
flattenSeparator: '__',
};

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

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

testRunner.add('should unwind and flatten an object in the right order', (t) => {
const opts = {
unwind: ['items'],
Expand Down
2 changes: 1 addition & 1 deletion test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should support flattenning deep JSON', (t) => {
testRunner.add('should support flattening deep JSON', (t) => {
const opts = {
flatten: true
};
Expand Down