jtcsv 2.1.3 → 2.2.2
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/LICENSE +1 -1
- package/README.md +60 -341
- package/bin/jtcsv.js +2462 -1372
- package/csv-to-json.js +35 -26
- package/dist/jtcsv.cjs.js +807 -133
- package/dist/jtcsv.cjs.js.map +1 -1
- package/dist/jtcsv.esm.js +800 -134
- package/dist/jtcsv.esm.js.map +1 -1
- package/dist/jtcsv.umd.js +807 -133
- package/dist/jtcsv.umd.js.map +1 -1
- package/errors.js +20 -0
- package/examples/browser-vanilla.html +37 -0
- package/examples/cli-batch-processing.js +38 -0
- package/examples/error-handling.js +324 -0
- package/examples/ndjson-processing.js +434 -0
- package/examples/react-integration.jsx +637 -0
- package/examples/schema-validation.js +640 -0
- package/examples/simple-usage.js +10 -7
- package/examples/typescript-example.ts +486 -0
- package/examples/web-workers-advanced.js +28 -0
- package/index.d.ts +2 -0
- package/json-save.js +2 -1
- package/json-to-csv.js +171 -131
- package/package.json +20 -4
- package/plugins/README.md +41 -467
- package/plugins/express-middleware/README.md +32 -274
- package/plugins/hono/README.md +16 -13
- package/plugins/nestjs/README.md +13 -11
- package/plugins/nextjs-api/README.md +28 -423
- package/plugins/nextjs-api/index.js +1 -2
- package/plugins/nextjs-api/route.js +1 -2
- package/plugins/nuxt/README.md +6 -7
- package/plugins/remix/README.md +9 -9
- package/plugins/sveltekit/README.md +8 -8
- package/plugins/trpc/README.md +8 -5
- package/src/browser/browser-functions.js +33 -3
- package/src/browser/csv-to-json-browser.js +269 -11
- package/src/browser/errors-browser.js +19 -1
- package/src/browser/index.js +39 -5
- package/src/browser/streams.js +393 -0
- package/src/browser/workers/csv-parser.worker.js +20 -2
- package/src/browser/workers/worker-pool.js +507 -447
- package/src/core/plugin-system.js +4 -0
- package/src/engines/fast-path-engine.js +31 -23
- package/src/errors.js +26 -0
- package/src/formats/ndjson-parser.js +54 -5
- package/src/formats/tsv-parser.js +4 -1
- package/src/utils/schema-validator.js +594 -0
- package/src/utils/transform-loader.js +205 -0
- package/src/web-server/index.js +683 -0
- package/stream-csv-to-json.js +16 -87
- package/stream-json-to-csv.js +18 -86
package/csv-to-json.js
CHANGED
|
@@ -114,8 +114,9 @@ function validateCsvInput(csv, options) {
|
|
|
114
114
|
* Parses a value based on options
|
|
115
115
|
* @private
|
|
116
116
|
*/
|
|
117
|
-
function parseCsvValue(value, options) {
|
|
118
|
-
|
|
117
|
+
function parseCsvValue(value, options) {
|
|
118
|
+
/* istanbul ignore next */
|
|
119
|
+
const { trim = true, parseNumbers = false, parseBooleans = false } = options;
|
|
119
120
|
|
|
120
121
|
let result = value;
|
|
121
122
|
|
|
@@ -131,10 +132,11 @@ function parseCsvValue(value, options) {
|
|
|
131
132
|
// Parse numbers
|
|
132
133
|
if (parseNumbers && /^-?\d+(\.\d+)?$/.test(result)) {
|
|
133
134
|
const num = parseFloat(result);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
/* istanbul ignore next */
|
|
136
|
+
if (!isNaN(num)) {
|
|
137
|
+
return num;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
138
140
|
|
|
139
141
|
// Parse booleans
|
|
140
142
|
if (parseBooleans) {
|
|
@@ -342,12 +344,13 @@ function csvToJson(csv, options = {}) {
|
|
|
342
344
|
console.warn(`[jtcsv] Line ${totalRows}: ${fields.length - headers.length} extra fields ignored`);
|
|
343
345
|
}
|
|
344
346
|
});
|
|
345
|
-
} catch (error) {
|
|
346
|
-
if (error && error.code === 'FAST_PATH_UNCLOSED_QUOTES') {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
347
|
+
} catch (error) {
|
|
348
|
+
if (error && error.code === 'FAST_PATH_UNCLOSED_QUOTES') {
|
|
349
|
+
const lineInfo = error.lineNumber ? ` at line ${error.lineNumber}` : '';
|
|
350
|
+
throw new ParsingError(`Unclosed quotes in CSV${lineInfo}`, error.lineNumber);
|
|
351
|
+
}
|
|
352
|
+
throw error;
|
|
353
|
+
}
|
|
351
354
|
|
|
352
355
|
if (!headers) {
|
|
353
356
|
return [];
|
|
@@ -371,7 +374,8 @@ function csvToJson(csv, options = {}) {
|
|
|
371
374
|
}, 'PARSE_FAILED', { function: 'csvToJson' });
|
|
372
375
|
}
|
|
373
376
|
|
|
374
|
-
|
|
377
|
+
/* istanbul ignore next */
|
|
378
|
+
async function* csvToJsonIterator(csv, options = {}) {
|
|
375
379
|
validateCsvInput(csv, options);
|
|
376
380
|
|
|
377
381
|
const opts = options && typeof options === 'object' ? options : {};
|
|
@@ -498,7 +502,8 @@ async function* csvToJsonIterator(csv, options = {}) {
|
|
|
498
502
|
}
|
|
499
503
|
} catch (error) {
|
|
500
504
|
if (error && error.code === 'FAST_PATH_UNCLOSED_QUOTES') {
|
|
501
|
-
|
|
505
|
+
const lineInfo = error.lineNumber ? ` at line ${error.lineNumber}` : '';
|
|
506
|
+
throw new ParsingError(`Unclosed quotes in CSV${lineInfo}`, error.lineNumber);
|
|
502
507
|
}
|
|
503
508
|
throw error;
|
|
504
509
|
}
|
|
@@ -638,24 +643,27 @@ function createTransformHooks() {
|
|
|
638
643
|
* @param {number} maxSize - Maximum cache size (default: 100)
|
|
639
644
|
* @returns {DelimiterCache} New DelimiterCache instance
|
|
640
645
|
*/
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
646
|
+
/* istanbul ignore next */
|
|
647
|
+
function createDelimiterCache(maxSize = 100) {
|
|
648
|
+
return new DelimiterCache(maxSize);
|
|
649
|
+
}
|
|
644
650
|
|
|
645
651
|
/**
|
|
646
652
|
* Gets statistics from the global delimiter cache
|
|
647
653
|
* @returns {Object} Cache statistics
|
|
648
654
|
*/
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
655
|
+
/* istanbul ignore next */
|
|
656
|
+
function getDelimiterCacheStats() {
|
|
657
|
+
return globalDelimiterCache.getStats();
|
|
658
|
+
}
|
|
652
659
|
|
|
653
660
|
/**
|
|
654
661
|
* Clears the global delimiter cache
|
|
655
662
|
*/
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
663
|
+
/* istanbul ignore next */
|
|
664
|
+
function clearDelimiterCache() {
|
|
665
|
+
globalDelimiterCache.clear();
|
|
666
|
+
}
|
|
659
667
|
|
|
660
668
|
// Export the functions
|
|
661
669
|
module.exports = {
|
|
@@ -674,6 +682,7 @@ module.exports = {
|
|
|
674
682
|
};
|
|
675
683
|
|
|
676
684
|
// For ES6 module compatibility
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
685
|
+
/* istanbul ignore next */
|
|
686
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
687
|
+
module.exports.default = csvToJson;
|
|
688
|
+
}
|