Skip to content

Commit

Permalink
fix(parser): RangeError (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Mar 9, 2018
1 parent 3841ba4 commit c8d5a87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
49 changes: 24 additions & 25 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class JSON2CSVBase {
const processedRow = (this.opts.unwind && this.opts.unwind.length)
? this.unwindData(row, this.opts.unwind)
: [row];

if (this.opts.flatten) {
return processedRow.map(this.flatten);
}
Expand Down Expand Up @@ -231,32 +232,30 @@ class JSON2CSVBase {
* @returns {Array} Array of objects containing all rows after unwind of chosen paths
*/
unwindData(dataRow, unwindPaths) {
return Array.prototype.concat.apply([],
unwindPaths.reduce((data, unwindPath) =>
Array.prototype.concat.apply([],
data.map((dataEl) => {
const unwindArray = lodashGet(dataEl, unwindPath);

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

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

const dataCopy = lodashCloneDeep(dataEl);
lodashSet(dataCopy, unwindPath, undefined);
return dataCopy;
})
),
[dataRow]
)
return unwindPaths
.reduce((data, unwindPath) =>
data.map((dataEl) => {
const unwindArray = lodashGet(dataEl, unwindPath);

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

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

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

Expand Down
13 changes: 7 additions & 6 deletions lib/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class JSON2CSVParser extends JSON2CSVBase {
const processedData = this.preprocessData(data);

if (!this.opts.fields) {
const dataFields = Array.prototype.concat.apply([],
processedData.map(item => Object.keys(item))
);
const dataFields = processedData
.map(item => Object.keys(item))
.reduce((tempData, rows) => tempData.concat(rows), []);

this.opts.fields = dataFields
.filter((field, pos, arr) => arr.indexOf(field) == pos);
}
Expand Down Expand Up @@ -43,9 +44,9 @@ class JSON2CSVParser extends JSON2CSVBase {
throw new Error('Data should not be empty or the "fields" option should be included');
}

return Array.prototype.concat.apply([],
processedData.map(row => this.preprocessRow(row))
);
return processedData
.map(row => this.preprocessRow(row))
.reduce((tempData, rows) => tempData.concat(rows), []);
}

/**
Expand Down

0 comments on commit c8d5a87

Please sign in to comment.