json2csv
Version:
Convert JSON to CSV
46 lines (39 loc) • 1.04 kB
JavaScript
;
function getProp(obj, path, defaultValue) {
return obj[path] === undefined ? defaultValue : obj[path];
}
function setProp(obj, path, value) {
const pathArray = Array.isArray(path) ? path : path.split('.');
const key = pathArray[0];
const newValue = pathArray.length > 1 ? setProp(obj[key] || {}, pathArray.slice(1), value) : value;
return Object.assign({}, obj, { [key]: newValue });
}
function flattenReducer(acc, arr) {
try {
// This is faster but susceptible to `RangeError: Maximum call stack size exceeded`
acc.push(...arr);
return acc;
} catch (err) {
// Fallback to a slower but safer option
return acc.concat(arr);
}
}
function fastJoin(arr, separator) {
let isFirst = true;
return arr.reduce((acc, elem) => {
if (elem === null || elem === undefined) {
elem = '';
}
if (isFirst) {
isFirst = false;
return `${elem}`;
}
return `${acc}${separator}${elem}`;
}, '');
}
module.exports = {
getProp,
setProp,
fastJoin,
flattenReducer
};