initial commit.
This commit is contained in:
37
node_modules/json2csv/lib/transforms/flatten.js
generated
vendored
Normal file
37
node_modules/json2csv/lib/transforms/flatten.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Performs the flattening of a data row recursively
|
||||
*
|
||||
* @param {String} separator Separator to be used as the flattened field name
|
||||
* @returns {Object => Object} Flattened object
|
||||
*/
|
||||
function flatten({ objects = true, arrays = false, separator = '.' } = {}) {
|
||||
function step (obj, flatDataRow, currentPath) {
|
||||
Object.keys(obj).forEach((key) => {
|
||||
const newPath = currentPath ? `${currentPath}${separator}${key}` : key;
|
||||
const value = obj[key];
|
||||
|
||||
if (objects
|
||||
&& typeof value === 'object'
|
||||
&& value !== null
|
||||
&& !Array.isArray(value)
|
||||
&& Object.prototype.toString.call(value.toJSON) !== '[object Function]'
|
||||
&& Object.keys(value).length) {
|
||||
step(value, flatDataRow, newPath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (arrays && Array.isArray(value)) {
|
||||
step(value, flatDataRow, newPath);
|
||||
return;
|
||||
}
|
||||
|
||||
flatDataRow[newPath] = value;
|
||||
});
|
||||
|
||||
return flatDataRow;
|
||||
}
|
||||
|
||||
return dataRow => step(dataRow, {});
|
||||
}
|
||||
|
||||
module.exports = flatten;
|
||||
63
node_modules/json2csv/lib/transforms/unwind.js
generated
vendored
Normal file
63
node_modules/json2csv/lib/transforms/unwind.js
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
const lodashGet = require('lodash.get');
|
||||
const { setProp, unsetProp, flattenReducer } = require('../utils');
|
||||
|
||||
function getUnwindablePaths(obj, currentPath) {
|
||||
return Object.keys(obj).reduce((unwindablePaths, key) => {
|
||||
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
||||
const value = obj[key];
|
||||
|
||||
if (typeof value === 'object'
|
||||
&& value !== null
|
||||
&& !Array.isArray(value)
|
||||
&& Object.prototype.toString.call(value.toJSON) !== '[object Function]'
|
||||
&& Object.keys(value).length) {
|
||||
unwindablePaths = unwindablePaths.concat(getUnwindablePaths(value, newPath));
|
||||
} else if (Array.isArray(value)) {
|
||||
unwindablePaths.push(newPath);
|
||||
unwindablePaths = unwindablePaths.concat(value
|
||||
.map(arrObj => getUnwindablePaths(arrObj, newPath))
|
||||
.reduce(flattenReducer, [])
|
||||
.filter((item, index, arr) => arr.indexOf(item) !== index));
|
||||
}
|
||||
|
||||
return unwindablePaths;
|
||||
}, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the unwind recursively in specified sequence
|
||||
*
|
||||
* @param {String[]} unwindPaths The paths as strings to be used to deconstruct the array
|
||||
* @returns {Object => Array} Array of objects containing all rows after unwind of chosen paths
|
||||
*/
|
||||
function unwind({ paths = undefined, blankOut = false } = {}) {
|
||||
function unwindReducer(rows, unwindPath) {
|
||||
return rows
|
||||
.map(row => {
|
||||
const unwindArray = lodashGet(row, unwindPath);
|
||||
|
||||
if (!Array.isArray(unwindArray)) {
|
||||
return row;
|
||||
}
|
||||
|
||||
if (!unwindArray.length) {
|
||||
return unsetProp(row, unwindPath);
|
||||
}
|
||||
|
||||
return unwindArray.map((unwindRow, index) => {
|
||||
const clonedRow = (blankOut && index > 0)
|
||||
? {}
|
||||
: row;
|
||||
|
||||
return setProp(clonedRow, unwindPath, unwindRow);
|
||||
});
|
||||
})
|
||||
.reduce(flattenReducer, []);
|
||||
}
|
||||
|
||||
paths = Array.isArray(paths) ? paths : (paths ? [paths] : undefined);
|
||||
return dataRow => (paths || getUnwindablePaths(dataRow)).reduce(unwindReducer, [dataRow]);
|
||||
}
|
||||
|
||||
module.exports = unwind;
|
||||
Reference in New Issue
Block a user