From c8d5a879a049b8741dab25c1b6cb99de5f973113 Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Fri, 9 Mar 2018 13:04:40 +0200 Subject: [PATCH] fix(parser): RangeError (#271) --- lib/JSON2CSVBase.js | 49 +++++++++++++++++++++---------------------- lib/JSON2CSVParser.js | 13 ++++++------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/lib/JSON2CSVBase.js b/lib/JSON2CSVBase.js index 33121bf6..881c389d 100644 --- a/lib/JSON2CSVBase.js +++ b/lib/JSON2CSVBase.js @@ -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); } @@ -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), []); } } diff --git a/lib/JSON2CSVParser.js b/lib/JSON2CSVParser.js index 3518c5ff..e8eae7e9 100644 --- a/lib/JSON2CSVParser.js +++ b/lib/JSON2CSVParser.js @@ -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); } @@ -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), []); } /**