-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathindex.js
More file actions
342 lines (313 loc) · 11.1 KB
/
index.js
File metadata and controls
342 lines (313 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* CsvToJson - CSV to JSON converter library
* Main entry point providing chainable API for CSV parsing with multiple configuration options
*/
/* globals FileOperationError, CsvFormatError, JsonValidationError, InputValidationError */
"use strict";
let csvToJson = require("./src/csvToJson.js");
const encodingOps = {
utf8: 'utf8',
ucs2: 'ucs2',
utf16le: 'utf16le',
latin1: 'latin1',
ascii: 'ascii',
base64: 'base64',
hex: 'hex'
};
/**
* Enable or disable automatic type formatting for values
* Converts numeric strings to numbers, 'true'/'false' to booleans
* @category 1-Core API
* @param {boolean} active - Whether to format values by type (default: true)
* @returns {object} Module context for method chaining
*/
exports.formatValueByType = function (active = true) {
csvToJson.formatValueByType(active);
return this;
};
/**
* Enable or disable support for RFC 4180 quoted fields
* When enabled, fields wrapped in double quotes can contain delimiters and newlines
* @category 1-Core API
* @param {boolean} active - Whether to support quoted fields (default: false)
* @returns {object} Module context for method chaining
*/
exports.supportQuotedField = function (active = false) {
csvToJson.supportQuotedField(active);
return this;
};
/**
* Set the field delimiter character used to separate CSV fields
* @category 1-Core API
* @param {string} delimiter - Character(s) to use as field separator (default: ',')
* @returns {object} Module context for method chaining
*/
exports.fieldDelimiter = function (delimiter) {
csvToJson.fieldDelimiter(delimiter);
return this;
};
/**
* Configure whitespace handling in CSV header field names
* When active, removes all whitespace from header names (e.g., "My Name" → "MyName")
* When inactive, only trims leading and trailing whitespace
* @category 1-Core API
* @param {boolean} active - Whether to remove all whitespace from headers (default: false)
* @returns {object} Module context for method chaining
*/
exports.trimHeaderFieldWhiteSpace = function (active = false) {
csvToJson.trimHeaderFieldWhiteSpace(active);
return this;
};
/**
* Set the row index where CSV headers are located
* Use this if headers are not on the first line (row 0)
* @category 1-Core API
* @param {number} index - Zero-based row index containing headers
* @returns {object} Module context for method chaining
*/
exports.indexHeader = function (index) {
csvToJson.indexHeader(index);
return this;
};
/**
* Configure sub-array parsing for special field values
* Fields bracketed by delimiter and containing separator are parsed into arrays
* @category 1-Core API
* @param {string} delimiter - Bracket character (default: '*')
* @param {string} separator - Item separator within brackets (default: ',')
* @returns {object} Module context for method chaining
* @example
* // Input field: "*val1,val2,val3*"
* // Output array: ["val1", "val2", "val3"]
* csvToJson.parseSubArray('*', ',')
*/
exports.parseSubArray = function (delimiter, separator) {
csvToJson.parseSubArray(delimiter, separator);
return this;
};
/**
* Set custom file encoding for reading CSV files
* Useful for non-UTF8 encoded files
* @category 1-Core API
* @param {string} encoding - Node.js supported encoding (e.g., 'utf8', 'latin1', 'ascii')
* @returns {object} Module context for method chaining
*/
exports.customEncoding = function (encoding) {
csvToJson.encoding = encoding;
return this;
};
/**
* Set UTF-8 encoding (default encoding)
* @category 1-Core API
* @returns {object} Module context for method chaining
*/
exports.utf8Encoding = function utf8Encoding() {
csvToJson.encoding = encodingOps.utf8;
return this;
};
/**
* Set UCS-2 encoding for reading files
* @category 1-Core API
* @returns {object} Module context for method chaining
*/
exports.ucs2Encoding = function () {
csvToJson.encoding = encodingOps.ucs2;
return this;
};
/**
* Set UTF-16 LE encoding for reading files
* @category 1-Core API
* @returns {object} Module context for method chaining
*/
exports.utf16leEncoding = function () {
csvToJson.encoding = encodingOps.utf16le;
return this;
};
/**
* Set Latin-1 (ISO-8859-1) encoding for reading files
* @category 1-Core API
* @returns {object} Module context for method chaining
*/
exports.latin1Encoding = function () {
csvToJson.encoding = encodingOps.latin1;
return this;
};
/**
* Set ASCII encoding for reading files
* @category 1-Core API
* @returns {object} Module context for method chaining
*/
exports.asciiEncoding = function () {
csvToJson.encoding = encodingOps.ascii;
return this;
};
/**
* Set Base64 encoding for reading files
* @category 1-Core API
* @returns {object} Module context for method chaining
*/
exports.base64Encoding = function () {
this.csvToJson = encodingOps.base64;
return this;
};
/**
* Set Hex encoding for reading files
* @category 1-Core API
* @returns {object} Module context for method chaining
*/
exports.hexEncoding = function () {
this.csvToJson = encodingOps.hex;
return this;
};
/**
* Set a mapper function to transform each row after conversion
* The mapper function receives (row, index) where row is the JSON object
* and index is the 0-based row number. Return null/undefined to filter out rows.
* @category 1-Core API
* @param {function(object, number): (object|null)} mapperFn - Function to transform each row
* @returns {object} Module context for method chaining
* @example
* csvToJson
* .mapRows((row, idx) => idx % 2 === 0 ? row : null) // Keep every other row
* .getJsonFromCsv('input.csv')
*/
exports.mapRows = function (mapperFn) {
csvToJson.mapRows(mapperFn);
return this;
};
/**
* Parse CSV file and write the parsed JSON to an output file (synchronous)
* @param {string} inputFileName - Path to input CSV file
* @param {string} outputFileName - Path to output JSON file
* @throws {Error} If inputFileName or outputFileName is not defined
* @throws {FileOperationError} If file operations fail
* @throws {CsvFormatError} If CSV is malformed
* @category 1-Core API
* @example
* const csvToJson = require('convert-csv-to-json');
* csvToJson.generateJsonFileFromCsv('input.csv', 'output.json');
*/
exports.generateJsonFileFromCsv = function(inputFileName, outputFileName) {
if (!inputFileName) {
throw new Error("inputFileName is not defined!!!");
}
if (!outputFileName) {
throw new Error("outputFileName is not defined!!!");
}
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName);
};
/**
* Parse CSV file and return parsed data as JSON array of objects (synchronous)
* @param {string} inputFileName - Path to input CSV file
* @returns {Array<object>} Array of objects representing CSV rows
* @throws {Error} If inputFileName is not defined
* @throws {FileOperationError} If file read fails
* @throws {CsvFormatError} If CSV is malformed
* @category 1-Core API
* @example
* const csvToJson = require('convert-csv-to-json');
* const rows = csvToJson.getJsonFromCsv('resource/input.csv');
* console.log(rows);
*/
exports.getJsonFromCsv = function(inputFileName) {
if (!inputFileName) {
throw new Error("inputFileName is not defined!!!");
}
return csvToJson.getJsonFromCsv(inputFileName);
};
/**
* Parse CSV file asynchronously and return parsed data as JSON array
* @param {string} inputFileNameOrCsv - Path to file or CSV string
* @param {object} options - Configuration options
* @param {boolean} options.raw - If true, treats first param as CSV content; if false, reads from file
* @returns {Promise<Array<object>>} Promise resolving to array of objects
* @throws {InputValidationError} If input is invalid
* @throws {FileOperationError} If file read fails
* @throws {CsvFormatError} If CSV is malformed
* @category 1-Core API
* @example
* const csvToJson = require('convert-csv-to-json');
* const data = await csvToJson.getJsonFromCsvAsync('resource/input.csv');
* console.log(data);
*/
const csvToJsonAsync = require('./src/csvToJsonAsync');
/**
* Parse CSV from a Readable stream and return parsed data as JSON array
* Processes data in chunks for memory-efficient handling of large files
* @param {object} stream - Node.js Readable stream containing CSV data
* @returns {Promise<Array<object>>} Promise resolving to array of objects representing CSV rows
* @throws {InputValidationError} If stream is invalid
* @throws {CsvFormatError} If CSV is malformed
* @category 1-Core API
* @example
* const fs = require('fs');
* const csvToJson = require('convert-csv-to-json');
* const stream = fs.createReadStream('large.csv');
* const data = await csvToJson.getJsonFromStreamAsync(stream);
* console.log(data);
*/
/**
* Parse CSV from a file path using streaming for memory-efficient processing
* @param {string} filePath - Path to the CSV file
* @returns {Promise<Array<object>>} Promise resolving to array of objects representing CSV rows
* @throws {InputValidationError} If filePath is invalid
* @throws {FileOperationError} If file cannot be read
* @throws {CsvFormatError} If CSV is malformed
* @category 1-Core API
* @example
* const csvToJson = require('convert-csv-to-json');
* const data = await csvToJson.getJsonFromFileStreamingAsync('large.csv');
* console.log(data);
*/
// Re-export all async API methods
Object.assign(exports, {
getJsonFromCsvAsync: function(input, options) {
return csvToJsonAsync.getJsonFromCsvAsync(input, options);
},
csvStringToJsonAsync: function(input, options) {
return csvToJsonAsync.csvStringToJsonAsync(input, options);
},
csvStringToJsonStringifiedAsync: function(input) {
return csvToJsonAsync.csvStringToJsonStringifiedAsync(input);
},
generateJsonFileFromCsvAsync: function(input, output) {
return csvToJsonAsync.generateJsonFileFromCsv(input, output);
},
getJsonFromStreamAsync: function(stream) {
return csvToJsonAsync.getJsonFromStreamAsync(stream);
},
getJsonFromFileStreamingAsync: function(filePath) {
return csvToJsonAsync.getJsonFromFileStreamingAsync(filePath);
}
});
/**
* Parse a CSV string and return as JSON array of objects (synchronous)
* @param {string} csvString - CSV content as string
* @returns {Array<object>} Array of objects representing CSV rows
* @throws {InputValidationError} If csvString is invalid
* @throws {CsvFormatError} If CSV is malformed
* @category 1-Core API
* @example
* const csvToJson = require('convert-csv-to-json');
* const rows = csvToJson.csvStringToJson('name,age\nAlice,30');
* console.log(rows); // [{ name: 'Alice', age: '30' }]
*/
exports.csvStringToJson = function(csvString) {
return csvToJson.csvStringToJson(csvString);
};
/**
* Parse CSV string and return as stringified JSON (synchronous)
* @param {string} csvString - CSV content as string
* @returns {string} JSON stringified array of objects
* @throws {InputValidationError} If csvString is invalid
* @throws {CsvFormatError} If CSV is malformed
* @throws {JsonValidationError} If JSON generation fails
* @category 1-Core API
*/
exports.csvStringToJsonStringified = function(csvString) {
if (csvString === undefined || csvString === null) {
throw new Error("csvString is not defined!!!");
}
return csvToJson.csvStringToJsonStringified(csvString);
};
exports.browser = require('./src/browserApi');