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

Avoid redundant deep cloning when unwinding. #286

Merged
merged 3 commits into from
Apr 16, 2018
Merged
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
61 changes: 32 additions & 29 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class JSON2CSVBase {
*/
processRow(row) {
if (!row
|| (Object.getOwnPropertyNames(row).length === 0
|| (Object.getOwnPropertyNames(row).length === 0
&& !this.opts.includeEmptyRows)) {
return undefined;
}
Expand Down Expand Up @@ -114,7 +114,7 @@ class JSON2CSVBase {
const defaultValue = typeof fieldInfo === 'object' && 'default' in fieldInfo
? fieldInfo.default
: this.opts.defaultValue;

let value;
if (fieldInfo) {
if (typeof fieldInfo === 'string') {
Expand Down Expand Up @@ -231,42 +231,45 @@ class JSON2CSVBase {
* @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
*/
*/
unwindData(dataRow, unwindPaths) {
return unwindPaths
.reduce((data, unwindPath) =>
data.map((dataEl) => {
const unwindArray = lodashGet(dataEl, unwindPath);
const unwind = (rows, unwindPath) => {
function clone(o) {
return unwindPath.indexOf('.') !== -1
? lodashCloneDeep(o)
: Object.assign({}, o);
}

return rows
.map(row => {
const unwindArray = lodashGet(row, unwindPath);

if (!Array.isArray(unwindArray)) {
return dataEl;
return row;
}

if (unwindArray.length) {
return unwindArray.map((unwindEl, index) => {
const dataCopy = lodashCloneDeep(dataEl);
if (!unwindArray.length) {
return lodashSet(clone(row), unwindPath, undefined);
}

if (this.opts.unwindBlank && index > 0) {
Object.keys(dataCopy).forEach(key => {
if (!unwindEl[key]) {
dataCopy[key] = null;
}
})
}
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));
}

lodashSet(dataCopy, unwindPath, unwindEl);
return dataCopy;
});
}
return lodashSet(clonedRow, unwindPath, unwindRow);
});
})
.reduce((a, e) => a.concat(e), []);
};

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

Expand Down