convert-csv-to-json 4.2.0 → 4.3.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
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>JSDoc: Source: src/csvToJsonAsync.js</title>
|
|
6
|
+
|
|
7
|
+
<script src="scripts/prettify/prettify.js"> </script>
|
|
8
|
+
<script src="scripts/prettify/lang-css.js"> </script>
|
|
9
|
+
<!--[if lt IE 9]>
|
|
10
|
+
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
11
|
+
<![endif]-->
|
|
12
|
+
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
13
|
+
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
14
|
+
</head>
|
|
15
|
+
|
|
16
|
+
<body>
|
|
17
|
+
|
|
18
|
+
<div id="main">
|
|
19
|
+
|
|
20
|
+
<h1 class="page-title">Source: src/csvToJsonAsync.js</h1>
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
<section>
|
|
28
|
+
<article>
|
|
29
|
+
<pre class="prettyprint source linenums"><code>/* globals CsvFormatError, FileOperationError */
|
|
30
|
+
'use strict';
|
|
31
|
+
|
|
32
|
+
const fileUtils = require('./util/fileUtils');
|
|
33
|
+
const csvToJson = require('./csvToJson');
|
|
34
|
+
const { InputValidationError } = require('./util/errors');
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Asynchronous CSV to JSON converter
|
|
38
|
+
* Proxies configuration to sync instance but provides async file I/O methods
|
|
39
|
+
*/
|
|
40
|
+
class CsvToJsonAsync {
|
|
41
|
+
/**
|
|
42
|
+
* Constructor initializes proxy to sync csvToJson instance
|
|
43
|
+
*/
|
|
44
|
+
constructor() {
|
|
45
|
+
// Proxy the configuration methods to the sync instance
|
|
46
|
+
this.csvToJson = csvToJson;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Enable or disable automatic type formatting for values
|
|
51
|
+
* @param {boolean} active - Whether to format values by type
|
|
52
|
+
* @returns {this} For method chaining
|
|
53
|
+
*/
|
|
54
|
+
formatValueByType(active) {
|
|
55
|
+
this.csvToJson.formatValueByType(active);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Enable or disable support for RFC 4180 quoted fields
|
|
61
|
+
* @param {boolean} active - Whether to support quoted fields
|
|
62
|
+
* @returns {this} For method chaining
|
|
63
|
+
*/
|
|
64
|
+
supportQuotedField(active) {
|
|
65
|
+
this.csvToJson.supportQuotedField(active);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Set the field delimiter character
|
|
71
|
+
* @param {string} delimiter - Character(s) to use as field separator
|
|
72
|
+
* @returns {this} For method chaining
|
|
73
|
+
*/
|
|
74
|
+
fieldDelimiter(delimiter) {
|
|
75
|
+
this.csvToJson.fieldDelimiter(delimiter);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Configure whitespace handling in header field names
|
|
81
|
+
* @param {boolean} active - If true, removes all whitespace; if false, only trims edges
|
|
82
|
+
* @returns {this} For method chaining
|
|
83
|
+
*/
|
|
84
|
+
trimHeaderFieldWhiteSpace(active) {
|
|
85
|
+
this.csvToJson.trimHeaderFieldWhiteSpace(active);
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Set the row index where CSV headers are located
|
|
91
|
+
* @param {number} indexHeader - Zero-based row index containing headers
|
|
92
|
+
* @returns {this} For method chaining
|
|
93
|
+
*/
|
|
94
|
+
indexHeader(indexHeader) {
|
|
95
|
+
this.csvToJson.indexHeader(indexHeader);
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Configure sub-array parsing for special field values
|
|
101
|
+
* @param {string} delimiter - Bracket character (default: '*')
|
|
102
|
+
* @param {string} separator - Item separator within brackets (default: ',')
|
|
103
|
+
* @returns {this} For method chaining
|
|
104
|
+
*/
|
|
105
|
+
parseSubArray(delimiter = '*', separator = ',') {
|
|
106
|
+
this.csvToJson.parseSubArray(delimiter, separator);
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Set a mapper function to transform each row after conversion
|
|
112
|
+
* @param {Function} mapperFn - Function receiving (row, index) that returns transformed row or null to filter
|
|
113
|
+
* @returns {this} For method chaining
|
|
114
|
+
*/
|
|
115
|
+
mapRows(mapperFn) {
|
|
116
|
+
this.csvToJson.mapRows(mapperFn);
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Set file encoding for reading CSV files
|
|
122
|
+
* @param {string} encoding - Node.js supported encoding (e.g., 'utf8', 'latin1')
|
|
123
|
+
* @returns {this} For method chaining
|
|
124
|
+
*/
|
|
125
|
+
encoding(encoding) {
|
|
126
|
+
this.csvToJson.encoding = encoding;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Read a CSV file and write parsed JSON to an output file (async)
|
|
132
|
+
* @param {string} fileInputName - Path to input CSV file
|
|
133
|
+
* @param {string} fileOutputName - Path to output JSON file
|
|
134
|
+
* @returns {Promise<void>}
|
|
135
|
+
* @throws {FileOperationError} If file operations fail
|
|
136
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
137
|
+
*/
|
|
138
|
+
async generateJsonFileFromCsv(fileInputName, fileOutputName) {
|
|
139
|
+
const jsonStringified = await this.getJsonFromCsvStringified(fileInputName);
|
|
140
|
+
await fileUtils.writeFileAsync(jsonStringified, fileOutputName);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Read a CSV file and return parsed data as stringified JSON (async)
|
|
145
|
+
* @param {string} fileInputName - Path to input CSV file
|
|
146
|
+
* @returns {Promise<string>} JSON stringified array of objects
|
|
147
|
+
* @throws {FileOperationError} If file read fails
|
|
148
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
149
|
+
*/
|
|
150
|
+
async getJsonFromCsvStringified(fileInputName) {
|
|
151
|
+
const json = await this.getJsonFromCsvAsync(fileInputName);
|
|
152
|
+
return JSON.stringify(json, undefined, 1);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Main async API method for reading CSV and returning parsed JSON
|
|
157
|
+
* Supports reading from file path or parsing CSV string content
|
|
158
|
+
* @param {string} inputFileNameOrCsv - File path or CSV string content
|
|
159
|
+
* @param {object} options - Configuration options
|
|
160
|
+
* @param {boolean} options.raw - If true, treats input as CSV string; if false, reads from file
|
|
161
|
+
* @returns {Promise<Array<object>>} Array of objects representing CSV rows
|
|
162
|
+
* @throws {InputValidationError} If input is invalid
|
|
163
|
+
* @throws {FileOperationError} If file read fails
|
|
164
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
165
|
+
* @example
|
|
166
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
167
|
+
* const data = await csvToJson.getJsonFromCsvAsync('resource/input.csv');
|
|
168
|
+
* console.log(data);
|
|
169
|
+
*/
|
|
170
|
+
async getJsonFromCsvAsync(inputFileNameOrCsv, options = {}) {
|
|
171
|
+
if (inputFileNameOrCsv === null || inputFileNameOrCsv === undefined) {
|
|
172
|
+
throw new InputValidationError(
|
|
173
|
+
'inputFileNameOrCsv',
|
|
174
|
+
'string (file path) or CSV string content',
|
|
175
|
+
`${typeof inputFileNameOrCsv}`,
|
|
176
|
+
'Either provide a valid file path or CSV content as a string.'
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (options.raw) {
|
|
181
|
+
if (inputFileNameOrCsv === '') {
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
return this.csvToJson.csvToJson(inputFileNameOrCsv);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const parsedCsv = await fileUtils.readFileAsync(inputFileNameOrCsv, this.csvToJson.encoding || 'utf8');
|
|
188
|
+
return this.csvToJson.csvToJson(parsedCsv);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Parse CSV string to JSON array (async)
|
|
193
|
+
* @param {string} csvString - CSV content as string
|
|
194
|
+
* @param {object} options - Configuration options (default: { raw: true })
|
|
195
|
+
* @returns {Promise<Array<object>>} Array of objects representing CSV rows
|
|
196
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
197
|
+
* @example
|
|
198
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
199
|
+
* const data = await csvToJson.csvStringToJsonAsync('name,age\nAlice,30');
|
|
200
|
+
* console.log(data);
|
|
201
|
+
*/
|
|
202
|
+
csvStringToJsonAsync(csvString, options = { raw: true }) {
|
|
203
|
+
return this.getJsonFromCsvAsync(csvString, options);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Parse CSV string to stringified JSON (async)
|
|
208
|
+
* @param {string} csvString - CSV content as string
|
|
209
|
+
* @returns {Promise<string>} JSON stringified array of objects
|
|
210
|
+
* @throws {CsvFormatError} If CSV is malformed
|
|
211
|
+
* @example
|
|
212
|
+
* const csvToJson = require('convert-csv-to-json');
|
|
213
|
+
* const jsonString = await csvToJson.csvStringToJsonStringifiedAsync('name,age\nAlice,30');
|
|
214
|
+
* console.log(jsonString);
|
|
215
|
+
*/
|
|
216
|
+
async csvStringToJsonStringifiedAsync(csvString) {
|
|
217
|
+
const json = await this.csvStringToJsonAsync(csvString);
|
|
218
|
+
return JSON.stringify(json, undefined, 1);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
module.exports = new CsvToJsonAsync();</code></pre>
|
|
223
|
+
</article>
|
|
224
|
+
</section>
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
</div>
|
|
230
|
+
|
|
231
|
+
<nav>
|
|
232
|
+
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BrowserApi.html">BrowserApi</a></li><li><a href="BrowserApiError.html">BrowserApiError</a></li><li><a href="ConfigurationError.html">ConfigurationError</a></li><li><a href="CsvFormatError.html">CsvFormatError</a></li><li><a href="CsvParsingError.html">CsvParsingError</a></li><li><a href="CsvToJson.html">CsvToJson</a></li><li><a href="CsvToJsonAsync.html">CsvToJsonAsync</a></li><li><a href="FileOperationError.html">FileOperationError</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="InputValidationError.html">InputValidationError</a></li><li><a href="JsonUtil.html">JsonUtil</a></li><li><a href="JsonValidationError.html">JsonValidationError</a></li></ul><h3>Global</h3><ul><li><a href="global.html#asciiEncoding">asciiEncoding</a></li><li><a href="global.html#base64Encoding">base64Encoding</a></li><li><a href="global.html#browser">browser</a></li><li><a href="global.html#csvStringToJson">csvStringToJson</a></li><li><a href="global.html#csvStringToJsonStringified">csvStringToJsonStringified</a></li><li><a href="global.html#csvToJsonAsync">csvToJsonAsync</a></li><li><a href="global.html#customEncoding">customEncoding</a></li><li><a href="global.html#fieldDelimiter">fieldDelimiter</a></li><li><a href="global.html#formatValueByType">formatValueByType</a></li><li><a href="global.html#generateJsonFileFromCsv">generateJsonFileFromCsv</a></li><li><a href="global.html#getJsonFromCsv">getJsonFromCsv</a></li><li><a href="global.html#hexEncoding">hexEncoding</a></li><li><a href="global.html#indexHeader">indexHeader</a></li><li><a href="global.html#latin1Encoding">latin1Encoding</a></li><li><a href="global.html#mapRows">mapRows</a></li><li><a href="global.html#parseSubArray">parseSubArray</a></li><li><a href="global.html#supportQuotedField">supportQuotedField</a></li><li><a href="global.html#trimHeaderFieldWhiteSpace">trimHeaderFieldWhiteSpace</a></li><li><a href="global.html#ucs2Encoding">ucs2Encoding</a></li><li><a href="global.html#utf16leEncoding">utf16leEncoding</a></li><li><a href="global.html#utf8Encoding">utf8Encoding</a></li></ul>
|
|
233
|
+
</nav>
|
|
234
|
+
|
|
235
|
+
<br class="clear">
|
|
236
|
+
|
|
237
|
+
<footer>
|
|
238
|
+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.5</a> on Fri Mar 20 2026 14:48:29 GMT+0000 (Coordinated Universal Time)
|
|
239
|
+
</footer>
|
|
240
|
+
|
|
241
|
+
<script> prettyPrint(); </script>
|
|
242
|
+
<script src="scripts/linenumber.js"> </script>
|
|
243
|
+
</body>
|
|
244
|
+
</html>
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>JSDoc: Source: src/util/errors.js</title>
|
|
6
|
+
|
|
7
|
+
<script src="scripts/prettify/prettify.js"> </script>
|
|
8
|
+
<script src="scripts/prettify/lang-css.js"> </script>
|
|
9
|
+
<!--[if lt IE 9]>
|
|
10
|
+
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
11
|
+
<![endif]-->
|
|
12
|
+
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
13
|
+
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
14
|
+
</head>
|
|
15
|
+
|
|
16
|
+
<body>
|
|
17
|
+
|
|
18
|
+
<div id="main">
|
|
19
|
+
|
|
20
|
+
<h1 class="page-title">Source: src/util/errors.js</h1>
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
<section>
|
|
28
|
+
<article>
|
|
29
|
+
<pre class="prettyprint source linenums"><code>'use strict';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Custom error classes following clean code principles
|
|
33
|
+
* Provides clear, actionable error messages with context
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Base class for all CSV parsing errors
|
|
38
|
+
* Provides consistent error formatting and context
|
|
39
|
+
*/
|
|
40
|
+
class CsvParsingError extends Error {
|
|
41
|
+
/**
|
|
42
|
+
* Create a CSV parsing error
|
|
43
|
+
* @param {string} message - Error message
|
|
44
|
+
* @param {string} code - Error code for identification
|
|
45
|
+
* @param {object} context - Additional context information (default: {})
|
|
46
|
+
*/
|
|
47
|
+
constructor(message, code, context = {}) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.name = 'CsvParsingError';
|
|
50
|
+
this.code = code;
|
|
51
|
+
this.context = context;
|
|
52
|
+
Error.captureStackTrace(this, this.constructor);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Convert error to formatted string with context information
|
|
57
|
+
* @returns {string} Formatted error message including context
|
|
58
|
+
*/
|
|
59
|
+
toString() {
|
|
60
|
+
let output = `${this.name}: ${this.message}`;
|
|
61
|
+
|
|
62
|
+
if (this.context && Object.keys(this.context).length > 0) {
|
|
63
|
+
output += '\n\nContext:';
|
|
64
|
+
Object.entries(this.context).forEach(([key, value]) => {
|
|
65
|
+
output += `\n ${key}: ${this.formatValue(value)}`;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return output;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Format a context value for display in error message
|
|
74
|
+
* @param {*} value - Value to format
|
|
75
|
+
* @returns {string} Formatted value string
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
formatValue(value) {
|
|
79
|
+
if (value === null) return 'null';
|
|
80
|
+
if (value === undefined) return 'undefined';
|
|
81
|
+
if (typeof value === 'string') return `"${value}"`;
|
|
82
|
+
if (typeof value === 'object') return JSON.stringify(value);
|
|
83
|
+
return String(value);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Input validation errors
|
|
89
|
+
* Thrown when function parameters don't meet expected type or value requirements
|
|
90
|
+
*/
|
|
91
|
+
class InputValidationError extends CsvParsingError {
|
|
92
|
+
/**
|
|
93
|
+
* Create an input validation error
|
|
94
|
+
* @param {string} paramName - Name of the invalid parameter
|
|
95
|
+
* @param {string} expectedType - Expected type description
|
|
96
|
+
* @param {string} receivedType - Actual type received
|
|
97
|
+
* @param {string} details - Additional error details (optional)
|
|
98
|
+
*/
|
|
99
|
+
constructor(paramName, expectedType, receivedType, details = '') {
|
|
100
|
+
const message =
|
|
101
|
+
`Invalid input: Parameter '${paramName}' is required.\n` +
|
|
102
|
+
`Expected: ${expectedType}\n` +
|
|
103
|
+
`Received: ${receivedType}${details ? '\n' + details : ''}`;
|
|
104
|
+
|
|
105
|
+
super(message, 'INPUT_VALIDATION_ERROR', {
|
|
106
|
+
parameter: paramName,
|
|
107
|
+
expectedType,
|
|
108
|
+
receivedType
|
|
109
|
+
});
|
|
110
|
+
this.name = 'InputValidationError';
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Configuration-related errors
|
|
116
|
+
* Thrown when configuration options conflict or are invalid
|
|
117
|
+
*/
|
|
118
|
+
class ConfigurationError extends CsvParsingError {
|
|
119
|
+
/**
|
|
120
|
+
* Create a configuration error
|
|
121
|
+
* @param {string} message - Error message
|
|
122
|
+
* @param {object} conflictingOptions - Configuration options in conflict (optional)
|
|
123
|
+
*/
|
|
124
|
+
constructor(message, conflictingOptions = {}) {
|
|
125
|
+
super(message, 'CONFIGURATION_ERROR', conflictingOptions);
|
|
126
|
+
this.name = 'ConfigurationError';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Create error for quoted field configuration conflict
|
|
131
|
+
* Occurs when quote character is used as delimiter while quoted fields are enabled
|
|
132
|
+
* @param {string} optionName - Name of the conflicting option
|
|
133
|
+
* @param {string} value - Value that causes the conflict
|
|
134
|
+
* @returns {ConfigurationError} Configured error instance
|
|
135
|
+
* @static
|
|
136
|
+
*/
|
|
137
|
+
static quotedFieldConflict(optionName, value) {
|
|
138
|
+
return new ConfigurationError(
|
|
139
|
+
`Configuration conflict: supportQuotedField() is enabled, but ${optionName} is set to '${value}'.\n` +
|
|
140
|
+
`The quote character (") cannot be used as a field delimiter, separator, or sub-array delimiter when quoted field support is active.\n\n` +
|
|
141
|
+
`Solutions:\n` +
|
|
142
|
+
` 1. Use a different character for ${optionName} (e.g., '|', '\\t', ';')\n` +
|
|
143
|
+
` 2. Disable supportQuotedField() if your CSV doesn't contain quoted fields\n` +
|
|
144
|
+
` 3. Refer to RFC 4180 for proper CSV formatting: https://tools.ietf.org/html/rfc4180`,
|
|
145
|
+
{ optionName, value, conflictingOption: 'supportQuotedField' }
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Create error for invalid header index
|
|
151
|
+
* Occurs when indexHeader() receives non-numeric value
|
|
152
|
+
* @param {*} value - Invalid header index value
|
|
153
|
+
* @returns {ConfigurationError} Configured error instance
|
|
154
|
+
* @static
|
|
155
|
+
*/
|
|
156
|
+
static invalidHeaderIndex(value) {
|
|
157
|
+
return new ConfigurationError(
|
|
158
|
+
`Invalid configuration: indexHeader() expects a numeric value.\n` +
|
|
159
|
+
`Received: ${typeof value} (${value})\n\n` +
|
|
160
|
+
`Solutions:\n` +
|
|
161
|
+
` 1. Ensure indexHeader() receives a number: indexHeader(0), indexHeader(1), etc.\n` +
|
|
162
|
+
` 2. Headers are typically found on row 0 (first line)\n` +
|
|
163
|
+
` 3. Use indexHeader(2) if headers are on the 3rd line`,
|
|
164
|
+
{ parameterName: 'indexHeader', value, type: typeof value }
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* CSV parsing errors with detailed context
|
|
171
|
+
* Thrown when CSV format is invalid or malformed
|
|
172
|
+
*/
|
|
173
|
+
class CsvFormatError extends CsvParsingError {
|
|
174
|
+
/**
|
|
175
|
+
* Create a CSV format error
|
|
176
|
+
* @param {string} message - Error message
|
|
177
|
+
* @param {object} context - Additional context information (optional)
|
|
178
|
+
*/
|
|
179
|
+
constructor(message, context = {}) {
|
|
180
|
+
super(message, 'CSV_FORMAT_ERROR', context);
|
|
181
|
+
this.name = 'CsvFormatError';
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Create error for missing CSV header row
|
|
186
|
+
* Occurs when no valid header row is found in CSV
|
|
187
|
+
* @returns {CsvFormatError} Configured error instance
|
|
188
|
+
* @static
|
|
189
|
+
*/
|
|
190
|
+
static missingHeader() {
|
|
191
|
+
return new CsvFormatError(
|
|
192
|
+
`CSV parsing error: No header row found.\n` +
|
|
193
|
+
`The CSV file appears to be empty or has no valid header line.\n\n` +
|
|
194
|
+
`Solutions:\n` +
|
|
195
|
+
` 1. Ensure your CSV file contains at least one row (header row)\n` +
|
|
196
|
+
` 2. Verify the file is not empty or contains only whitespace\n` +
|
|
197
|
+
` 3. Check if you need to use indexHeader(n) to specify a non-standard header row\n` +
|
|
198
|
+
` 4. Refer to RFC 4180 for proper CSV format: https://tools.ietf.org/html/rfc4180`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Create error for mismatched quotes in CSV
|
|
204
|
+
* Occurs when quoted fields are not properly closed
|
|
205
|
+
* @param {string} location - Where the error occurred (default: 'CSV')
|
|
206
|
+
* @returns {CsvFormatError} Configured error instance
|
|
207
|
+
* @static
|
|
208
|
+
*/
|
|
209
|
+
static mismatchedQuotes(location = 'CSV') {
|
|
210
|
+
return new CsvFormatError(
|
|
211
|
+
`CSV parsing error: Mismatched quotes detected in ${location}.\n` +
|
|
212
|
+
`A quoted field was not properly closed with a matching quote character.\n\n` +
|
|
213
|
+
`RFC 4180 rules for quoted fields:\n` +
|
|
214
|
+
` • Fields containing delimiters or quotes MUST be enclosed in double quotes\n` +
|
|
215
|
+
` • To include a quote within a quoted field, use two consecutive quotes: ""\n` +
|
|
216
|
+
` • Example: "Smith, John" (name contains comma)\n` +
|
|
217
|
+
` • Example: "He said ""Hello""" (text contains quotes)\n\n` +
|
|
218
|
+
`Solutions:\n` +
|
|
219
|
+
` 1. Review your CSV for properly paired quote characters\n` +
|
|
220
|
+
` 2. Use double quotes ("") to escape quotes within quoted fields\n` +
|
|
221
|
+
` 3. Ensure all commas within field values are inside quotes\n` +
|
|
222
|
+
` 4. Enable supportQuotedField(true) if you're using quoted fields`,
|
|
223
|
+
{ location }
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* File operation errors
|
|
230
|
+
* Thrown when file read or write operations fail
|
|
231
|
+
*/
|
|
232
|
+
class FileOperationError extends CsvParsingError {
|
|
233
|
+
/**
|
|
234
|
+
* Create a file operation error
|
|
235
|
+
* @param {string} operation - Type of operation that failed (e.g., 'read', 'write')
|
|
236
|
+
* @param {string} filePath - Path to the file where operation failed
|
|
237
|
+
* @param {Error} originalError - The underlying error object from Node.js
|
|
238
|
+
*/
|
|
239
|
+
constructor(operation, filePath, originalError) {
|
|
240
|
+
const message =
|
|
241
|
+
`File operation error: Failed to ${operation} file.\n` +
|
|
242
|
+
`File path: ${filePath}\n` +
|
|
243
|
+
`Reason: ${originalError.message}\n\n` +
|
|
244
|
+
`Solutions:\n` +
|
|
245
|
+
` 1. Verify the file path is correct: ${filePath}\n` +
|
|
246
|
+
` 2. Check file permissions (read access for input, write access for output)\n` +
|
|
247
|
+
` 3. Ensure the directory exists and is writable for output files\n` +
|
|
248
|
+
` 4. Verify the file is not in use by another process`;
|
|
249
|
+
|
|
250
|
+
super(message, 'FILE_OPERATION_ERROR', {
|
|
251
|
+
operation,
|
|
252
|
+
filePath,
|
|
253
|
+
originalError: originalError.message
|
|
254
|
+
});
|
|
255
|
+
this.name = 'FileOperationError';
|
|
256
|
+
this.originalError = originalError;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* JSON validation errors
|
|
262
|
+
* Thrown when parsed CSV data cannot be converted to valid JSON
|
|
263
|
+
*/
|
|
264
|
+
class JsonValidationError extends CsvParsingError {
|
|
265
|
+
/**
|
|
266
|
+
* Create a JSON validation error
|
|
267
|
+
* @param {string} csvData - The CSV data that failed validation
|
|
268
|
+
* @param {Error} originalError - The underlying JSON parsing error
|
|
269
|
+
*/
|
|
270
|
+
constructor(csvData, originalError) {
|
|
271
|
+
const message =
|
|
272
|
+
`JSON validation error: The parsed CSV data generated invalid JSON.\n` +
|
|
273
|
+
`This typically indicates malformed field names or values in the CSV.\n` +
|
|
274
|
+
`Original error: ${originalError.message}\n\n` +
|
|
275
|
+
`Solutions:\n` +
|
|
276
|
+
` 1. Check that field names are valid JavaScript identifiers (or will be converted safely)\n` +
|
|
277
|
+
` 2. Review the CSV data for special characters that aren't properly escaped\n` +
|
|
278
|
+
` 3. Enable supportQuotedField(true) for fields containing special characters\n` +
|
|
279
|
+
` 4. Verify that formatValueByType() isn't converting values incorrectly`;
|
|
280
|
+
|
|
281
|
+
super(message, 'JSON_VALIDATION_ERROR', {
|
|
282
|
+
originalError: originalError.message,
|
|
283
|
+
csvPreview: csvData ? csvData.substring(0, 200) : 'N/A'
|
|
284
|
+
});
|
|
285
|
+
this.name = 'JsonValidationError';
|
|
286
|
+
this.originalError = originalError;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Browser-specific errors
|
|
292
|
+
* Thrown when browser API operations fail
|
|
293
|
+
*/
|
|
294
|
+
class BrowserApiError extends CsvParsingError {
|
|
295
|
+
/**
|
|
296
|
+
* Create a browser API error
|
|
297
|
+
* @param {string} message - Error message
|
|
298
|
+
* @param {object} context - Additional context information (optional)
|
|
299
|
+
*/
|
|
300
|
+
constructor(message, context = {}) {
|
|
301
|
+
super(message, 'BROWSER_API_ERROR', context);
|
|
302
|
+
this.name = 'BrowserApiError';
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Create error for unavailable FileReader API
|
|
307
|
+
* Occurs when browser doesn't support FileReader
|
|
308
|
+
* @returns {BrowserApiError} Configured error instance
|
|
309
|
+
* @static
|
|
310
|
+
*/
|
|
311
|
+
static fileReaderNotAvailable() {
|
|
312
|
+
return new BrowserApiError(
|
|
313
|
+
`Browser compatibility error: FileReader API is not available.\n` +
|
|
314
|
+
`Your browser does not support the FileReader API required for file parsing.\n\n` +
|
|
315
|
+
`Solutions:\n` +
|
|
316
|
+
` 1. Use a modern browser that supports FileReader (Chrome 13+, Firefox 10+, Safari 6+)\n` +
|
|
317
|
+
` 2. Consider using csvStringToJson() or csvStringToJsonAsync() for string-based parsing\n` +
|
|
318
|
+
` 3. Implement a polyfill or alternative file reading method`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Create error for file parsing failure in browser
|
|
324
|
+
* Occurs when file read or CSV parse fails
|
|
325
|
+
* @param {Error} originalError - The underlying error that occurred
|
|
326
|
+
* @returns {BrowserApiError} Configured error instance
|
|
327
|
+
* @static
|
|
328
|
+
*/
|
|
329
|
+
static parseFileError(originalError) {
|
|
330
|
+
return new BrowserApiError(
|
|
331
|
+
`Browser file parsing error: Failed to read and parse the file.\n` +
|
|
332
|
+
`Error details: ${originalError.message}\n\n` +
|
|
333
|
+
`Solutions:\n` +
|
|
334
|
+
` 1. Verify the file is a valid CSV file\n` +
|
|
335
|
+
` 2. Check the file encoding (UTF-8 is recommended)\n` +
|
|
336
|
+
` 3. Try a smaller file to isolate the issue\n` +
|
|
337
|
+
` 4. Check browser console for additional error details`,
|
|
338
|
+
{ originalError: originalError.message }
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
module.exports = {
|
|
344
|
+
CsvParsingError,
|
|
345
|
+
InputValidationError,
|
|
346
|
+
ConfigurationError,
|
|
347
|
+
CsvFormatError,
|
|
348
|
+
FileOperationError,
|
|
349
|
+
JsonValidationError,
|
|
350
|
+
BrowserApiError
|
|
351
|
+
};
|
|
352
|
+
</code></pre>
|
|
353
|
+
</article>
|
|
354
|
+
</section>
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
</div>
|
|
360
|
+
|
|
361
|
+
<nav>
|
|
362
|
+
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BrowserApi.html">BrowserApi</a></li><li><a href="BrowserApiError.html">BrowserApiError</a></li><li><a href="ConfigurationError.html">ConfigurationError</a></li><li><a href="CsvFormatError.html">CsvFormatError</a></li><li><a href="CsvParsingError.html">CsvParsingError</a></li><li><a href="CsvToJson.html">CsvToJson</a></li><li><a href="CsvToJsonAsync.html">CsvToJsonAsync</a></li><li><a href="FileOperationError.html">FileOperationError</a></li><li><a href="FileUtils.html">FileUtils</a></li><li><a href="InputValidationError.html">InputValidationError</a></li><li><a href="JsonUtil.html">JsonUtil</a></li><li><a href="JsonValidationError.html">JsonValidationError</a></li></ul><h3>Global</h3><ul><li><a href="global.html#asciiEncoding">asciiEncoding</a></li><li><a href="global.html#base64Encoding">base64Encoding</a></li><li><a href="global.html#browser">browser</a></li><li><a href="global.html#csvStringToJson">csvStringToJson</a></li><li><a href="global.html#csvStringToJsonStringified">csvStringToJsonStringified</a></li><li><a href="global.html#csvToJsonAsync">csvToJsonAsync</a></li><li><a href="global.html#customEncoding">customEncoding</a></li><li><a href="global.html#fieldDelimiter">fieldDelimiter</a></li><li><a href="global.html#formatValueByType">formatValueByType</a></li><li><a href="global.html#generateJsonFileFromCsv">generateJsonFileFromCsv</a></li><li><a href="global.html#getJsonFromCsv">getJsonFromCsv</a></li><li><a href="global.html#hexEncoding">hexEncoding</a></li><li><a href="global.html#indexHeader">indexHeader</a></li><li><a href="global.html#latin1Encoding">latin1Encoding</a></li><li><a href="global.html#mapRows">mapRows</a></li><li><a href="global.html#parseSubArray">parseSubArray</a></li><li><a href="global.html#supportQuotedField">supportQuotedField</a></li><li><a href="global.html#trimHeaderFieldWhiteSpace">trimHeaderFieldWhiteSpace</a></li><li><a href="global.html#ucs2Encoding">ucs2Encoding</a></li><li><a href="global.html#utf16leEncoding">utf16leEncoding</a></li><li><a href="global.html#utf8Encoding">utf8Encoding</a></li></ul>
|
|
363
|
+
</nav>
|
|
364
|
+
|
|
365
|
+
<br class="clear">
|
|
366
|
+
|
|
367
|
+
<footer>
|
|
368
|
+
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.5</a> on Fri Mar 20 2026 14:48:29 GMT+0000 (Coordinated Universal Time)
|
|
369
|
+
</footer>
|
|
370
|
+
|
|
371
|
+
<script> prettyPrint(); </script>
|
|
372
|
+
<script src="scripts/linenumber.js"> </script>
|
|
373
|
+
</body>
|
|
374
|
+
</html>
|