Skip to content

Commit

Permalink
fix: Clean up the flattening separator feature (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Aug 1, 2018
1 parent 24d7654 commit ee3d181
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
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

0 comments on commit ee3d181

Please sign in to comment.