Skip to content

Commit

Permalink
Avoid redundant deep cloning when unwinding.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarib committed Apr 16, 2018
1 parent 61d9808 commit 8ba4101
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,12 @@ class JSON2CSVBase {
*/
unwindData(dataRow, unwindPaths) {
return unwindPaths
.reduce((data, unwindPath) =>
data.map((dataEl) => {
.reduce((data, unwindPath) => {
function clone(o) {
return unwindPath.indexOf('.') !== -1 ? lodashCloneDeep(o) : Object.assign({}, o);
}

return data.map((dataEl) => {
const unwindArray = lodashGet(dataEl, unwindPath);

if (!Array.isArray(unwindArray)) {
Expand All @@ -244,7 +248,7 @@ class JSON2CSVBase {

if (unwindArray.length) {
return unwindArray.map((unwindEl, index) => {
const dataCopy = lodashCloneDeep(dataEl);
const dataCopy = clone(dataEl);

if (this.opts.unwindBlank && index > 0) {
Object.keys(dataCopy).forEach(key => {
Expand All @@ -254,16 +258,16 @@ class JSON2CSVBase {
})
}


lodashSet(dataCopy, unwindPath, unwindEl);
return dataCopy;
});
}

const dataCopy = lodashCloneDeep(dataEl);
const dataCopy = clone(dataEl);
lodashSet(dataCopy, unwindPath, undefined);
return dataCopy;
}).reduce((tempData, rows) => tempData.concat(rows), []),
}).reduce((tempData, rows) => tempData.concat(rows), [])
},
[dataRow]
)
.reduce((tempData, rows) => tempData.concat(rows), []);
Expand Down

0 comments on commit 8ba4101

Please sign in to comment.