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,362 +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/browserApi.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/browserApi.js</h1>
|
|
104
|
-
</header>
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
<section>
|
|
111
|
-
<article>
|
|
112
|
-
<pre class="prettyprint source linenums"><code>/* globals CsvFormatError */
|
|
113
|
-
|
|
114
|
-
"use strict";
|
|
115
|
-
|
|
116
|
-
const csvToJson = require('./csvToJson');
|
|
117
|
-
const { InputValidationError, BrowserApiError } = require('./util/errors');
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Browser-friendly CSV to JSON API
|
|
121
|
-
* Provides methods for parsing CSV strings and File/Blob objects in browser environments
|
|
122
|
-
* Proxies configuration to sync csvToJson instance
|
|
123
|
-
* @category 4-Browser
|
|
124
|
-
*/
|
|
125
|
-
class BrowserApi {
|
|
126
|
-
/**
|
|
127
|
-
* Constructor initializes proxy to sync csvToJson instance
|
|
128
|
-
*/
|
|
129
|
-
constructor() {
|
|
130
|
-
// reuse the existing csvToJson instance for parsing and configuration
|
|
131
|
-
this.csvToJson = csvToJson;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Enable or disable automatic type formatting for values
|
|
136
|
-
* @param {boolean} active - Whether to format values by type (default: true)
|
|
137
|
-
* @returns {this} For method chaining
|
|
138
|
-
*/
|
|
139
|
-
formatValueByType(active = true) {
|
|
140
|
-
this.csvToJson.formatValueByType(active);
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Enable or disable support for RFC 4180 quoted fields
|
|
146
|
-
* @param {boolean} active - Whether to support quoted fields (default: false)
|
|
147
|
-
* @returns {this} For method chaining
|
|
148
|
-
*/
|
|
149
|
-
supportQuotedField(active = false) {
|
|
150
|
-
this.csvToJson.supportQuotedField(active);
|
|
151
|
-
return this;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Set the field delimiter character
|
|
156
|
-
* @param {string} delimiter - Character(s) to use as field separator
|
|
157
|
-
* @returns {this} For method chaining
|
|
158
|
-
*/
|
|
159
|
-
fieldDelimiter(delimiter) {
|
|
160
|
-
this.csvToJson.fieldDelimiter(delimiter);
|
|
161
|
-
return this;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Configure whitespace handling in header field names
|
|
166
|
-
* @param {boolean} active - If true, removes all whitespace; if false, only trims edges (default: false)
|
|
167
|
-
* @returns {this} For method chaining
|
|
168
|
-
*/
|
|
169
|
-
trimHeaderFieldWhiteSpace(active = false) {
|
|
170
|
-
this.csvToJson.trimHeaderFieldWhiteSpace(active);
|
|
171
|
-
return this;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Set the row index where CSV headers are located
|
|
176
|
-
* @param {number} index - Zero-based row index containing headers
|
|
177
|
-
* @returns {this} For method chaining
|
|
178
|
-
*/
|
|
179
|
-
indexHeader(index) {
|
|
180
|
-
this.csvToJson.indexHeader(index);
|
|
181
|
-
return this;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Configure sub-array parsing for special field values
|
|
186
|
-
* @param {string} delimiter - Bracket character (default: '*')
|
|
187
|
-
* @param {string} separator - Item separator within brackets (default: ',')
|
|
188
|
-
* @returns {this} For method chaining
|
|
189
|
-
*/
|
|
190
|
-
parseSubArray(delimiter = '*', separator = ',') {
|
|
191
|
-
this.csvToJson.parseSubArray(delimiter, separator);
|
|
192
|
-
return this;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Set a mapper function to transform each row after conversion
|
|
197
|
-
* @param {function(object, number): (object|null)} mapperFn - Function receiving (row, index) that returns transformed row or null to filter
|
|
198
|
-
* @returns {this} For method chaining
|
|
199
|
-
*/
|
|
200
|
-
mapRows(mapperFn) {
|
|
201
|
-
this.csvToJson.mapRows(mapperFn);
|
|
202
|
-
return this;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Parse a CSV string and return as JSON array of objects
|
|
207
|
-
* @param {string} csvString - CSV content as string
|
|
208
|
-
* @returns {Array<object>} Array of objects representing CSV rows
|
|
209
|
-
* @throws {InputValidationError} If csvString is invalid
|
|
210
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
211
|
-
* @example
|
|
212
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
213
|
-
* const rows = csvToJson.browser.csvStringToJson('name,age\nAlice,30');
|
|
214
|
-
* console.log(rows); // [{ name: 'Alice', age: '30' }]
|
|
215
|
-
*/
|
|
216
|
-
csvStringToJson(csvString) {
|
|
217
|
-
if (csvString === undefined || csvString === null) {
|
|
218
|
-
throw new InputValidationError(
|
|
219
|
-
'csvString',
|
|
220
|
-
'string',
|
|
221
|
-
`${typeof csvString}`,
|
|
222
|
-
'Provide valid CSV content as a string to parse.'
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
return this.csvToJson.csvToJson(csvString);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Parse a CSV string and return as stringified JSON
|
|
230
|
-
* @param {string} csvString - CSV content as string
|
|
231
|
-
* @returns {string} JSON stringified array of objects
|
|
232
|
-
* @throws {InputValidationError} If csvString is invalid
|
|
233
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
234
|
-
* @example
|
|
235
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
236
|
-
* const jsonString = csvToJson.browser.csvStringToJsonStringified('name,age\nAlice,30');
|
|
237
|
-
* console.log(jsonString);
|
|
238
|
-
*/
|
|
239
|
-
csvStringToJsonStringified(csvString) {
|
|
240
|
-
if (csvString === undefined || csvString === null) {
|
|
241
|
-
throw new InputValidationError(
|
|
242
|
-
'csvString',
|
|
243
|
-
'string',
|
|
244
|
-
`${typeof csvString}`,
|
|
245
|
-
'Provide valid CSV content as a string to parse.'
|
|
246
|
-
);
|
|
247
|
-
}
|
|
248
|
-
return this.csvToJson.csvStringToJsonStringified(csvString);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Parse a CSV string asynchronously (returns resolved Promise)
|
|
253
|
-
* @param {string} csvString - CSV content as string
|
|
254
|
-
* @returns {Promise<Array<object>>} Promise resolving to array of objects
|
|
255
|
-
* @throws {InputValidationError} If csvString is invalid
|
|
256
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
257
|
-
* @example
|
|
258
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
259
|
-
* const rows = await csvToJson.browser.csvStringToJsonAsync('name,age\nAlice,30');
|
|
260
|
-
* console.log(rows);
|
|
261
|
-
*/
|
|
262
|
-
csvStringToJsonAsync(csvString) {
|
|
263
|
-
return Promise.resolve(this.csvStringToJson(csvString));
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Parse a CSV string asynchronously and return as stringified JSON
|
|
268
|
-
* @param {string} csvString - CSV content as string
|
|
269
|
-
* @returns {Promise<string>} Promise resolving to JSON stringified array
|
|
270
|
-
* @throws {InputValidationError} If csvString is invalid
|
|
271
|
-
* @throws {CsvFormatError} If CSV is malformed
|
|
272
|
-
* @example
|
|
273
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
274
|
-
* const json = await csvToJson.browser.csvStringToJsonStringifiedAsync('name,age\nAlice,30');
|
|
275
|
-
* console.log(json);
|
|
276
|
-
*/
|
|
277
|
-
csvStringToJsonStringifiedAsync(csvString) {
|
|
278
|
-
return Promise.resolve(this.csvStringToJsonStringified(csvString));
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Parse a browser File or Blob object to JSON array.
|
|
283
|
-
* @param {File|Blob} file - File or Blob to read as text
|
|
284
|
-
* @param {object} [options] - options: { encoding?: string }
|
|
285
|
-
* @returns {Promise<object[]>} Promise resolving to parsed JSON rows
|
|
286
|
-
* @example
|
|
287
|
-
* const csvToJson = require('convert-csv-to-json');
|
|
288
|
-
* const fileInput = document.querySelector('#csvfile').files[0];
|
|
289
|
-
* const rows = await csvToJson.browser.parseFile(fileInput);
|
|
290
|
-
* console.log(rows);
|
|
291
|
-
*/
|
|
292
|
-
parseFile(file, options = {}) {
|
|
293
|
-
if (!file) {
|
|
294
|
-
return Promise.reject(new InputValidationError(
|
|
295
|
-
'file',
|
|
296
|
-
'File or Blob object',
|
|
297
|
-
`${typeof file}`,
|
|
298
|
-
'Provide a valid File or Blob object to parse.'
|
|
299
|
-
));
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return new Promise((resolve, reject) => {
|
|
303
|
-
if (typeof FileReader === 'undefined') {
|
|
304
|
-
reject(BrowserApiError.fileReaderNotAvailable());
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
const reader = new FileReader();
|
|
309
|
-
reader.onerror = () => reject(BrowserApiError.parseFileError(
|
|
310
|
-
reader.error || new Error('Unknown file reading error')
|
|
311
|
-
));
|
|
312
|
-
reader.onload = () => {
|
|
313
|
-
try {
|
|
314
|
-
const text = reader.result;
|
|
315
|
-
const result = this.csvToJson.csvToJson(String(text));
|
|
316
|
-
resolve(result);
|
|
317
|
-
} catch (err) {
|
|
318
|
-
reject(BrowserApiError.parseFileError(err));
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
|
|
322
|
-
// If encoding is provided, pass it to readAsText
|
|
323
|
-
if (options.encoding) {
|
|
324
|
-
reader.readAsText(file, options.encoding);
|
|
325
|
-
} else {
|
|
326
|
-
reader.readAsText(file);
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
module.exports = new BrowserApi();
|
|
333
|
-
</code></pre>
|
|
334
|
-
</article>
|
|
335
|
-
</section>
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
</div>
|
|
341
|
-
|
|
342
|
-
<footer class="footer">
|
|
343
|
-
<div class="content has-text-centered">
|
|
344
|
-
<p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a></p>
|
|
345
|
-
<p class="sidebar-created-by">
|
|
346
|
-
<a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
|
|
347
|
-
<a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
|
|
348
|
-
</p>
|
|
349
|
-
</div>
|
|
350
|
-
</footer>
|
|
351
|
-
|
|
352
|
-
</div>
|
|
353
|
-
<div id="side-nav" class="side-nav">
|
|
354
|
-
</div>
|
|
355
|
-
</div>
|
|
356
|
-
<script src="scripts/app.min.js"></script>
|
|
357
|
-
<script>PR.prettyPrint();</script>
|
|
358
|
-
<script src="scripts/linenumber.js"> </script>
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
</body>
|
|
362
|
-
</html>
|