convert-csv-to-json 4.13.0 → 4.15.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/.github/workflows/ci-cd.yml +1 -1
- package/eslint.config.js +115 -0
- package/index.js +21 -5
- package/jsdoc.json +29 -4
- package/package.json +5 -3
- package/src/browserApi.js +4 -3
- package/src/csvToJson.js +3 -2
- package/src/csvToJsonAsync.js +2 -1
- package/src/util/errors.js +10 -2
- package/src/util/fileUtils.js +1 -0
- package/src/util/jsonUtils.js +1 -0
- package/src/util/stringUtils.js +5 -1
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -48
- package/docs/api/BrowserApi.html +0 -2435
- package/docs/api/BrowserApiError.html +0 -522
- package/docs/api/ConfigurationError.html +0 -594
- package/docs/api/CsvFormatError.html +0 -530
- package/docs/api/CsvParsingError.html +0 -384
- package/docs/api/CsvToJson.html +0 -3136
- package/docs/api/CsvToJsonAsync.html +0 -2672
- package/docs/api/FileOperationError.html +0 -270
- package/docs/api/FileUtils.html +0 -1012
- package/docs/api/InputValidationError.html +0 -293
- package/docs/api/JsonUtil.html +0 -340
- package/docs/api/JsonValidationError.html +0 -247
- package/docs/api/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/api/fonts/OpenSans-Bold-webfont.svg +0 -1830
- 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 +0 -1830
- 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 +0 -1830
- 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 +0 -1831
- 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 +0 -1835
- 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 +0 -1831
- package/docs/api/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/api/global.html +0 -3315
- package/docs/api/index.html +0 -326
- package/docs/api/index.js.html +0 -341
- package/docs/api/scripts/linenumber.js +0 -25
- package/docs/api/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/docs/api/scripts/prettify/lang-css.js +0 -2
- package/docs/api/scripts/prettify/prettify.js +0 -28
- package/docs/api/src_browserApi.js.html +0 -271
- package/docs/api/src_csvToJson.js.html +0 -605
- package/docs/api/src_csvToJsonAsync.js.html +0 -244
- package/docs/api/src_util_errors.js.html +0 -374
- package/docs/api/src_util_fileUtils.js.html +0 -147
- package/docs/api/src_util_jsonUtils.js.html +0 -75
- package/docs/api/src_util_stringUtils.js.html +0 -212
- package/docs/api/styles/jsdoc-default.css +0 -358
- package/docs/api/styles/prettify-jsdoc.css +0 -111
- package/docs/api/styles/prettify-tomorrow.css +0 -132
package/eslint.config.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const js = require('@eslint/js');
|
|
2
|
+
const jsdocPlugin = require('eslint-plugin-jsdoc');
|
|
3
|
+
|
|
4
|
+
const jsdocRules = {
|
|
5
|
+
'jsdoc/check-alignment': 'error',
|
|
6
|
+
'jsdoc/check-param-names': 'error',
|
|
7
|
+
'jsdoc/check-tag-names': 'error',
|
|
8
|
+
'jsdoc/check-types': 'warn',
|
|
9
|
+
'jsdoc/no-undefined-types': 'warn',
|
|
10
|
+
'jsdoc/require-description': 'warn',
|
|
11
|
+
'jsdoc/require-param': 'error',
|
|
12
|
+
'jsdoc/require-returns': 'error',
|
|
13
|
+
'jsdoc/require-jsdoc': [
|
|
14
|
+
'warn',
|
|
15
|
+
{
|
|
16
|
+
require: {
|
|
17
|
+
FunctionDeclaration: true,
|
|
18
|
+
MethodDefinition: true,
|
|
19
|
+
ClassDeclaration: false,
|
|
20
|
+
ArrowFunctionExpression: false,
|
|
21
|
+
FunctionExpression: false,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const sharedGlobals = {
|
|
28
|
+
// Node.js globals
|
|
29
|
+
console: 'readonly',
|
|
30
|
+
process: 'readonly',
|
|
31
|
+
Buffer: 'readonly',
|
|
32
|
+
__dirname: 'readonly',
|
|
33
|
+
__filename: 'readonly',
|
|
34
|
+
global: 'readonly',
|
|
35
|
+
clearInterval: 'readonly',
|
|
36
|
+
clearTimeout: 'readonly',
|
|
37
|
+
setInterval: 'readonly',
|
|
38
|
+
setTimeout: 'readonly',
|
|
39
|
+
// Browser globals
|
|
40
|
+
window: 'readonly',
|
|
41
|
+
document: 'readonly',
|
|
42
|
+
navigator: 'readonly',
|
|
43
|
+
location: 'readonly',
|
|
44
|
+
fetch: 'readonly',
|
|
45
|
+
XMLHttpRequest: 'readonly',
|
|
46
|
+
Blob: 'readonly',
|
|
47
|
+
File: 'readonly',
|
|
48
|
+
FileReader: 'readonly',
|
|
49
|
+
// Jest globals
|
|
50
|
+
describe: 'readonly',
|
|
51
|
+
it: 'readonly',
|
|
52
|
+
expect: 'readonly',
|
|
53
|
+
beforeEach: 'readonly',
|
|
54
|
+
afterEach: 'readonly',
|
|
55
|
+
beforeAll: 'readonly',
|
|
56
|
+
afterAll: 'readonly',
|
|
57
|
+
test: 'readonly',
|
|
58
|
+
jest: 'readonly',
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
module.exports = [
|
|
62
|
+
{
|
|
63
|
+
ignores: ['node_modules', 'coverage', 'docs', '.eslintignore'],
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
files: ['**/*.js'],
|
|
67
|
+
languageOptions: {
|
|
68
|
+
ecmaVersion: 2024,
|
|
69
|
+
sourceType: 'commonjs',
|
|
70
|
+
globals: sharedGlobals,
|
|
71
|
+
},
|
|
72
|
+
plugins: {
|
|
73
|
+
jsdoc: jsdocPlugin,
|
|
74
|
+
},
|
|
75
|
+
settings: {
|
|
76
|
+
jsdoc: {
|
|
77
|
+
tagNamePreference: {
|
|
78
|
+
category: 'category'
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
rules: {
|
|
83
|
+
...js.configs.recommended.rules,
|
|
84
|
+
...jsdocPlugin.configs.recommended.rules,
|
|
85
|
+
'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
|
|
86
|
+
...jsdocRules,
|
|
87
|
+
'no-mixed-spaces-and-tabs': 'off',
|
|
88
|
+
'no-prototype-builtins': 'off',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
files: ['jest.config.js'],
|
|
93
|
+
languageOptions: {
|
|
94
|
+
ecmaVersion: 2024,
|
|
95
|
+
sourceType: 'commonjs',
|
|
96
|
+
globals: sharedGlobals,
|
|
97
|
+
},
|
|
98
|
+
rules: {
|
|
99
|
+
'jsdoc/require-jsdoc': 'off',
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
files: ['test/**/*.js', 'test/**/*.spec.js'],
|
|
104
|
+
languageOptions: {
|
|
105
|
+
ecmaVersion: 2024,
|
|
106
|
+
sourceType: 'commonjs',
|
|
107
|
+
globals: sharedGlobals,
|
|
108
|
+
},
|
|
109
|
+
rules: {
|
|
110
|
+
'jsdoc/require-jsdoc': 'off',
|
|
111
|
+
'no-unused-vars': 'off',
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
];
|
|
115
|
+
|
package/index.js
CHANGED
|
@@ -22,6 +22,7 @@ const encodingOps = {
|
|
|
22
22
|
/**
|
|
23
23
|
* Enable or disable automatic type formatting for values
|
|
24
24
|
* Converts numeric strings to numbers, 'true'/'false' to booleans
|
|
25
|
+
* @category 1-Core API
|
|
25
26
|
* @param {boolean} active - Whether to format values by type (default: true)
|
|
26
27
|
* @returns {object} Module context for method chaining
|
|
27
28
|
*/
|
|
@@ -33,6 +34,7 @@ exports.formatValueByType = function (active = true) {
|
|
|
33
34
|
/**
|
|
34
35
|
* Enable or disable support for RFC 4180 quoted fields
|
|
35
36
|
* When enabled, fields wrapped in double quotes can contain delimiters and newlines
|
|
37
|
+
* @category 1-Core API
|
|
36
38
|
* @param {boolean} active - Whether to support quoted fields (default: false)
|
|
37
39
|
* @returns {object} Module context for method chaining
|
|
38
40
|
*/
|
|
@@ -42,6 +44,7 @@ exports.supportQuotedField = function (active = false) {
|
|
|
42
44
|
};
|
|
43
45
|
/**
|
|
44
46
|
* Set the field delimiter character used to separate CSV fields
|
|
47
|
+
* @category 1-Core API
|
|
45
48
|
* @param {string} delimiter - Character(s) to use as field separator (default: ',')
|
|
46
49
|
* @returns {object} Module context for method chaining
|
|
47
50
|
*/
|
|
@@ -54,6 +57,7 @@ exports.fieldDelimiter = function (delimiter) {
|
|
|
54
57
|
* Configure whitespace handling in CSV header field names
|
|
55
58
|
* When active, removes all whitespace from header names (e.g., "My Name" → "MyName")
|
|
56
59
|
* When inactive, only trims leading and trailing whitespace
|
|
60
|
+
* @category 1-Core API
|
|
57
61
|
* @param {boolean} active - Whether to remove all whitespace from headers (default: false)
|
|
58
62
|
* @returns {object} Module context for method chaining
|
|
59
63
|
*/
|
|
@@ -65,6 +69,7 @@ exports.trimHeaderFieldWhiteSpace = function (active = false) {
|
|
|
65
69
|
/**
|
|
66
70
|
* Set the row index where CSV headers are located
|
|
67
71
|
* Use this if headers are not on the first line (row 0)
|
|
72
|
+
* @category 1-Core API
|
|
68
73
|
* @param {number} index - Zero-based row index containing headers
|
|
69
74
|
* @returns {object} Module context for method chaining
|
|
70
75
|
*/
|
|
@@ -76,6 +81,7 @@ exports.indexHeader = function (index) {
|
|
|
76
81
|
/**
|
|
77
82
|
* Configure sub-array parsing for special field values
|
|
78
83
|
* Fields bracketed by delimiter and containing separator are parsed into arrays
|
|
84
|
+
* @category 1-Core API
|
|
79
85
|
* @param {string} delimiter - Bracket character (default: '*')
|
|
80
86
|
* @param {string} separator - Item separator within brackets (default: ',')
|
|
81
87
|
* @returns {object} Module context for method chaining
|
|
@@ -92,6 +98,7 @@ exports.parseSubArray = function (delimiter, separator) {
|
|
|
92
98
|
/**
|
|
93
99
|
* Set custom file encoding for reading CSV files
|
|
94
100
|
* Useful for non-UTF8 encoded files
|
|
101
|
+
* @category 1-Core API
|
|
95
102
|
* @param {string} encoding - Node.js supported encoding (e.g., 'utf8', 'latin1', 'ascii')
|
|
96
103
|
* @returns {object} Module context for method chaining
|
|
97
104
|
*/
|
|
@@ -102,6 +109,7 @@ exports.customEncoding = function (encoding) {
|
|
|
102
109
|
|
|
103
110
|
/**
|
|
104
111
|
* Set UTF-8 encoding (default encoding)
|
|
112
|
+
* @category 1-Core API
|
|
105
113
|
* @returns {object} Module context for method chaining
|
|
106
114
|
*/
|
|
107
115
|
exports.utf8Encoding = function utf8Encoding() {
|
|
@@ -111,6 +119,7 @@ exports.utf8Encoding = function utf8Encoding() {
|
|
|
111
119
|
|
|
112
120
|
/**
|
|
113
121
|
* Set UCS-2 encoding for reading files
|
|
122
|
+
* @category 1-Core API
|
|
114
123
|
* @returns {object} Module context for method chaining
|
|
115
124
|
*/
|
|
116
125
|
exports.ucs2Encoding = function () {
|
|
@@ -120,6 +129,7 @@ exports.ucs2Encoding = function () {
|
|
|
120
129
|
|
|
121
130
|
/**
|
|
122
131
|
* Set UTF-16 LE encoding for reading files
|
|
132
|
+
* @category 1-Core API
|
|
123
133
|
* @returns {object} Module context for method chaining
|
|
124
134
|
*/
|
|
125
135
|
exports.utf16leEncoding = function () {
|
|
@@ -129,6 +139,7 @@ exports.utf16leEncoding = function () {
|
|
|
129
139
|
|
|
130
140
|
/**
|
|
131
141
|
* Set Latin-1 (ISO-8859-1) encoding for reading files
|
|
142
|
+
* @category 1-Core API
|
|
132
143
|
* @returns {object} Module context for method chaining
|
|
133
144
|
*/
|
|
134
145
|
exports.latin1Encoding = function () {
|
|
@@ -138,6 +149,7 @@ exports.latin1Encoding = function () {
|
|
|
138
149
|
|
|
139
150
|
/**
|
|
140
151
|
* Set ASCII encoding for reading files
|
|
152
|
+
* @category 1-Core API
|
|
141
153
|
* @returns {object} Module context for method chaining
|
|
142
154
|
*/
|
|
143
155
|
exports.asciiEncoding = function () {
|
|
@@ -147,6 +159,7 @@ exports.asciiEncoding = function () {
|
|
|
147
159
|
|
|
148
160
|
/**
|
|
149
161
|
* Set Base64 encoding for reading files
|
|
162
|
+
* @category 1-Core API
|
|
150
163
|
* @returns {object} Module context for method chaining
|
|
151
164
|
*/
|
|
152
165
|
exports.base64Encoding = function () {
|
|
@@ -156,6 +169,7 @@ exports.base64Encoding = function () {
|
|
|
156
169
|
|
|
157
170
|
/**
|
|
158
171
|
* Set Hex encoding for reading files
|
|
172
|
+
* @category 1-Core API
|
|
159
173
|
* @returns {object} Module context for method chaining
|
|
160
174
|
*/
|
|
161
175
|
exports.hexEncoding = function () {
|
|
@@ -167,7 +181,8 @@ exports.hexEncoding = function () {
|
|
|
167
181
|
* Set a mapper function to transform each row after conversion
|
|
168
182
|
* The mapper function receives (row, index) where row is the JSON object
|
|
169
183
|
* and index is the 0-based row number. Return null/undefined to filter out rows.
|
|
170
|
-
* @
|
|
184
|
+
* @category 1-Core API
|
|
185
|
+
* @param {function(object, number): (object|null)} mapperFn - Function to transform each row
|
|
171
186
|
* @returns {object} Module context for method chaining
|
|
172
187
|
* @example
|
|
173
188
|
* csvToJson
|
|
@@ -186,6 +201,7 @@ exports.mapRows = function (mapperFn) {
|
|
|
186
201
|
* @throws {Error} If inputFileName or outputFileName is not defined
|
|
187
202
|
* @throws {FileOperationError} If file operations fail
|
|
188
203
|
* @throws {CsvFormatError} If CSV is malformed
|
|
204
|
+
* @category 1-Core API
|
|
189
205
|
* @example
|
|
190
206
|
* const csvToJson = require('convert-csv-to-json');
|
|
191
207
|
* csvToJson.generateJsonFileFromCsv('input.csv', 'output.json');
|
|
@@ -207,6 +223,7 @@ exports.generateJsonFileFromCsv = function(inputFileName, outputFileName) {
|
|
|
207
223
|
* @throws {Error} If inputFileName is not defined
|
|
208
224
|
* @throws {FileOperationError} If file read fails
|
|
209
225
|
* @throws {CsvFormatError} If CSV is malformed
|
|
226
|
+
* @category 1-Core API
|
|
210
227
|
* @example
|
|
211
228
|
* const csvToJson = require('convert-csv-to-json');
|
|
212
229
|
* const rows = csvToJson.getJsonFromCsv('resource/input.csv');
|
|
@@ -228,6 +245,7 @@ exports.getJsonFromCsv = function(inputFileName) {
|
|
|
228
245
|
* @throws {InputValidationError} If input is invalid
|
|
229
246
|
* @throws {FileOperationError} If file read fails
|
|
230
247
|
* @throws {CsvFormatError} If CSV is malformed
|
|
248
|
+
* @category 1-Core API
|
|
231
249
|
* @example
|
|
232
250
|
* const csvToJson = require('convert-csv-to-json');
|
|
233
251
|
* const data = await csvToJson.getJsonFromCsvAsync('resource/input.csv');
|
|
@@ -257,6 +275,7 @@ Object.assign(exports, {
|
|
|
257
275
|
* @returns {Array<object>} Array of objects representing CSV rows
|
|
258
276
|
* @throws {InputValidationError} If csvString is invalid
|
|
259
277
|
* @throws {CsvFormatError} If CSV is malformed
|
|
278
|
+
* @category 1-Core API
|
|
260
279
|
* @example
|
|
261
280
|
* const csvToJson = require('convert-csv-to-json');
|
|
262
281
|
* const rows = csvToJson.csvStringToJson('name,age\nAlice,30');
|
|
@@ -273,6 +292,7 @@ exports.csvStringToJson = function(csvString) {
|
|
|
273
292
|
* @throws {InputValidationError} If csvString is invalid
|
|
274
293
|
* @throws {CsvFormatError} If CSV is malformed
|
|
275
294
|
* @throws {JsonValidationError} If JSON generation fails
|
|
295
|
+
* @category 1-Core API
|
|
276
296
|
*/
|
|
277
297
|
exports.csvStringToJsonStringified = function(csvString) {
|
|
278
298
|
if (csvString === undefined || csvString === null) {
|
|
@@ -283,8 +303,4 @@ exports.csvStringToJsonStringified = function(csvString) {
|
|
|
283
303
|
|
|
284
304
|
|
|
285
305
|
|
|
286
|
-
/**
|
|
287
|
-
* Browser API
|
|
288
|
-
* Provides parsing helpers suitable for browser environments (parsing strings and File/Blob objects)
|
|
289
|
-
*/
|
|
290
306
|
exports.browser = require('./src/browserApi');
|
package/jsdoc.json
CHANGED
|
@@ -5,13 +5,38 @@
|
|
|
5
5
|
"excludePattern": "(^|/)node_modules(/|$)"
|
|
6
6
|
},
|
|
7
7
|
"opts": {
|
|
8
|
-
"destination": "./docs/
|
|
8
|
+
"destination": "./docs/api",
|
|
9
9
|
"recurse": true,
|
|
10
|
-
"readme": "./README.md"
|
|
10
|
+
"readme": "./README.md",
|
|
11
|
+
"template": "./node_modules/better-docs"
|
|
12
|
+
},
|
|
13
|
+
"plugins": ["plugins/markdown", "node_modules/better-docs/category"],
|
|
14
|
+
"tags": {
|
|
15
|
+
"allowUnknownTags": ["category"]
|
|
11
16
|
},
|
|
12
|
-
"plugins": ["plugins/markdown"],
|
|
13
17
|
"templates": {
|
|
14
18
|
"cleverLinks": false,
|
|
15
|
-
"monospaceLinks": false
|
|
19
|
+
"monospaceLinks": false,
|
|
20
|
+
"default": {
|
|
21
|
+
"outputSourceFiles": true,
|
|
22
|
+
"useLongNameInNav": true
|
|
23
|
+
},
|
|
24
|
+
"better-docs": {
|
|
25
|
+
"name": "Convert CSV to JSON",
|
|
26
|
+
"logo": "https://raw.githubusercontent.com/iuccio/CSVtoJSON/master/CSVtoJSON.png",
|
|
27
|
+
"css": "./node_modules/better-docs/css/better-docs.css",
|
|
28
|
+
"hideGenerator": false,
|
|
29
|
+
"trackingCode": "",
|
|
30
|
+
"navLinks": [
|
|
31
|
+
{
|
|
32
|
+
"label": "GitHub",
|
|
33
|
+
"href": "https://github.com/iuccio/CSVtoJSON"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"label": "NPM",
|
|
37
|
+
"href": "https://www.npmjs.com/package/convert-csv-to-json"
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
}
|
|
16
41
|
}
|
|
17
42
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "convert-csv-to-json",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.15.0",
|
|
4
4
|
"description": "Convert CSV to JSON",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -49,9 +49,11 @@
|
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://github.com/iuccio/CSVtoJSON#readme",
|
|
51
51
|
"devDependencies": {
|
|
52
|
+
"@eslint/js": "^10.0.1",
|
|
52
53
|
"@types/jest": "^30.0.0",
|
|
53
|
-
"
|
|
54
|
-
"eslint
|
|
54
|
+
"better-docs": "^2.7.3",
|
|
55
|
+
"eslint": "^10.1.0",
|
|
56
|
+
"eslint-plugin-jsdoc": "^62.8.0",
|
|
55
57
|
"jest": "^30.2.0",
|
|
56
58
|
"jsdoc": "^4.0.5",
|
|
57
59
|
"ts-jest": "^29.4.5",
|
package/src/browserApi.js
CHANGED
|
@@ -9,6 +9,7 @@ const { InputValidationError, BrowserApiError } = require('./util/errors');
|
|
|
9
9
|
* Browser-friendly CSV to JSON API
|
|
10
10
|
* Provides methods for parsing CSV strings and File/Blob objects in browser environments
|
|
11
11
|
* Proxies configuration to sync csvToJson instance
|
|
12
|
+
* @category 4-Browser
|
|
12
13
|
*/
|
|
13
14
|
class BrowserApi {
|
|
14
15
|
/**
|
|
@@ -82,7 +83,7 @@ class BrowserApi {
|
|
|
82
83
|
|
|
83
84
|
/**
|
|
84
85
|
* Set a mapper function to transform each row after conversion
|
|
85
|
-
* @param {
|
|
86
|
+
* @param {function(object, number): (object|null)} mapperFn - Function receiving (row, index) that returns transformed row or null to filter
|
|
86
87
|
* @returns {this} For method chaining
|
|
87
88
|
*/
|
|
88
89
|
mapRows(mapperFn) {
|
|
@@ -169,8 +170,8 @@ class BrowserApi {
|
|
|
169
170
|
/**
|
|
170
171
|
* Parse a browser File or Blob object to JSON array.
|
|
171
172
|
* @param {File|Blob} file - File or Blob to read as text
|
|
172
|
-
* @param {object} options - options: { encoding?: string }
|
|
173
|
-
* @returns {Promise<
|
|
173
|
+
* @param {object} [options] - options: { encoding?: string }
|
|
174
|
+
* @returns {Promise<object[]>} Promise resolving to parsed JSON rows
|
|
174
175
|
* @example
|
|
175
176
|
* const csvToJson = require('convert-csv-to-json');
|
|
176
177
|
* const fileInput = document.querySelector('#csvfile').files[0];
|
package/src/csvToJson.js
CHANGED
|
@@ -19,6 +19,7 @@ const CR = '\r';
|
|
|
19
19
|
/**
|
|
20
20
|
* Main CSV to JSON converter class
|
|
21
21
|
* Provides chainable API for configuring and converting CSV data
|
|
22
|
+
* @category 2-Sync
|
|
22
23
|
*/
|
|
23
24
|
class CsvToJson {
|
|
24
25
|
|
|
@@ -108,8 +109,8 @@ class CsvToJson {
|
|
|
108
109
|
|
|
109
110
|
/**
|
|
110
111
|
* Sets a mapper function to transform each row after conversion
|
|
111
|
-
* @param {
|
|
112
|
-
* @returns {this}
|
|
112
|
+
* @param {function(object, number): (object|null)} mapperFn - Function that receives (row, index) and returns transformed row or null to filter out
|
|
113
|
+
* @returns {this} For method chaining
|
|
113
114
|
*/
|
|
114
115
|
mapRows(mapperFn) {
|
|
115
116
|
if (typeof mapperFn !== 'function') {
|
package/src/csvToJsonAsync.js
CHANGED
|
@@ -8,6 +8,7 @@ const { InputValidationError } = require('./util/errors');
|
|
|
8
8
|
/**
|
|
9
9
|
* Asynchronous CSV to JSON converter
|
|
10
10
|
* Proxies configuration to sync instance but provides async file I/O methods
|
|
11
|
+
* @category 3-Async
|
|
11
12
|
*/
|
|
12
13
|
class CsvToJsonAsync {
|
|
13
14
|
/**
|
|
@@ -81,7 +82,7 @@ class CsvToJsonAsync {
|
|
|
81
82
|
|
|
82
83
|
/**
|
|
83
84
|
* Set a mapper function to transform each row after conversion
|
|
84
|
-
* @param {
|
|
85
|
+
* @param {function(object, number): (object|null)} mapperFn - Function receiving (row, index) that returns transformed row or null to filter
|
|
85
86
|
* @returns {this} For method chaining
|
|
86
87
|
*/
|
|
87
88
|
mapRows(mapperFn) {
|
package/src/util/errors.js
CHANGED
|
@@ -3,11 +3,13 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Custom error classes following clean code principles
|
|
5
5
|
* Provides clear, actionable error messages with context
|
|
6
|
+
* @category Error Classes
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Base class for all CSV parsing errors
|
|
10
11
|
* Provides consistent error formatting and context
|
|
12
|
+
* @category Error Classes
|
|
11
13
|
*/
|
|
12
14
|
class CsvParsingError extends Error {
|
|
13
15
|
/**
|
|
@@ -43,7 +45,7 @@ class CsvParsingError extends Error {
|
|
|
43
45
|
|
|
44
46
|
/**
|
|
45
47
|
* Format a context value for display in error message
|
|
46
|
-
* @param {
|
|
48
|
+
* @param {unknown} value - Value to format
|
|
47
49
|
* @returns {string} Formatted value string
|
|
48
50
|
* @private
|
|
49
51
|
*/
|
|
@@ -59,6 +61,7 @@ class CsvParsingError extends Error {
|
|
|
59
61
|
/**
|
|
60
62
|
* Input validation errors
|
|
61
63
|
* Thrown when function parameters don't meet expected type or value requirements
|
|
64
|
+
* @category Error Classes
|
|
62
65
|
*/
|
|
63
66
|
class InputValidationError extends CsvParsingError {
|
|
64
67
|
/**
|
|
@@ -86,6 +89,7 @@ class InputValidationError extends CsvParsingError {
|
|
|
86
89
|
/**
|
|
87
90
|
* Configuration-related errors
|
|
88
91
|
* Thrown when configuration options conflict or are invalid
|
|
92
|
+
* @category Error Classes
|
|
89
93
|
*/
|
|
90
94
|
class ConfigurationError extends CsvParsingError {
|
|
91
95
|
/**
|
|
@@ -121,7 +125,7 @@ class ConfigurationError extends CsvParsingError {
|
|
|
121
125
|
/**
|
|
122
126
|
* Create error for invalid header index
|
|
123
127
|
* Occurs when indexHeader() receives non-numeric value
|
|
124
|
-
* @param {
|
|
128
|
+
* @param {unknown} value - Invalid header index value
|
|
125
129
|
* @returns {ConfigurationError} Configured error instance
|
|
126
130
|
* @static
|
|
127
131
|
*/
|
|
@@ -141,6 +145,7 @@ class ConfigurationError extends CsvParsingError {
|
|
|
141
145
|
/**
|
|
142
146
|
* CSV parsing errors with detailed context
|
|
143
147
|
* Thrown when CSV format is invalid or malformed
|
|
148
|
+
* @category Error Classes
|
|
144
149
|
*/
|
|
145
150
|
class CsvFormatError extends CsvParsingError {
|
|
146
151
|
/**
|
|
@@ -200,6 +205,7 @@ class CsvFormatError extends CsvParsingError {
|
|
|
200
205
|
/**
|
|
201
206
|
* File operation errors
|
|
202
207
|
* Thrown when file read or write operations fail
|
|
208
|
+
* @category Error Classes
|
|
203
209
|
*/
|
|
204
210
|
class FileOperationError extends CsvParsingError {
|
|
205
211
|
/**
|
|
@@ -232,6 +238,7 @@ class FileOperationError extends CsvParsingError {
|
|
|
232
238
|
/**
|
|
233
239
|
* JSON validation errors
|
|
234
240
|
* Thrown when parsed CSV data cannot be converted to valid JSON
|
|
241
|
+
* @category Error Classes
|
|
235
242
|
*/
|
|
236
243
|
class JsonValidationError extends CsvParsingError {
|
|
237
244
|
/**
|
|
@@ -262,6 +269,7 @@ class JsonValidationError extends CsvParsingError {
|
|
|
262
269
|
/**
|
|
263
270
|
* Browser-specific errors
|
|
264
271
|
* Thrown when browser API operations fail
|
|
272
|
+
* @category Error Classes
|
|
265
273
|
*/
|
|
266
274
|
class BrowserApiError extends CsvParsingError {
|
|
267
275
|
/**
|
package/src/util/fileUtils.js
CHANGED
package/src/util/jsonUtils.js
CHANGED
package/src/util/stringUtils.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* String processing utilities for CSV parsing
|
|
5
|
+
* @category Utilities
|
|
6
|
+
*/
|
|
3
7
|
class StringUtils {
|
|
4
8
|
// Regular expressions as constants for better maintainability
|
|
5
9
|
static PATTERNS = {
|
|
@@ -66,7 +70,7 @@ class StringUtils {
|
|
|
66
70
|
// Private helper methods for type checking and conversion
|
|
67
71
|
/**
|
|
68
72
|
* Check if a value is empty (undefined or empty string)
|
|
69
|
-
* @param {
|
|
73
|
+
* @param {unknown} value - Value to check
|
|
70
74
|
* @returns {boolean} True if value is undefined or empty string
|
|
71
75
|
* @private
|
|
72
76
|
*/
|
package/.eslintignore
DELETED
package/.eslintrc.json
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"root": true,
|
|
3
|
-
"env": {
|
|
4
|
-
"node": true,
|
|
5
|
-
"browser": true,
|
|
6
|
-
"es2024": true,
|
|
7
|
-
"jest": true
|
|
8
|
-
},
|
|
9
|
-
"extends": [
|
|
10
|
-
"eslint:recommended",
|
|
11
|
-
"plugin:jsdoc/recommended"
|
|
12
|
-
],
|
|
13
|
-
"plugins": ["jsdoc"],
|
|
14
|
-
"overrides": [
|
|
15
|
-
{
|
|
16
|
-
"files": ["test/**/*.js", "test/**/*.spec.js"],
|
|
17
|
-
"rules": {
|
|
18
|
-
"jsdoc/require-jsdoc": "off",
|
|
19
|
-
"no-unused-vars": "off"
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
],
|
|
23
|
-
"rules": {
|
|
24
|
-
"no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
|
|
25
|
-
"jsdoc/check-alignment": "error",
|
|
26
|
-
"jsdoc/check-param-names": "error",
|
|
27
|
-
"jsdoc/check-tag-names": "error",
|
|
28
|
-
"jsdoc/check-types": "warn",
|
|
29
|
-
"jsdoc/no-undefined-types": "warn",
|
|
30
|
-
"jsdoc/require-description": "warn",
|
|
31
|
-
"jsdoc/require-param": "error",
|
|
32
|
-
"jsdoc/require-returns": "error",
|
|
33
|
-
"jsdoc/require-jsdoc": [
|
|
34
|
-
"warn",
|
|
35
|
-
{
|
|
36
|
-
"require": {
|
|
37
|
-
"FunctionDeclaration": true,
|
|
38
|
-
"MethodDefinition": true,
|
|
39
|
-
"ClassDeclaration": false,
|
|
40
|
-
"ArrowFunctionExpression": false,
|
|
41
|
-
"FunctionExpression": false
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
],
|
|
45
|
-
"no-mixed-spaces-and-tabs": "off",
|
|
46
|
-
"no-prototype-builtins": "off"
|
|
47
|
-
}
|
|
48
|
-
}
|