convert-csv-to-json 4.2.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +3 -0
- package/.eslintrc.json +48 -0
- package/.github/workflows/ci-cd.yml +28 -2
- package/docs/api/BrowserApi.html +2435 -0
- package/docs/api/BrowserApiError.html +522 -0
- package/docs/api/ConfigurationError.html +594 -0
- package/docs/api/CsvFormatError.html +530 -0
- package/docs/api/CsvParsingError.html +384 -0
- package/docs/api/CsvToJson.html +3136 -0
- package/docs/api/CsvToJsonAsync.html +2672 -0
- package/docs/api/FileOperationError.html +270 -0
- package/docs/api/FileUtils.html +1012 -0
- package/docs/api/InputValidationError.html +293 -0
- package/docs/api/JsonUtil.html +340 -0
- package/docs/api/JsonValidationError.html +247 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.svg +1830 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
- package/docs/api/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.svg +1830 -0
- package/docs/api/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Light-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Light-webfont.svg +1831 -0
- package/docs/api/fonts/OpenSans-Light-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
- package/docs/api/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.svg +1831 -0
- package/docs/api/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/api/global.html +3315 -0
- package/docs/api/index.html +326 -0
- package/docs/api/index.js.html +341 -0
- package/docs/api/scripts/linenumber.js +25 -0
- package/docs/api/scripts/prettify/Apache-License-2.0.txt +202 -0
- package/docs/api/scripts/prettify/lang-css.js +2 -0
- package/docs/api/scripts/prettify/prettify.js +28 -0
- package/docs/api/src_browserApi.js.html +271 -0
- package/docs/api/src_csvToJson.js.html +605 -0
- package/docs/api/src_csvToJsonAsync.js.html +244 -0
- package/docs/api/src_util_errors.js.html +374 -0
- package/docs/api/src_util_fileUtils.js.html +147 -0
- package/docs/api/src_util_jsonUtils.js.html +75 -0
- package/docs/api/src_util_stringUtils.js.html +212 -0
- package/docs/api/styles/jsdoc-default.css +358 -0
- package/docs/api/styles/prettify-jsdoc.css +111 -0
- package/docs/api/styles/prettify-tomorrow.css +132 -0
- package/index.js +109 -32
- package/jsdoc.json +17 -0
- package/package.json +10 -3
- package/src/browserApi.js +96 -4
- package/src/csvToJson.js +163 -2
- package/src/csvToJsonAsync.js +74 -14
- package/src/util/errors.js +96 -0
- package/src/util/fileUtils.js +34 -0
- package/src/util/jsonUtils.js +8 -0
- package/src/util/stringUtils.js +51 -0
package/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CsvToJson - CSV to JSON converter library
|
|
3
|
+
* Main entry point providing chainable API for CSV parsing with multiple configuration options
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/* globals FileOperationError, CsvFormatError, JsonValidationError, InputValidationError */
|
|
7
|
+
|
|
1
8
|
"use strict";
|
|
2
9
|
|
|
3
10
|
let csvToJson = require("./src/csvToJson.js");
|
|
@@ -13,7 +20,10 @@ const encodingOps = {
|
|
|
13
20
|
};
|
|
14
21
|
|
|
15
22
|
/**
|
|
16
|
-
*
|
|
23
|
+
* Enable or disable automatic type formatting for values
|
|
24
|
+
* Converts numeric strings to numbers, 'true'/'false' to booleans
|
|
25
|
+
* @param {boolean} active - Whether to format values by type (default: true)
|
|
26
|
+
* @returns {object} Module context for method chaining
|
|
17
27
|
*/
|
|
18
28
|
exports.formatValueByType = function (active = true) {
|
|
19
29
|
csvToJson.formatValueByType(active);
|
|
@@ -21,14 +31,19 @@ exports.formatValueByType = function (active = true) {
|
|
|
21
31
|
};
|
|
22
32
|
|
|
23
33
|
/**
|
|
24
|
-
*
|
|
34
|
+
* Enable or disable support for RFC 4180 quoted fields
|
|
35
|
+
* When enabled, fields wrapped in double quotes can contain delimiters and newlines
|
|
36
|
+
* @param {boolean} active - Whether to support quoted fields (default: false)
|
|
37
|
+
* @returns {object} Module context for method chaining
|
|
25
38
|
*/
|
|
26
39
|
exports.supportQuotedField = function (active = false) {
|
|
27
40
|
csvToJson.supportQuotedField(active);
|
|
28
41
|
return this;
|
|
29
42
|
};
|
|
30
43
|
/**
|
|
31
|
-
*
|
|
44
|
+
* Set the field delimiter character used to separate CSV fields
|
|
45
|
+
* @param {string} delimiter - Character(s) to use as field separator (default: ',')
|
|
46
|
+
* @returns {object} Module context for method chaining
|
|
32
47
|
*/
|
|
33
48
|
exports.fieldDelimiter = function (delimiter) {
|
|
34
49
|
csvToJson.fieldDelimiter(delimiter);
|
|
@@ -36,7 +51,11 @@ exports.fieldDelimiter = function (delimiter) {
|
|
|
36
51
|
};
|
|
37
52
|
|
|
38
53
|
/**
|
|
39
|
-
*
|
|
54
|
+
* Configure whitespace handling in CSV header field names
|
|
55
|
+
* When active, removes all whitespace from header names (e.g., "My Name" → "MyName")
|
|
56
|
+
* When inactive, only trims leading and trailing whitespace
|
|
57
|
+
* @param {boolean} active - Whether to remove all whitespace from headers (default: false)
|
|
58
|
+
* @returns {object} Module context for method chaining
|
|
40
59
|
*/
|
|
41
60
|
exports.trimHeaderFieldWhiteSpace = function (active = false) {
|
|
42
61
|
csvToJson.trimHeaderFieldWhiteSpace(active);
|
|
@@ -44,7 +63,10 @@ exports.trimHeaderFieldWhiteSpace = function (active = false) {
|
|
|
44
63
|
};
|
|
45
64
|
|
|
46
65
|
/**
|
|
47
|
-
*
|
|
66
|
+
* Set the row index where CSV headers are located
|
|
67
|
+
* Use this if headers are not on the first line (row 0)
|
|
68
|
+
* @param {number} index - Zero-based row index containing headers
|
|
69
|
+
* @returns {object} Module context for method chaining
|
|
48
70
|
*/
|
|
49
71
|
exports.indexHeader = function (index) {
|
|
50
72
|
csvToJson.indexHeader(index);
|
|
@@ -52,7 +74,15 @@ exports.indexHeader = function (index) {
|
|
|
52
74
|
};
|
|
53
75
|
|
|
54
76
|
/**
|
|
55
|
-
*
|
|
77
|
+
* Configure sub-array parsing for special field values
|
|
78
|
+
* Fields bracketed by delimiter and containing separator are parsed into arrays
|
|
79
|
+
* @param {string} delimiter - Bracket character (default: '*')
|
|
80
|
+
* @param {string} separator - Item separator within brackets (default: ',')
|
|
81
|
+
* @returns {object} Module context for method chaining
|
|
82
|
+
* @example
|
|
83
|
+
* // Input field: "*val1,val2,val3*"
|
|
84
|
+
* // Output array: ["val1", "val2", "val3"]
|
|
85
|
+
* csvToJson.parseSubArray('*', ',')
|
|
56
86
|
*/
|
|
57
87
|
exports.parseSubArray = function (delimiter, separator) {
|
|
58
88
|
csvToJson.parseSubArray(delimiter, separator);
|
|
@@ -60,7 +90,10 @@ exports.parseSubArray = function (delimiter, separator) {
|
|
|
60
90
|
};
|
|
61
91
|
|
|
62
92
|
/**
|
|
63
|
-
*
|
|
93
|
+
* Set custom file encoding for reading CSV files
|
|
94
|
+
* Useful for non-UTF8 encoded files
|
|
95
|
+
* @param {string} encoding - Node.js supported encoding (e.g., 'utf8', 'latin1', 'ascii')
|
|
96
|
+
* @returns {object} Module context for method chaining
|
|
64
97
|
*/
|
|
65
98
|
exports.customEncoding = function (encoding) {
|
|
66
99
|
csvToJson.encoding = encoding;
|
|
@@ -68,7 +101,8 @@ exports.customEncoding = function (encoding) {
|
|
|
68
101
|
};
|
|
69
102
|
|
|
70
103
|
/**
|
|
71
|
-
*
|
|
104
|
+
* Set UTF-8 encoding (default encoding)
|
|
105
|
+
* @returns {object} Module context for method chaining
|
|
72
106
|
*/
|
|
73
107
|
exports.utf8Encoding = function utf8Encoding() {
|
|
74
108
|
csvToJson.encoding = encodingOps.utf8;
|
|
@@ -76,7 +110,8 @@ exports.utf8Encoding = function utf8Encoding() {
|
|
|
76
110
|
};
|
|
77
111
|
|
|
78
112
|
/**
|
|
79
|
-
*
|
|
113
|
+
* Set UCS-2 encoding for reading files
|
|
114
|
+
* @returns {object} Module context for method chaining
|
|
80
115
|
*/
|
|
81
116
|
exports.ucs2Encoding = function () {
|
|
82
117
|
csvToJson.encoding = encodingOps.ucs2;
|
|
@@ -84,7 +119,8 @@ exports.ucs2Encoding = function () {
|
|
|
84
119
|
};
|
|
85
120
|
|
|
86
121
|
/**
|
|
87
|
-
*
|
|
122
|
+
* Set UTF-16 LE encoding for reading files
|
|
123
|
+
* @returns {object} Module context for method chaining
|
|
88
124
|
*/
|
|
89
125
|
exports.utf16leEncoding = function () {
|
|
90
126
|
csvToJson.encoding = encodingOps.utf16le;
|
|
@@ -92,7 +128,8 @@ exports.utf16leEncoding = function () {
|
|
|
92
128
|
};
|
|
93
129
|
|
|
94
130
|
/**
|
|
95
|
-
*
|
|
131
|
+
* Set Latin-1 (ISO-8859-1) encoding for reading files
|
|
132
|
+
* @returns {object} Module context for method chaining
|
|
96
133
|
*/
|
|
97
134
|
exports.latin1Encoding = function () {
|
|
98
135
|
csvToJson.encoding = encodingOps.latin1;
|
|
@@ -100,7 +137,8 @@ exports.latin1Encoding = function () {
|
|
|
100
137
|
};
|
|
101
138
|
|
|
102
139
|
/**
|
|
103
|
-
*
|
|
140
|
+
* Set ASCII encoding for reading files
|
|
141
|
+
* @returns {object} Module context for method chaining
|
|
104
142
|
*/
|
|
105
143
|
exports.asciiEncoding = function () {
|
|
106
144
|
csvToJson.encoding = encodingOps.ascii;
|
|
@@ -108,7 +146,8 @@ exports.asciiEncoding = function () {
|
|
|
108
146
|
};
|
|
109
147
|
|
|
110
148
|
/**
|
|
111
|
-
*
|
|
149
|
+
* Set Base64 encoding for reading files
|
|
150
|
+
* @returns {object} Module context for method chaining
|
|
112
151
|
*/
|
|
113
152
|
exports.base64Encoding = function () {
|
|
114
153
|
this.csvToJson = encodingOps.base64;
|
|
@@ -116,7 +155,8 @@ exports.base64Encoding = function () {
|
|
|
116
155
|
};
|
|
117
156
|
|
|
118
157
|
/**
|
|
119
|
-
*
|
|
158
|
+
* Set Hex encoding for reading files
|
|
159
|
+
* @returns {object} Module context for method chaining
|
|
120
160
|
*/
|
|
121
161
|
exports.hexEncoding = function () {
|
|
122
162
|
this.csvToJson = encodingOps.hex;
|
|
@@ -124,11 +164,15 @@ exports.hexEncoding = function () {
|
|
|
124
164
|
};
|
|
125
165
|
|
|
126
166
|
/**
|
|
127
|
-
*
|
|
167
|
+
* Set a mapper function to transform each row after conversion
|
|
128
168
|
* The mapper function receives (row, index) where row is the JSON object
|
|
129
169
|
* and index is the 0-based row number. Return null/undefined to filter out rows.
|
|
130
|
-
* @param {Function} mapperFn - Function to transform each row
|
|
131
|
-
* @
|
|
170
|
+
* @param {Function} mapperFn - Function to transform each row: `(row, index) => transformedRow | null`
|
|
171
|
+
* @returns {object} Module context for method chaining
|
|
172
|
+
* @example
|
|
173
|
+
* csvToJson
|
|
174
|
+
* .mapRows((row, idx) => idx % 2 === 0 ? row : null) // Keep every other row
|
|
175
|
+
* .getJsonFromCsv('input.csv')
|
|
132
176
|
*/
|
|
133
177
|
exports.mapRows = function (mapperFn) {
|
|
134
178
|
csvToJson.mapRows(mapperFn);
|
|
@@ -136,10 +180,15 @@ exports.mapRows = function (mapperFn) {
|
|
|
136
180
|
};
|
|
137
181
|
|
|
138
182
|
/**
|
|
139
|
-
*
|
|
140
|
-
* @param {
|
|
141
|
-
* @param {
|
|
142
|
-
*
|
|
183
|
+
* Parse CSV file and write the parsed JSON to an output file (synchronous)
|
|
184
|
+
* @param {string} inputFileName - Path to input CSV file
|
|
185
|
+
* @param {string} outputFileName - Path to output JSON file
|
|
186
|
+
* @throws {Error} If inputFileName or outputFileName is not defined
|
|
187
|
+
* @throws {FileOperationError} If file operations fail
|
|
188
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
189
|
+
* @example
|
|
190
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
191
|
+
* csvToJson.generateJsonFileFromCsv('input.csv', 'output.json');
|
|
143
192
|
*/
|
|
144
193
|
exports.generateJsonFileFromCsv = function(inputFileName, outputFileName) {
|
|
145
194
|
if (!inputFileName) {
|
|
@@ -152,10 +201,16 @@ exports.generateJsonFileFromCsv = function(inputFileName, outputFileName) {
|
|
|
152
201
|
};
|
|
153
202
|
|
|
154
203
|
/**
|
|
155
|
-
*
|
|
156
|
-
* @param {
|
|
157
|
-
* @
|
|
158
|
-
*
|
|
204
|
+
* Parse CSV file and return parsed data as JSON array of objects (synchronous)
|
|
205
|
+
* @param {string} inputFileName - Path to input CSV file
|
|
206
|
+
* @returns {Array<object>} Array of objects representing CSV rows
|
|
207
|
+
* @throws {Error} If inputFileName is not defined
|
|
208
|
+
* @throws {FileOperationError} If file read fails
|
|
209
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
210
|
+
* @example
|
|
211
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
212
|
+
* const rows = csvToJson.getJsonFromCsv('resource/input.csv');
|
|
213
|
+
* console.log(rows);
|
|
159
214
|
*/
|
|
160
215
|
exports.getJsonFromCsv = function(inputFileName) {
|
|
161
216
|
if (!inputFileName) {
|
|
@@ -165,10 +220,18 @@ exports.getJsonFromCsv = function(inputFileName) {
|
|
|
165
220
|
};
|
|
166
221
|
|
|
167
222
|
/**
|
|
168
|
-
*
|
|
169
|
-
* @param {string} inputFileNameOrCsv
|
|
170
|
-
* @param {object} options
|
|
171
|
-
* @
|
|
223
|
+
* Parse CSV file asynchronously and return parsed data as JSON array
|
|
224
|
+
* @param {string} inputFileNameOrCsv - Path to file or CSV string
|
|
225
|
+
* @param {object} options - Configuration options
|
|
226
|
+
* @param {boolean} options.raw - If true, treats first param as CSV content; if false, reads from file
|
|
227
|
+
* @returns {Promise<Array<object>>} Promise resolving to array of objects
|
|
228
|
+
* @throws {InputValidationError} If input is invalid
|
|
229
|
+
* @throws {FileOperationError} If file read fails
|
|
230
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
231
|
+
* @example
|
|
232
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
233
|
+
* const data = await csvToJson.getJsonFromCsvAsync('resource/input.csv');
|
|
234
|
+
* console.log(data);
|
|
172
235
|
*/
|
|
173
236
|
const csvToJsonAsync = require('./src/csvToJsonAsync');
|
|
174
237
|
|
|
@@ -188,14 +251,28 @@ Object.assign(exports, {
|
|
|
188
251
|
}
|
|
189
252
|
});
|
|
190
253
|
|
|
254
|
+
/**
|
|
255
|
+
* Parse a CSV string and return as JSON array of objects (synchronous)
|
|
256
|
+
* @param {string} csvString - CSV content as string
|
|
257
|
+
* @returns {Array<object>} Array of objects representing CSV rows
|
|
258
|
+
* @throws {InputValidationError} If csvString is invalid
|
|
259
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
260
|
+
* @example
|
|
261
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
262
|
+
* const rows = csvToJson.csvStringToJson('name,age\nAlice,30');
|
|
263
|
+
* console.log(rows); // [{ name: 'Alice', age: '30' }]
|
|
264
|
+
*/
|
|
191
265
|
exports.csvStringToJson = function(csvString) {
|
|
192
266
|
return csvToJson.csvStringToJson(csvString);
|
|
193
267
|
};
|
|
194
268
|
|
|
195
269
|
/**
|
|
196
|
-
*
|
|
197
|
-
* @param {
|
|
198
|
-
* @
|
|
270
|
+
* Parse CSV string and return as stringified JSON (synchronous)
|
|
271
|
+
* @param {string} csvString - CSV content as string
|
|
272
|
+
* @returns {string} JSON stringified array of objects
|
|
273
|
+
* @throws {InputValidationError} If csvString is invalid
|
|
274
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
275
|
+
* @throws {JsonValidationError} If JSON generation fails
|
|
199
276
|
*/
|
|
200
277
|
exports.csvStringToJsonStringified = function(csvString) {
|
|
201
278
|
if (csvString === undefined || csvString === null) {
|
package/jsdoc.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"source": {
|
|
3
|
+
"include": ["src", "index.js"],
|
|
4
|
+
"includePattern": ".+\\.js(doc|x)?$",
|
|
5
|
+
"excludePattern": "(^|/)node_modules(/|$)"
|
|
6
|
+
},
|
|
7
|
+
"opts": {
|
|
8
|
+
"destination": "./docs/jsdoc",
|
|
9
|
+
"recurse": true,
|
|
10
|
+
"readme": "./README.md"
|
|
11
|
+
},
|
|
12
|
+
"plugins": ["plugins/markdown"],
|
|
13
|
+
"templates": {
|
|
14
|
+
"cleverLinks": false,
|
|
15
|
+
"monospaceLinks": false
|
|
16
|
+
}
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "convert-csv-to-json",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"description": "Convert CSV to JSON",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "jest",
|
|
9
9
|
"test-debug": "node --inspect-brk node_modules/.bin/jest --runInBand --detectOpenHandles",
|
|
10
|
+
"lint": "eslint .",
|
|
11
|
+
"docs": "jsdoc -c jsdoc.json",
|
|
12
|
+
"docs:api": "jsdoc -c jsdoc.json -d docs/api",
|
|
13
|
+
"prepublishOnly": "npm run docs:api",
|
|
10
14
|
"version-patch": "npm version patch",
|
|
11
15
|
"version-minor": "npm version minor",
|
|
12
16
|
"version-major": "npm version major",
|
|
@@ -45,10 +49,13 @@
|
|
|
45
49
|
},
|
|
46
50
|
"homepage": "https://github.com/iuccio/CSVtoJSON#readme",
|
|
47
51
|
"devDependencies": {
|
|
52
|
+
"@types/jest": "^30.0.0",
|
|
53
|
+
"eslint": "^8.57.0",
|
|
54
|
+
"eslint-plugin-jsdoc": "^44.1.0",
|
|
48
55
|
"jest": "^30.2.0",
|
|
56
|
+
"jsdoc": "^4.0.5",
|
|
49
57
|
"ts-jest": "^29.4.5",
|
|
50
|
-
"typescript": "^5.9.3"
|
|
51
|
-
"@types/jest": "^30.0.0"
|
|
58
|
+
"typescript": "^5.9.3"
|
|
52
59
|
},
|
|
53
60
|
"publishConfig": {
|
|
54
61
|
"provenance": true,
|
package/src/browserApi.js
CHANGED
|
@@ -1,51 +1,106 @@
|
|
|
1
|
+
/* globals CsvFormatError */
|
|
2
|
+
|
|
1
3
|
"use strict";
|
|
2
4
|
|
|
3
5
|
const csvToJson = require('./csvToJson');
|
|
4
6
|
const { InputValidationError, BrowserApiError } = require('./util/errors');
|
|
5
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Browser-friendly CSV to JSON API
|
|
10
|
+
* Provides methods for parsing CSV strings and File/Blob objects in browser environments
|
|
11
|
+
* Proxies configuration to sync csvToJson instance
|
|
12
|
+
*/
|
|
6
13
|
class BrowserApi {
|
|
14
|
+
/**
|
|
15
|
+
* Constructor initializes proxy to sync csvToJson instance
|
|
16
|
+
*/
|
|
7
17
|
constructor() {
|
|
8
18
|
// reuse the existing csvToJson instance for parsing and configuration
|
|
9
19
|
this.csvToJson = csvToJson;
|
|
10
20
|
}
|
|
11
21
|
|
|
12
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Enable or disable automatic type formatting for values
|
|
24
|
+
* @param {boolean} active - Whether to format values by type (default: true)
|
|
25
|
+
* @returns {this} For method chaining
|
|
26
|
+
*/
|
|
13
27
|
formatValueByType(active = true) {
|
|
14
28
|
this.csvToJson.formatValueByType(active);
|
|
15
29
|
return this;
|
|
16
30
|
}
|
|
17
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Enable or disable support for RFC 4180 quoted fields
|
|
34
|
+
* @param {boolean} active - Whether to support quoted fields (default: false)
|
|
35
|
+
* @returns {this} For method chaining
|
|
36
|
+
*/
|
|
18
37
|
supportQuotedField(active = false) {
|
|
19
38
|
this.csvToJson.supportQuotedField(active);
|
|
20
39
|
return this;
|
|
21
40
|
}
|
|
22
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Set the field delimiter character
|
|
44
|
+
* @param {string} delimiter - Character(s) to use as field separator
|
|
45
|
+
* @returns {this} For method chaining
|
|
46
|
+
*/
|
|
23
47
|
fieldDelimiter(delimiter) {
|
|
24
48
|
this.csvToJson.fieldDelimiter(delimiter);
|
|
25
49
|
return this;
|
|
26
50
|
}
|
|
27
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Configure whitespace handling in header field names
|
|
54
|
+
* @param {boolean} active - If true, removes all whitespace; if false, only trims edges (default: false)
|
|
55
|
+
* @returns {this} For method chaining
|
|
56
|
+
*/
|
|
28
57
|
trimHeaderFieldWhiteSpace(active = false) {
|
|
29
58
|
this.csvToJson.trimHeaderFieldWhiteSpace(active);
|
|
30
59
|
return this;
|
|
31
60
|
}
|
|
32
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Set the row index where CSV headers are located
|
|
64
|
+
* @param {number} index - Zero-based row index containing headers
|
|
65
|
+
* @returns {this} For method chaining
|
|
66
|
+
*/
|
|
33
67
|
indexHeader(index) {
|
|
34
68
|
this.csvToJson.indexHeader(index);
|
|
35
69
|
return this;
|
|
36
70
|
}
|
|
37
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Configure sub-array parsing for special field values
|
|
74
|
+
* @param {string} delimiter - Bracket character (default: '*')
|
|
75
|
+
* @param {string} separator - Item separator within brackets (default: ',')
|
|
76
|
+
* @returns {this} For method chaining
|
|
77
|
+
*/
|
|
38
78
|
parseSubArray(delimiter = '*', separator = ',') {
|
|
39
79
|
this.csvToJson.parseSubArray(delimiter, separator);
|
|
40
80
|
return this;
|
|
41
81
|
}
|
|
42
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Set a mapper function to transform each row after conversion
|
|
85
|
+
* @param {Function} mapperFn - Function receiving (row, index) that returns transformed row or null to filter
|
|
86
|
+
* @returns {this} For method chaining
|
|
87
|
+
*/
|
|
43
88
|
mapRows(mapperFn) {
|
|
44
89
|
this.csvToJson.mapRows(mapperFn);
|
|
45
90
|
return this;
|
|
46
91
|
}
|
|
47
92
|
|
|
48
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Parse a CSV string and return as JSON array of objects
|
|
95
|
+
* @param {string} csvString - CSV content as string
|
|
96
|
+
* @returns {Array<object>} Array of objects representing CSV rows
|
|
97
|
+
* @throws {InputValidationError} If csvString is invalid
|
|
98
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
99
|
+
* @example
|
|
100
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
101
|
+
* const rows = csvToJson.browser.csvStringToJson('name,age\nAlice,30');
|
|
102
|
+
* console.log(rows); // [{ name: 'Alice', age: '30' }]
|
|
103
|
+
*/
|
|
49
104
|
csvStringToJson(csvString) {
|
|
50
105
|
if (csvString === undefined || csvString === null) {
|
|
51
106
|
throw new InputValidationError(
|
|
@@ -58,6 +113,17 @@ class BrowserApi {
|
|
|
58
113
|
return this.csvToJson.csvToJson(csvString);
|
|
59
114
|
}
|
|
60
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Parse a CSV string and return as stringified JSON
|
|
118
|
+
* @param {string} csvString - CSV content as string
|
|
119
|
+
* @returns {string} JSON stringified array of objects
|
|
120
|
+
* @throws {InputValidationError} If csvString is invalid
|
|
121
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
122
|
+
* @example
|
|
123
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
124
|
+
* const jsonString = csvToJson.browser.csvStringToJsonStringified('name,age\nAlice,30');
|
|
125
|
+
* console.log(jsonString);
|
|
126
|
+
*/
|
|
61
127
|
csvStringToJsonStringified(csvString) {
|
|
62
128
|
if (csvString === undefined || csvString === null) {
|
|
63
129
|
throw new InputValidationError(
|
|
@@ -70,11 +136,32 @@ class BrowserApi {
|
|
|
70
136
|
return this.csvToJson.csvStringToJsonStringified(csvString);
|
|
71
137
|
}
|
|
72
138
|
|
|
73
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Parse a CSV string asynchronously (returns resolved Promise)
|
|
141
|
+
* @param {string} csvString - CSV content as string
|
|
142
|
+
* @returns {Promise<Array<object>>} Promise resolving to array of objects
|
|
143
|
+
* @throws {InputValidationError} If csvString is invalid
|
|
144
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
145
|
+
* @example
|
|
146
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
147
|
+
* const rows = await csvToJson.browser.csvStringToJsonAsync('name,age\nAlice,30');
|
|
148
|
+
* console.log(rows);
|
|
149
|
+
*/
|
|
74
150
|
csvStringToJsonAsync(csvString) {
|
|
75
151
|
return Promise.resolve(this.csvStringToJson(csvString));
|
|
76
152
|
}
|
|
77
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Parse a CSV string asynchronously and return as stringified JSON
|
|
156
|
+
* @param {string} csvString - CSV content as string
|
|
157
|
+
* @returns {Promise<string>} Promise resolving to JSON stringified array
|
|
158
|
+
* @throws {InputValidationError} If csvString is invalid
|
|
159
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
160
|
+
* @example
|
|
161
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
162
|
+
* const json = await csvToJson.browser.csvStringToJsonStringifiedAsync('name,age\nAlice,30');
|
|
163
|
+
* console.log(json);
|
|
164
|
+
*/
|
|
78
165
|
csvStringToJsonStringifiedAsync(csvString) {
|
|
79
166
|
return Promise.resolve(this.csvStringToJsonStringified(csvString));
|
|
80
167
|
}
|
|
@@ -83,7 +170,12 @@ class BrowserApi {
|
|
|
83
170
|
* Parse a browser File or Blob object to JSON array.
|
|
84
171
|
* @param {File|Blob} file - File or Blob to read as text
|
|
85
172
|
* @param {object} options - options: { encoding?: string }
|
|
86
|
-
* @returns {Promise<any[]>}
|
|
173
|
+
* @returns {Promise<any[]>} Promise resolving to parsed JSON rows
|
|
174
|
+
* @example
|
|
175
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
176
|
+
* const fileInput = document.querySelector('#csvfile').files[0];
|
|
177
|
+
* const rows = await csvToJson.browser.parseFile(fileInput);
|
|
178
|
+
* console.log(rows);
|
|
87
179
|
*/
|
|
88
180
|
parseFile(file, options = {}) {
|
|
89
181
|
if (!file) {
|