Skip to content

Commit c8d5a87

Browse files
juanjoDiazknownasilya
authored andcommitted
fix(parser): RangeError (#271)
1 parent 3841ba4 commit c8d5a87

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

lib/JSON2CSVBase.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class JSON2CSVBase {
6262
const processedRow = (this.opts.unwind && this.opts.unwind.length)
6363
? this.unwindData(row, this.opts.unwind)
6464
: [row];
65+
6566
if (this.opts.flatten) {
6667
return processedRow.map(this.flatten);
6768
}
@@ -231,32 +232,30 @@ class JSON2CSVBase {
231232
* @returns {Array} Array of objects containing all rows after unwind of chosen paths
232233
*/
233234
unwindData(dataRow, unwindPaths) {
234-
return Array.prototype.concat.apply([],
235-
unwindPaths.reduce((data, unwindPath) =>
236-
Array.prototype.concat.apply([],
237-
data.map((dataEl) => {
238-
const unwindArray = lodashGet(dataEl, unwindPath);
239-
240-
if (!Array.isArray(unwindArray)) {
241-
return dataEl;
242-
}
243-
244-
if (unwindArray.length) {
245-
return unwindArray.map((unwindEl) => {
246-
const dataCopy = lodashCloneDeep(dataEl);
247-
lodashSet(dataCopy, unwindPath, unwindEl);
248-
return dataCopy;
249-
});
250-
}
251-
252-
const dataCopy = lodashCloneDeep(dataEl);
253-
lodashSet(dataCopy, unwindPath, undefined);
254-
return dataCopy;
255-
})
256-
),
257-
[dataRow]
258-
)
235+
return unwindPaths
236+
.reduce((data, unwindPath) =>
237+
data.map((dataEl) => {
238+
const unwindArray = lodashGet(dataEl, unwindPath);
239+
240+
if (!Array.isArray(unwindArray)) {
241+
return dataEl;
242+
}
243+
244+
if (unwindArray.length) {
245+
return unwindArray.map((unwindEl) => {
246+
const dataCopy = lodashCloneDeep(dataEl);
247+
lodashSet(dataCopy, unwindPath, unwindEl);
248+
return dataCopy;
249+
});
250+
}
251+
252+
const dataCopy = lodashCloneDeep(dataEl);
253+
lodashSet(dataCopy, unwindPath, undefined);
254+
return dataCopy;
255+
}).reduce((tempData, rows) => tempData.concat(rows), []),
256+
[dataRow]
259257
)
258+
.reduce((tempData, rows) => tempData.concat(rows), []);
260259
}
261260
}
262261

lib/JSON2CSVParser.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ class JSON2CSVParser extends JSON2CSVBase {
1313
const processedData = this.preprocessData(data);
1414

1515
if (!this.opts.fields) {
16-
const dataFields = Array.prototype.concat.apply([],
17-
processedData.map(item => Object.keys(item))
18-
);
16+
const dataFields = processedData
17+
.map(item => Object.keys(item))
18+
.reduce((tempData, rows) => tempData.concat(rows), []);
19+
1920
this.opts.fields = dataFields
2021
.filter((field, pos, arr) => arr.indexOf(field) == pos);
2122
}
@@ -43,9 +44,9 @@ class JSON2CSVParser extends JSON2CSVBase {
4344
throw new Error('Data should not be empty or the "fields" option should be included');
4445
}
4546

46-
return Array.prototype.concat.apply([],
47-
processedData.map(row => this.preprocessRow(row))
48-
);
47+
return processedData
48+
.map(row => this.preprocessRow(row))
49+
.reduce((tempData, rows) => tempData.concat(rows), []);
4950
}
5051

5152
/**

0 commit comments

Comments
 (0)