convert-csv-to-json 4.14.0 → 4.16.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/docs/.nojekyll +0 -0
- package/docs/README.md +44 -0
- package/docs/demo.bundle.js +1708 -0
- package/docs/index.html +536 -0
- package/eslint.config.js +1 -1
- package/package.json +5 -1
- package/docs/api/BrowserApi.html +0 -2662
- package/docs/api/BrowserApiError.html +0 -669
- package/docs/api/ConfigurationError.html +0 -745
- package/docs/api/CsvFormatError.html +0 -677
- package/docs/api/CsvParsingError.html +0 -511
- package/docs/api/CsvToJson.html +0 -3367
- package/docs/api/CsvToJsonAsync.html +0 -2880
- package/docs/api/FileOperationError.html +0 -382
- package/docs/api/FileUtils.html +0 -1150
- package/docs/api/InputValidationError.html +0 -407
- package/docs/api/JsonUtil.html +0 -452
- package/docs/api/JsonValidationError.html +0 -357
- package/docs/api/StringUtils.html +0 -833
- package/docs/api/global.html +0 -3498
- package/docs/api/index.html +0 -414
- package/docs/api/index.js.html +0 -447
- package/docs/api/scripts/app.min.js +0 -1
- package/docs/api/scripts/linenumber.js +0 -26
- package/docs/api/scripts/search.js +0 -39
- package/docs/api/src_browserApi.js.html +0 -362
- package/docs/api/src_csvToJson.js.html +0 -696
- package/docs/api/src_csvToJsonAsync.js.html +0 -335
- package/docs/api/src_util_errors.js.html +0 -472
- package/docs/api/src_util_fileUtils.js.html +0 -238
- package/docs/api/src_util_jsonUtils.js.html +0 -166
- package/docs/api/src_util_stringUtils.js.html +0 -306
- package/docs/api/styles/app.min.css +0 -1
- package/docs/api/styles/iframe.css +0 -13
- package/docs/api/styles/prettify-jsdoc.css +0 -111
- package/docs/api/styles/prettify-tomorrow.css +0 -132
- package/docs/api/styles/reset.css +0 -44
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
<!DOCTYPE html>
|
|
4
|
-
<html lang="en">
|
|
5
|
-
|
|
6
|
-
<head>
|
|
7
|
-
|
|
8
|
-
<meta charset="utf-8">
|
|
9
|
-
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
10
|
-
<title> src/csvToJsonAsync.js</title>
|
|
11
|
-
|
|
12
|
-
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
|
|
13
|
-
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
|
|
14
|
-
<script src="./build/entry.js"></script>
|
|
15
|
-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
|
16
|
-
<!--[if lt IE 9]>
|
|
17
|
-
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
18
|
-
<![endif]-->
|
|
19
|
-
<link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
|
|
20
|
-
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
|
|
21
|
-
<link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
|
|
22
|
-
<link type="text/css" rel="stylesheet" href="styles/app.min.css">
|
|
23
|
-
<link type="text/css" rel="stylesheet" href="styles/iframe.css">
|
|
24
|
-
<link type="text/css" rel="stylesheet" href="./node_modules/better-docs/css/better-docs.css">
|
|
25
|
-
<script async defer src="https://buttons.github.io/buttons.js"></script>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
</head>
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
<body class="layout small-header">
|
|
33
|
-
<div id="stickyNavbarOverlay"></div>
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
<div class="top-nav">
|
|
37
|
-
<div class="inner">
|
|
38
|
-
<a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
|
|
39
|
-
<span aria-hidden="true"></span>
|
|
40
|
-
<span aria-hidden="true"></span>
|
|
41
|
-
<span aria-hidden="true"></span>
|
|
42
|
-
</a>
|
|
43
|
-
<div class="logo">
|
|
44
|
-
|
|
45
|
-
<a class="image" href="index.html">
|
|
46
|
-
<img src="https://raw.githubusercontent.com/iuccio/CSVtoJSON/master/CSVtoJSON.png" alt="logo">
|
|
47
|
-
</a>
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
<a href="index.html">
|
|
51
|
-
<h1 class="navbar-item">Convert CSV to JSON</h1>
|
|
52
|
-
</a>
|
|
53
|
-
|
|
54
|
-
</div>
|
|
55
|
-
<div class="menu">
|
|
56
|
-
|
|
57
|
-
<div class="navigation">
|
|
58
|
-
<a
|
|
59
|
-
href="index.html"
|
|
60
|
-
class="link"
|
|
61
|
-
>
|
|
62
|
-
Documentation
|
|
63
|
-
</a>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
<a
|
|
68
|
-
class="link user-link "
|
|
69
|
-
href="https://github.com/iuccio/CSVtoJSON"
|
|
70
|
-
>
|
|
71
|
-
GitHub
|
|
72
|
-
</a>
|
|
73
|
-
|
|
74
|
-
<a
|
|
75
|
-
class="link user-link "
|
|
76
|
-
href="https://www.npmjs.com/package/convert-csv-to-json"
|
|
77
|
-
>
|
|
78
|
-
NPM
|
|
79
|
-
</a>
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
</div>
|
|
84
|
-
</div>
|
|
85
|
-
</div>
|
|
86
|
-
</div>
|
|
87
|
-
<div id="main">
|
|
88
|
-
<div
|
|
89
|
-
class="sidebar "
|
|
90
|
-
id="sidebarNav"
|
|
91
|
-
>
|
|
92
|
-
|
|
93
|
-
<nav>
|
|
94
|
-
|
|
95
|
-
<h2><a href="index.html">Documentation</a></h2><div class="category"></div><div class="category"><h2>1-Core API</h2><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#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></div><div class="category"><h2>2-Sync</h2><h3>Classes</h3><ul><li><a href="CsvToJson.html">CsvToJson</a></li></ul></div><div class="category"><h2>3-Async</h2><h3>Classes</h3><ul><li><a href="CsvToJsonAsync.html">CsvToJsonAsync</a></li></ul></div><div class="category"><h2>4-Browser</h2><h3>Classes</h3><ul><li><a href="BrowserApi.html">BrowserApi</a></li></ul></div><div class="category"><h2>Error Classes</h2><h3>Classes</h3><ul><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="FileOperationError.html">FileOperationError</a></li><li><a href="InputValidationError.html">InputValidationError</a></li><li><a href="JsonValidationError.html">JsonValidationError</a></li></ul></div><div class="category"><h2>Utilities</h2><h3>Classes</h3><ul><li><a href="FileUtils.html">FileUtils</a></li><li><a href="JsonUtil.html">JsonUtil</a></li><li><a href="StringUtils.html">StringUtils</a></li></ul></div>
|
|
96
|
-
|
|
97
|
-
</nav>
|
|
98
|
-
</div>
|
|
99
|
-
<div class="core" id="main-content-wrapper">
|
|
100
|
-
<div class="content">
|
|
101
|
-
<header class="page-title">
|
|
102
|
-
<p>Source</p>
|
|
103
|
-
<h1>src/csvToJsonAsync.js</h1>
|
|
104
|
-
</header>
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
<section>
|
|
111
|
-
<article>
|
|
112
|
-
<pre class="prettyprint source linenums"><code>/* globals CsvFormatError, FileOperationError */
|
|
113
|
-
'use strict';
|
|
114
|
-
|
|
115
|
-
const fileUtils = require('./util/fileUtils');
|
|
116
|
-
const csvToJson = require('./csvToJson');
|
|
117
|
-
const { InputValidationError } = require('./util/errors');
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Asynchronous CSV to JSON converter
|
|
121
|
-
* Proxies configuration to sync instance but provides async file I/O methods
|
|
122
|
-
* @category 3-Async
|
|
123
|
-
*/
|
|
124
|
-
class CsvToJsonAsync {
|
|
125
|
-
/**
|
|
126
|
-
* Constructor initializes proxy to sync csvToJson instance
|
|
127
|
-
*/
|
|
128
|
-
constructor() {
|
|
129
|
-
// Proxy the configuration methods to the sync instance
|
|
130
|
-
this.csvToJson = csvToJson;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Enable or disable automatic type formatting for values
|
|
135
|
-
* @param {boolean} active - Whether to format values by type
|
|
136
|
-
* @returns {this} For method chaining
|
|
137
|
-
*/
|
|
138
|
-
formatValueByType(active) {
|
|
139
|
-
this.csvToJson.formatValueByType(active);
|
|
140
|
-
return this;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Enable or disable support for RFC 4180 quoted fields
|
|
145
|
-
* @param {boolean} active - Whether to support quoted fields
|
|
146
|
-
* @returns {this} For method chaining
|
|
147
|
-
*/
|
|
148
|
-
supportQuotedField(active) {
|
|
149
|
-
this.csvToJson.supportQuotedField(active);
|
|
150
|
-
return this;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Set the field delimiter character
|
|
155
|
-
* @param {string} delimiter - Character(s) to use as field separator
|
|
156
|
-
* @returns {this} For method chaining
|
|
157
|
-
*/
|
|
158
|
-
fieldDelimiter(delimiter) {
|
|
159
|
-
this.csvToJson.fieldDelimiter(delimiter);
|
|
160
|
-
return this;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Configure whitespace handling in header field names
|
|
165
|
-
* @param {boolean} active - If true, removes all whitespace; if false, only trims edges
|
|
166
|
-
* @returns {this} For method chaining
|
|
167
|
-
*/
|
|
168
|
-
trimHeaderFieldWhiteSpace(active) {
|
|
169
|
-
this.csvToJson.trimHeaderFieldWhiteSpace(active);
|
|
170
|
-
return this;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Set the row index where CSV headers are located
|
|
175
|
-
* @param {number} indexHeader - Zero-based row index containing headers
|
|
176
|
-
* @returns {this} For method chaining
|
|
177
|
-
*/
|
|
178
|
-
indexHeader(indexHeader) {
|
|
179
|
-
this.csvToJson.indexHeader(indexHeader);
|
|
180
|
-
return this;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Configure sub-array parsing for special field values
|
|
185
|
-
* @param {string} delimiter - Bracket character (default: '*')
|
|
186
|
-
* @param {string} separator - Item separator within brackets (default: ',')
|
|
187
|
-
* @returns {this} For method chaining
|
|
188
|
-
*/
|
|
189
|
-
parseSubArray(delimiter = '*', separator = ',') {
|
|
190
|
-
this.csvToJson.parseSubArray(delimiter, separator);
|
|
191
|
-
return this;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Set a mapper function to transform each row after conversion
|
|
196
|
-
* @param {function(object, number): (object|null)} mapperFn - Function receiving (row, index) that returns transformed row or null to filter
|
|
197
|
-
* @returns {this} For method chaining
|
|
198
|
-
*/
|
|
199
|
-
mapRows(mapperFn) {
|
|
200
|
-
this.csvToJson.mapRows(mapperFn);
|
|
201
|
-
return this;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Set file encoding for reading CSV files
|
|
206
|
-
* @param {string} encoding - Node.js supported encoding (e.g., 'utf8', 'latin1')
|
|
207
|
-
* @returns {this} For method chaining
|
|
208
|
-
*/
|
|
209
|
-
encoding(encoding) {
|
|
210
|
-
this.csvToJson.encoding = encoding;
|
|
211
|
-
return this;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Read a CSV file and write parsed JSON to an output file (async)
|
|
216
|
-
* @param {string} fileInputName - Path to input CSV file
|
|
217
|
-
* @param {string} fileOutputName - Path to output JSON file
|
|
218
|
-
* @returns {Promise<void>}
|
|
219
|
-
* @throws {FileOperationError} If file operations fail
|
|
220
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
221
|
-
*/
|
|
222
|
-
async generateJsonFileFromCsv(fileInputName, fileOutputName) {
|
|
223
|
-
const jsonStringified = await this.getJsonFromCsvStringified(fileInputName);
|
|
224
|
-
await fileUtils.writeFileAsync(jsonStringified, fileOutputName);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Read a CSV file and return parsed data as stringified JSON (async)
|
|
229
|
-
* @param {string} fileInputName - Path to input CSV file
|
|
230
|
-
* @returns {Promise<string>} JSON stringified array of objects
|
|
231
|
-
* @throws {FileOperationError} If file read fails
|
|
232
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
233
|
-
*/
|
|
234
|
-
async getJsonFromCsvStringified(fileInputName) {
|
|
235
|
-
const json = await this.getJsonFromCsvAsync(fileInputName);
|
|
236
|
-
return JSON.stringify(json, undefined, 1);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Main async API method for reading CSV and returning parsed JSON
|
|
241
|
-
* Supports reading from file path or parsing CSV string content
|
|
242
|
-
* @param {string} inputFileNameOrCsv - File path or CSV string content
|
|
243
|
-
* @param {object} options - Configuration options
|
|
244
|
-
* @param {boolean} options.raw - If true, treats input as CSV string; if false, reads from file
|
|
245
|
-
* @returns {Promise<Array<object>>} Array of objects representing CSV rows
|
|
246
|
-
* @throws {InputValidationError} If input is invalid
|
|
247
|
-
* @throws {FileOperationError} If file read fails
|
|
248
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
249
|
-
* @example
|
|
250
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
251
|
-
* const data = await csvToJson.getJsonFromCsvAsync('resource/input.csv');
|
|
252
|
-
* console.log(data);
|
|
253
|
-
*/
|
|
254
|
-
async getJsonFromCsvAsync(inputFileNameOrCsv, options = {}) {
|
|
255
|
-
if (inputFileNameOrCsv === null || inputFileNameOrCsv === undefined) {
|
|
256
|
-
throw new InputValidationError(
|
|
257
|
-
'inputFileNameOrCsv',
|
|
258
|
-
'string (file path) or CSV string content',
|
|
259
|
-
`${typeof inputFileNameOrCsv}`,
|
|
260
|
-
'Either provide a valid file path or CSV content as a string.'
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (options.raw) {
|
|
265
|
-
if (inputFileNameOrCsv === '') {
|
|
266
|
-
return [];
|
|
267
|
-
}
|
|
268
|
-
return this.csvToJson.csvToJson(inputFileNameOrCsv);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const parsedCsv = await fileUtils.readFileAsync(inputFileNameOrCsv, this.csvToJson.encoding || 'utf8');
|
|
272
|
-
return this.csvToJson.csvToJson(parsedCsv);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Parse CSV string to JSON array (async)
|
|
277
|
-
* @param {string} csvString - CSV content as string
|
|
278
|
-
* @param {object} options - Configuration options (default: { raw: true })
|
|
279
|
-
* @returns {Promise<Array<object>>} Array of objects representing CSV rows
|
|
280
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
281
|
-
* @example
|
|
282
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
283
|
-
* const data = await csvToJson.csvStringToJsonAsync('name,age\nAlice,30');
|
|
284
|
-
* console.log(data);
|
|
285
|
-
*/
|
|
286
|
-
csvStringToJsonAsync(csvString, options = { raw: true }) {
|
|
287
|
-
return this.getJsonFromCsvAsync(csvString, options);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Parse CSV string to stringified JSON (async)
|
|
292
|
-
* @param {string} csvString - CSV content as string
|
|
293
|
-
* @returns {Promise<string>} JSON stringified array of objects
|
|
294
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
295
|
-
* @example
|
|
296
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
297
|
-
* const jsonString = await csvToJson.csvStringToJsonStringifiedAsync('name,age\nAlice,30');
|
|
298
|
-
* console.log(jsonString);
|
|
299
|
-
*/
|
|
300
|
-
async csvStringToJsonStringifiedAsync(csvString) {
|
|
301
|
-
const json = await this.csvStringToJsonAsync(csvString);
|
|
302
|
-
return JSON.stringify(json, undefined, 1);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
module.exports = new CsvToJsonAsync();</code></pre>
|
|
307
|
-
</article>
|
|
308
|
-
</section>
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
</div>
|
|
314
|
-
|
|
315
|
-
<footer class="footer">
|
|
316
|
-
<div class="content has-text-centered">
|
|
317
|
-
<p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a></p>
|
|
318
|
-
<p class="sidebar-created-by">
|
|
319
|
-
<a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
|
|
320
|
-
<a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
|
|
321
|
-
</p>
|
|
322
|
-
</div>
|
|
323
|
-
</footer>
|
|
324
|
-
|
|
325
|
-
</div>
|
|
326
|
-
<div id="side-nav" class="side-nav">
|
|
327
|
-
</div>
|
|
328
|
-
</div>
|
|
329
|
-
<script src="scripts/app.min.js"></script>
|
|
330
|
-
<script>PR.prettyPrint();</script>
|
|
331
|
-
<script src="scripts/linenumber.js"> </script>
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
</body>
|
|
335
|
-
</html>
|