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.
Files changed (52) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +60 -341
  3. package/bin/jtcsv.js +2462 -1372
  4. package/csv-to-json.js +35 -26
  5. package/dist/jtcsv.cjs.js +807 -133
  6. package/dist/jtcsv.cjs.js.map +1 -1
  7. package/dist/jtcsv.esm.js +800 -134
  8. package/dist/jtcsv.esm.js.map +1 -1
  9. package/dist/jtcsv.umd.js +807 -133
  10. package/dist/jtcsv.umd.js.map +1 -1
  11. package/errors.js +20 -0
  12. package/examples/browser-vanilla.html +37 -0
  13. package/examples/cli-batch-processing.js +38 -0
  14. package/examples/error-handling.js +324 -0
  15. package/examples/ndjson-processing.js +434 -0
  16. package/examples/react-integration.jsx +637 -0
  17. package/examples/schema-validation.js +640 -0
  18. package/examples/simple-usage.js +10 -7
  19. package/examples/typescript-example.ts +486 -0
  20. package/examples/web-workers-advanced.js +28 -0
  21. package/index.d.ts +2 -0
  22. package/json-save.js +2 -1
  23. package/json-to-csv.js +171 -131
  24. package/package.json +20 -4
  25. package/plugins/README.md +41 -467
  26. package/plugins/express-middleware/README.md +32 -274
  27. package/plugins/hono/README.md +16 -13
  28. package/plugins/nestjs/README.md +13 -11
  29. package/plugins/nextjs-api/README.md +28 -423
  30. package/plugins/nextjs-api/index.js +1 -2
  31. package/plugins/nextjs-api/route.js +1 -2
  32. package/plugins/nuxt/README.md +6 -7
  33. package/plugins/remix/README.md +9 -9
  34. package/plugins/sveltekit/README.md +8 -8
  35. package/plugins/trpc/README.md +8 -5
  36. package/src/browser/browser-functions.js +33 -3
  37. package/src/browser/csv-to-json-browser.js +269 -11
  38. package/src/browser/errors-browser.js +19 -1
  39. package/src/browser/index.js +39 -5
  40. package/src/browser/streams.js +393 -0
  41. package/src/browser/workers/csv-parser.worker.js +20 -2
  42. package/src/browser/workers/worker-pool.js +507 -447
  43. package/src/core/plugin-system.js +4 -0
  44. package/src/engines/fast-path-engine.js +31 -23
  45. package/src/errors.js +26 -0
  46. package/src/formats/ndjson-parser.js +54 -5
  47. package/src/formats/tsv-parser.js +4 -1
  48. package/src/utils/schema-validator.js +594 -0
  49. package/src/utils/transform-loader.js +205 -0
  50. package/src/web-server/index.js +683 -0
  51. package/stream-csv-to-json.js +16 -87
  52. 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
- const { trim = true, parseNumbers = false, parseBooleans = false } = options;
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
- if (!isNaN(num)) {
135
- return num;
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
- throw new ParsingError(error.message, error.lineNumber);
348
- }
349
- throw error;
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
- async function* csvToJsonIterator(csv, options = {}) {
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
- throw new ParsingError(error.message, error.lineNumber);
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
- function createDelimiterCache(maxSize = 100) {
642
- return new DelimiterCache(maxSize);
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
- function getDelimiterCacheStats() {
650
- return globalDelimiterCache.getStats();
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
- function clearDelimiterCache() {
657
- globalDelimiterCache.clear();
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
- if (typeof module !== 'undefined' && module.exports) {
678
- module.exports.default = csvToJson;
679
- }
685
+ /* istanbul ignore next */
686
+ if (typeof module !== 'undefined' && module.exports) {
687
+ module.exports.default = csvToJson;
688
+ }