Skip to content

Commit

Permalink
fix(perf): Improve unwind performance and maintainability (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Apr 16, 2018
1 parent 9fe65b3 commit 80e496d
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,9 @@ class JSON2CSVBase {
*/
unwindData(dataRow, unwindPaths) {
const unwind = (rows, unwindPath) => {
function clone(o) {
return unwindPath.indexOf('.') !== -1
? lodashCloneDeep(o)
: Object.assign({}, o);
}
const clone = unwindPath.indexOf('.') !== -1
? o => lodashCloneDeep(o)
: o => Object.assign({}, o);

return rows
.map(row => {
Expand All @@ -253,15 +251,9 @@ class JSON2CSVBase {
}

return unwindArray.map((unwindRow, index) => {
const clonedRow = clone(row);
const shouldBlank = this.opts.unwindBlank && index > 0;

if (shouldBlank) {
const parentKeys = Object.keys(clonedRow).filter(
key => !unwindRow[key]
);
parentKeys.forEach(key => (clonedRow[key] = null));
}
const clonedRow = (this.opts.unwindBlank && index > 0)
? {}
: clone(row);

return lodashSet(clonedRow, unwindPath, unwindRow);
});
Expand Down

0 comments on commit 80e496d

Please sign in to comment.