@json2csv/plainjs
Advanced tools
Comparing version 6.1.2 to 6.1.3
@@ -20,2 +20,6 @@ var __create = Object.create; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
@@ -41,2 +45,8 @@ mod | ||
} | ||
/** | ||
* Check passing opts and set defaults. | ||
* | ||
* @param {Json2CsvOptions} opts Options object containing fields, | ||
* delimiter, default value, quote mark, header, etc. | ||
*/ | ||
preprocessOpts(opts) { | ||
@@ -75,2 +85,9 @@ const processedOpts = Object.assign({}, opts); | ||
} | ||
/** | ||
* Check and normalize the fields configuration. | ||
* | ||
* @param {(string|object)[]} fields Fields configuration provided by the user | ||
* or inferred from the data | ||
* @returns {object[]} preprocessed FieldsInfo array | ||
*/ | ||
preprocessFieldsInfo(fields, globalDefaultValue) { | ||
@@ -109,2 +126,7 @@ return fields.map((fieldInfo) => { | ||
} | ||
/** | ||
* Create the title row with all the provided fields as column headings | ||
* | ||
* @returns {String} titles as a string | ||
*/ | ||
getHeader() { | ||
@@ -118,2 +140,6 @@ return (0, import_utils.fastJoin)( | ||
} | ||
/** | ||
* Preprocess each object according to the given transforms (unwind, flatten, etc.). | ||
* @param {Object} row JSON object to be converted in a CSV row | ||
*/ | ||
preprocessRow(row) { | ||
@@ -125,2 +151,8 @@ return this.opts.transforms.reduce( | ||
} | ||
/** | ||
* Create the content of a specific CSV row | ||
* | ||
* @param {Object} row JSON object to be converted in a CSV row | ||
* @returns {String} CSV string (row) | ||
*/ | ||
processRow(row) { | ||
@@ -138,5 +170,18 @@ if (!row) { | ||
} | ||
/** | ||
* Create the content of a specfic CSV row cell | ||
* | ||
* @param {Object} row JSON object representing the CSV row that the cell belongs to | ||
* @param {FieldInfo} fieldInfo Details of the field to process to be a CSV cell | ||
* @returns {String} CSV string (cell) | ||
*/ | ||
processCell(row, fieldInfo) { | ||
return this.processValue(fieldInfo.value(row)); | ||
} | ||
/** | ||
* Create the content of a specfic CSV row cell | ||
* | ||
* @param {Any} value Value to be included in a CSV cell | ||
* @returns {String} Value stringified and processed | ||
*/ | ||
processValue(value) { | ||
@@ -146,3 +191,1 @@ return this.opts.formatters[typeof value](value); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = {}); |
@@ -20,2 +20,6 @@ var __create = Object.create; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
@@ -22,0 +26,0 @@ mod |
@@ -20,2 +20,6 @@ var __create = Object.create; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
@@ -36,2 +40,8 @@ mod | ||
} | ||
/** | ||
* Main function that converts json to csv. | ||
* | ||
* @param {Array|Object} data Array of JSON objects to be converted to CSV | ||
* @returns {String} The CSV formated data as a string | ||
*/ | ||
parse(data) { | ||
@@ -54,2 +64,8 @@ const preprocessedData = this.preprocessData(data); | ||
} | ||
/** | ||
* Preprocess the data according to the give opts (unwind, flatten, etc.) | ||
and calculate the fields and field names if they are not provided. | ||
* | ||
* @param {Array|Object} data Array or object to be converted to CSV | ||
*/ | ||
preprocessData(data) { | ||
@@ -66,5 +82,12 @@ const processedData = Array.isArray(data) ? data : [data]; | ||
} | ||
/** | ||
* Create the content row by row below the header | ||
* | ||
* @param {Array} data Array of JSON objects to be converted to CSV | ||
* @returns {String} CSV string (body) | ||
*/ | ||
processData(data) { | ||
return (0, import_utils.fastJoin)( | ||
data.map((row) => this.processRow(row)).filter((row) => row), | ||
// Filter empty rows | ||
this.opts.eol | ||
@@ -74,3 +97,1 @@ ); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = {}); |
@@ -20,2 +20,6 @@ var __create = Object.create; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
@@ -68,3 +72,3 @@ mod | ||
}; | ||
tokenParser.onValue = (value) => this.pushLine(value); | ||
tokenParser.onValue = ({ value }) => this.pushLine(value); | ||
tokenParser.onError = (err) => this.onError(err); | ||
@@ -88,3 +92,3 @@ tokenParser.onEnd = () => { | ||
const tokenizer = new import_json.Tokenizer(asyncOpts); | ||
tokenizer.onToken = (token, value, offset) => { | ||
tokenizer.onToken = ({ token, value, offset }) => { | ||
if (token === import_json.TokenType.LEFT_BRACKET) { | ||
@@ -102,3 +106,3 @@ this.tokenParser = new import_json.TokenParser({ | ||
this.configureCallbacks(tokenizer, this.tokenParser); | ||
this.tokenParser.write(token, value, offset); | ||
this.tokenParser.write({ token, value, offset }); | ||
}; | ||
@@ -136,2 +140,5 @@ tokenizer.onError = () => this.onError(new Error("Data should be a JSON object or array")); | ||
} | ||
/** | ||
* Generate the csv header and pushes it downstream. | ||
*/ | ||
pushHeader() { | ||
@@ -148,2 +155,7 @@ if (this.opts.withBOM) { | ||
} | ||
/** | ||
* Transforms an incoming json data to csv and pushes it downstream. | ||
* | ||
* @param {Object} data JSON object to be converted in a CSV row | ||
*/ | ||
pushLine(data) { | ||
@@ -166,2 +178,4 @@ const processedData = this.preprocessRow(data); | ||
} | ||
// No idea why eslint doesn't detect the usage of these | ||
/* eslint-disable no-unused-vars */ | ||
onHeader(header) { | ||
@@ -177,4 +191,3 @@ } | ||
} | ||
/* eslint-enable no-unused-vars */ | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = {}); |
{ | ||
"name": "@json2csv/plainjs", | ||
"version": "6.1.2", | ||
"version": "6.1.3", | ||
"description": "Pure Javascript JSON to CSV converter.", | ||
@@ -43,3 +43,3 @@ "keywords": [ | ||
"test:raw": "node test", | ||
"deprendencies:update-internal": "npm uninstall @json2csv/formatters && npm install @json2csv/formatters", | ||
"dependencies:update-internal": "npm uninstall @json2csv/formatters && npm install @json2csv/formatters", | ||
"build:cjs": "node ../../build-cjs.js plainjs", | ||
@@ -49,6 +49,6 @@ "prepublishOnly": "npm run build:cjs" | ||
"dependencies": { | ||
"@json2csv/formatters": "^6.1.2", | ||
"@streamparser/json": "^0.0.10", | ||
"@json2csv/formatters": "^6.1.3", | ||
"@streamparser/json": "^0.0.12", | ||
"lodash.get": "^4.4.2" | ||
} | ||
} |
@@ -44,3 +44,3 @@ import { Tokenizer, TokenParser, TokenType } from '@streamparser/json'; | ||
tokenParser.onValue = (value) => this.pushLine(value); | ||
tokenParser.onValue = ({ value }) => this.pushLine(value); | ||
tokenParser.onError = (err) => this.onError(err); | ||
@@ -66,3 +66,3 @@ tokenParser.onEnd = () => { | ||
const tokenizer = new Tokenizer(asyncOpts); | ||
tokenizer.onToken = (token, value, offset) => { | ||
tokenizer.onToken = ({ token, value, offset }) => { | ||
if (token === TokenType.LEFT_BRACKET) { | ||
@@ -82,3 +82,3 @@ this.tokenParser = new TokenParser({ | ||
this.tokenParser.write(token, value, offset); | ||
this.tokenParser.write({ token, value, offset }); | ||
}; | ||
@@ -85,0 +85,0 @@ tokenizer.onError = () => |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45521
963
+ Added@streamparser/json@0.0.12(transitive)
- Removed@streamparser/json@0.0.10(transitive)
Updated@json2csv/formatters@^6.1.3
Updated@streamparser/json@^0.0.12