UNPKG

@iwsio/json-csv-core

Version:

Easily convert JSON to CSV. This is a core library built for browser and node to support buffered conversion to CSV.

146 lines (145 loc) 4.94 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.toCsv = void 0; exports.checkOptions = checkOptions; exports.buffered = buffered; exports.prepValue = prepValue; exports.getHeaderRow = getHeaderRow; exports.getBodyRow = getBodyRow; exports.getValue = getValue; exports.getValueIx = getValueIx; function checkOptions(opts) { var options = opts == null ? {} : __assign({}, opts); if (options.fieldSeparator == null) options.fieldSeparator = ','; if (options.ignoreHeader == null) options.ignoreHeader = false; if (options.fields == null) options.fields = []; return options; } /** * Main entry point. Convert a buffered array of data to a CSV string. */ function buffered(data, opts) { var options = checkOptions(opts); var output = ''; var writtenHeader = false; var ignoreHeader = options.ignoreHeader, fields = options.fields; data.forEach(function (dataItem) { if (!writtenHeader && !ignoreHeader) { writtenHeader = true; var header = getHeaderRow(fields, options.fieldSeparator); output += header; } var line = getBodyRow(dataItem, fields, options.fieldSeparator); output += line; }); return output; } function prepValue(text, forceQuoted, fieldSeparator) { if (text == null) text = ''; var quoted = forceQuoted || text.indexOf('"') >= 0 || text.indexOf(fieldSeparator) >= 0 || text.indexOf('\n') >= 0; var result = text.replace(/"/g, '""'); if (quoted) result = "\"".concat(result, "\""); return result; } function getHeaderRow(fields, fieldSeparator) { var header = ''; for (var ix = 0; ix < fields.length; ix++) { var field = fields[ix]; var label = field.label || field.name; if (ix > 0) { header += fieldSeparator; } header += prepValue(label, field.quoted, fieldSeparator); } header += '\r\n'; return header; } var assertString = function (value) { return typeof value === 'string'; }; function getBodyRow(data, fields, fieldSeparator) { var reducer = function (line, field) { if (line === 'START') { line = ''; } else { line += fieldSeparator; } var val = getValue(data, field.name); // support to OR || operator allowing multiples names to the same column // the code will use the last non null and non empty value if (field.name.includes('||')) { // by default column is empty val = ''; var fields_1 = field.name.split('||'); // for each alternative fields_1.forEach(function (field) { // get value and associate var fieldVal = getValue(data, field); // remove whitespaces and check if non null before assign if (val != null && assertString(fieldVal) && fieldVal.trim().length > 0 && fieldVal.trim() !== '') { val = fieldVal; } // do this for every field }); } if (typeof field.transform === 'function') { val = field.transform(val); } else if (typeof field.filter === 'function') { // backward compatibility val = field.filter(val); } if (typeof val !== 'undefined' && val !== null) { var quoted = typeof field.quoted !== 'undefined' && field.quoted; line += prepValue(val.toString(), quoted, fieldSeparator); } return line; }; var row = fields.reduce(reducer, 'START'); row += '\r\n'; return row; } function getValue(data, keyPath) { var keys = keyPath.split('.'); if (keys.length > 0) return getValueIx(data, keys, 0); return ''; } var assertObject = function (value) { if (typeof value === 'object') return true; return false; }; function getValueIx(data, keys, ix) { if (data == null) return ''; // for filtered fields using the whole row as a source. // `this` is a keyword here; hoping not to conflict with existing fields. if (keys[0] === 'this') return data; var val = data[keys[ix]]; if (val == null) return ''; // walk the dot-notation recursively to get the remaining values. if ((keys.length - 1) > ix && assertObject(val)) return getValueIx(val, keys, ix + 1); return val; } exports.toCsv = buffered; exports.default = buffered;