jtcsv 2.2.8 → 3.1.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.
Files changed (246) hide show
  1. package/README.md +204 -115
  2. package/bin/jtcsv.ts +2612 -0
  3. package/browser.d.ts +142 -0
  4. package/dist/benchmark.js +446 -0
  5. package/dist/benchmark.js.map +1 -0
  6. package/dist/bin/jtcsv.js +1940 -0
  7. package/dist/bin/jtcsv.js.map +1 -0
  8. package/dist/csv-to-json.js +1262 -0
  9. package/dist/csv-to-json.js.map +1 -0
  10. package/dist/errors.js +291 -0
  11. package/dist/errors.js.map +1 -0
  12. package/dist/eslint.config.js +147 -0
  13. package/dist/eslint.config.js.map +1 -0
  14. package/dist/index-core.js +95 -0
  15. package/dist/index-core.js.map +1 -0
  16. package/dist/index.js +93 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/json-save.js +229 -0
  19. package/dist/json-save.js.map +1 -0
  20. package/dist/json-to-csv.js +576 -0
  21. package/dist/json-to-csv.js.map +1 -0
  22. package/dist/jtcsv-core.cjs.js +1736 -0
  23. package/dist/jtcsv-core.cjs.js.map +1 -0
  24. package/dist/jtcsv-core.esm.js +1708 -0
  25. package/dist/jtcsv-core.esm.js.map +1 -0
  26. package/dist/jtcsv-core.umd.js +1742 -0
  27. package/dist/jtcsv-core.umd.js.map +1 -0
  28. package/dist/jtcsv-full.cjs.js +2241 -0
  29. package/dist/jtcsv-full.cjs.js.map +1 -0
  30. package/dist/jtcsv-full.esm.js +2209 -0
  31. package/dist/jtcsv-full.esm.js.map +1 -0
  32. package/dist/jtcsv-full.umd.js +2247 -0
  33. package/dist/jtcsv-full.umd.js.map +1 -0
  34. package/dist/jtcsv-workers.esm.js +768 -0
  35. package/dist/jtcsv-workers.esm.js.map +1 -0
  36. package/dist/jtcsv-workers.umd.js +782 -0
  37. package/dist/jtcsv-workers.umd.js.map +1 -0
  38. package/dist/jtcsv.cjs.js +1996 -2048
  39. package/dist/jtcsv.cjs.js.map +1 -1
  40. package/dist/jtcsv.esm.js +1992 -2048
  41. package/dist/jtcsv.esm.js.map +1 -1
  42. package/dist/jtcsv.umd.js +2157 -2209
  43. package/dist/jtcsv.umd.js.map +1 -1
  44. package/dist/plugins/express-middleware/index.js +350 -0
  45. package/dist/plugins/express-middleware/index.js.map +1 -0
  46. package/dist/plugins/fastify-plugin/index.js +315 -0
  47. package/dist/plugins/fastify-plugin/index.js.map +1 -0
  48. package/dist/plugins/hono/index.js +111 -0
  49. package/dist/plugins/hono/index.js.map +1 -0
  50. package/dist/plugins/nestjs/index.js +112 -0
  51. package/dist/plugins/nestjs/index.js.map +1 -0
  52. package/dist/plugins/nuxt/index.js +53 -0
  53. package/dist/plugins/nuxt/index.js.map +1 -0
  54. package/dist/plugins/remix/index.js +133 -0
  55. package/dist/plugins/remix/index.js.map +1 -0
  56. package/dist/plugins/sveltekit/index.js +155 -0
  57. package/dist/plugins/sveltekit/index.js.map +1 -0
  58. package/dist/plugins/trpc/index.js +136 -0
  59. package/dist/plugins/trpc/index.js.map +1 -0
  60. package/dist/run-demo.js +49 -0
  61. package/dist/run-demo.js.map +1 -0
  62. package/dist/src/browser/browser-functions.js +193 -0
  63. package/dist/src/browser/browser-functions.js.map +1 -0
  64. package/dist/src/browser/core.js +123 -0
  65. package/dist/src/browser/core.js.map +1 -0
  66. package/dist/src/browser/csv-to-json-browser.js +353 -0
  67. package/dist/src/browser/csv-to-json-browser.js.map +1 -0
  68. package/dist/src/browser/errors-browser.js +219 -0
  69. package/dist/src/browser/errors-browser.js.map +1 -0
  70. package/dist/src/browser/extensions/plugins.js +106 -0
  71. package/dist/src/browser/extensions/plugins.js.map +1 -0
  72. package/dist/src/browser/extensions/workers.js +66 -0
  73. package/dist/src/browser/extensions/workers.js.map +1 -0
  74. package/dist/src/browser/index.js +140 -0
  75. package/dist/src/browser/index.js.map +1 -0
  76. package/dist/src/browser/json-to-csv-browser.js +225 -0
  77. package/dist/src/browser/json-to-csv-browser.js.map +1 -0
  78. package/dist/src/browser/streams.js +340 -0
  79. package/dist/src/browser/streams.js.map +1 -0
  80. package/dist/src/browser/workers/csv-parser.worker.js +264 -0
  81. package/dist/src/browser/workers/csv-parser.worker.js.map +1 -0
  82. package/dist/src/browser/workers/worker-pool.js +338 -0
  83. package/dist/src/browser/workers/worker-pool.js.map +1 -0
  84. package/dist/src/core/delimiter-cache.js +196 -0
  85. package/dist/src/core/delimiter-cache.js.map +1 -0
  86. package/dist/src/core/node-optimizations.js +279 -0
  87. package/dist/src/core/node-optimizations.js.map +1 -0
  88. package/dist/src/core/plugin-system.js +399 -0
  89. package/dist/src/core/plugin-system.js.map +1 -0
  90. package/dist/src/core/transform-hooks.js +348 -0
  91. package/dist/src/core/transform-hooks.js.map +1 -0
  92. package/dist/src/engines/fast-path-engine-new.js +262 -0
  93. package/dist/src/engines/fast-path-engine-new.js.map +1 -0
  94. package/dist/src/engines/fast-path-engine.js +671 -0
  95. package/dist/src/engines/fast-path-engine.js.map +1 -0
  96. package/dist/src/errors.js +18 -0
  97. package/dist/src/errors.js.map +1 -0
  98. package/dist/src/formats/ndjson-parser.js +332 -0
  99. package/dist/src/formats/ndjson-parser.js.map +1 -0
  100. package/dist/src/formats/tsv-parser.js +230 -0
  101. package/dist/src/formats/tsv-parser.js.map +1 -0
  102. package/dist/src/index-with-plugins.js +259 -0
  103. package/dist/src/index-with-plugins.js.map +1 -0
  104. package/dist/src/types/index.js +3 -0
  105. package/dist/src/types/index.js.map +1 -0
  106. package/dist/src/utils/bom-utils.js +267 -0
  107. package/dist/src/utils/bom-utils.js.map +1 -0
  108. package/dist/src/utils/encoding-support.js +77 -0
  109. package/dist/src/utils/encoding-support.js.map +1 -0
  110. package/dist/src/utils/schema-validator.js +609 -0
  111. package/dist/src/utils/schema-validator.js.map +1 -0
  112. package/dist/src/utils/transform-loader.js +281 -0
  113. package/dist/src/utils/transform-loader.js.map +1 -0
  114. package/dist/src/utils/validators.js +40 -0
  115. package/dist/src/utils/validators.js.map +1 -0
  116. package/dist/src/utils/zod-adapter.js +144 -0
  117. package/dist/src/utils/zod-adapter.js.map +1 -0
  118. package/dist/src/web-server/index.js +648 -0
  119. package/dist/src/web-server/index.js.map +1 -0
  120. package/dist/src/workers/csv-multithreaded.js +211 -0
  121. package/dist/src/workers/csv-multithreaded.js.map +1 -0
  122. package/dist/src/workers/csv-parser.worker.js +179 -0
  123. package/dist/src/workers/csv-parser.worker.js.map +1 -0
  124. package/dist/src/workers/worker-pool.js +228 -0
  125. package/dist/src/workers/worker-pool.js.map +1 -0
  126. package/dist/stream-csv-to-json.js +665 -0
  127. package/dist/stream-csv-to-json.js.map +1 -0
  128. package/dist/stream-json-to-csv.js +389 -0
  129. package/dist/stream-json-to-csv.js.map +1 -0
  130. package/examples/advanced/conditional-transformations.ts +446 -0
  131. package/examples/advanced/csv-parser.worker.ts +89 -0
  132. package/examples/advanced/nested-objects-example.ts +306 -0
  133. package/examples/advanced/performance-optimization.ts +504 -0
  134. package/examples/advanced/run-demo-server.ts +116 -0
  135. package/examples/advanced/web-worker-usage.html +874 -0
  136. package/examples/async-multithreaded-example.ts +335 -0
  137. package/examples/cli-advanced-usage.md +290 -0
  138. package/examples/{cli-batch-processing.js → cli-batch-processing.ts} +38 -38
  139. package/examples/{cli-tool.js → cli-tool.ts} +5 -8
  140. package/examples/{error-handling.js → error-handling.ts} +356 -324
  141. package/examples/{express-api.js → express-api.ts} +161 -164
  142. package/examples/{large-dataset-example.js → large-dataset-example.ts} +201 -182
  143. package/examples/{ndjson-processing.js → ndjson-processing.ts} +456 -434
  144. package/examples/{plugin-excel-exporter.js → plugin-excel-exporter.ts} +6 -7
  145. package/examples/react-integration.tsx +637 -0
  146. package/examples/{schema-validation.js → schema-validation.ts} +2 -2
  147. package/examples/simple-usage.ts +194 -0
  148. package/examples/{streaming-example.js → streaming-example.ts} +12 -12
  149. package/index.d.ts +187 -18
  150. package/package.json +75 -81
  151. package/plugins.d.ts +37 -0
  152. package/schema.d.ts +103 -0
  153. package/src/browser/browser-functions.ts +402 -0
  154. package/src/browser/core.ts +152 -0
  155. package/src/browser/csv-to-json-browser.d.ts +3 -0
  156. package/src/browser/csv-to-json-browser.ts +494 -0
  157. package/src/browser/{errors-browser.js → errors-browser.ts} +305 -197
  158. package/src/browser/extensions/plugins.ts +93 -0
  159. package/src/browser/extensions/workers.ts +39 -0
  160. package/src/browser/globals.d.ts +5 -0
  161. package/src/browser/index.ts +192 -0
  162. package/src/browser/json-to-csv-browser.d.ts +3 -0
  163. package/src/browser/json-to-csv-browser.ts +338 -0
  164. package/src/browser/streams.ts +403 -0
  165. package/src/browser/workers/{csv-parser.worker.js → csv-parser.worker.ts} +3 -3
  166. package/src/browser/workers/{worker-pool.js → worker-pool.ts} +51 -30
  167. package/src/core/delimiter-cache.ts +320 -0
  168. package/src/core/{node-optimizations.js → node-optimizations.ts} +448 -407
  169. package/src/core/plugin-system.ts +588 -0
  170. package/src/core/transform-hooks.ts +566 -0
  171. package/src/engines/{fast-path-engine-new.js → fast-path-engine-new.ts} +11 -2
  172. package/src/engines/{fast-path-engine.js → fast-path-engine.ts} +79 -53
  173. package/src/errors.ts +1 -0
  174. package/src/formats/{ndjson-parser.js → ndjson-parser.ts} +24 -16
  175. package/src/formats/{tsv-parser.js → tsv-parser.ts} +18 -17
  176. package/src/{index-with-plugins.js → index-with-plugins.ts} +381 -357
  177. package/src/types/index.ts +275 -0
  178. package/src/utils/bom-utils.ts +373 -0
  179. package/src/utils/encoding-support.ts +155 -0
  180. package/src/utils/{schema-validator.js → schema-validator.ts} +814 -589
  181. package/src/utils/transform-loader.ts +389 -0
  182. package/src/utils/validators.ts +35 -0
  183. package/src/utils/zod-adapter.ts +280 -0
  184. package/src/web-server/{index.js → index.ts} +19 -19
  185. package/src/workers/csv-multithreaded.ts +310 -0
  186. package/src/workers/csv-parser.worker.ts +227 -0
  187. package/src/workers/worker-pool.ts +409 -0
  188. package/bin/jtcsv.js +0 -2462
  189. package/csv-to-json.js +0 -688
  190. package/errors.js +0 -208
  191. package/examples/simple-usage.js +0 -282
  192. package/index.js +0 -68
  193. package/json-save.js +0 -254
  194. package/json-to-csv.js +0 -526
  195. package/plugins/README.md +0 -91
  196. package/plugins/express-middleware/README.md +0 -64
  197. package/plugins/express-middleware/example.js +0 -136
  198. package/plugins/express-middleware/index.d.ts +0 -114
  199. package/plugins/express-middleware/index.js +0 -360
  200. package/plugins/express-middleware/package.json +0 -52
  201. package/plugins/fastify-plugin/index.js +0 -406
  202. package/plugins/fastify-plugin/package.json +0 -55
  203. package/plugins/hono/README.md +0 -28
  204. package/plugins/hono/index.d.ts +0 -12
  205. package/plugins/hono/index.js +0 -36
  206. package/plugins/hono/package.json +0 -35
  207. package/plugins/nestjs/README.md +0 -35
  208. package/plugins/nestjs/index.d.ts +0 -25
  209. package/plugins/nestjs/index.js +0 -77
  210. package/plugins/nestjs/package.json +0 -37
  211. package/plugins/nextjs-api/README.md +0 -57
  212. package/plugins/nextjs-api/examples/ConverterComponent.jsx +0 -386
  213. package/plugins/nextjs-api/examples/api-convert.js +0 -69
  214. package/plugins/nextjs-api/index.js +0 -387
  215. package/plugins/nextjs-api/package.json +0 -63
  216. package/plugins/nextjs-api/route.js +0 -371
  217. package/plugins/nuxt/README.md +0 -24
  218. package/plugins/nuxt/index.js +0 -21
  219. package/plugins/nuxt/package.json +0 -35
  220. package/plugins/nuxt/runtime/composables/useJtcsv.js +0 -6
  221. package/plugins/nuxt/runtime/plugin.js +0 -6
  222. package/plugins/remix/README.md +0 -26
  223. package/plugins/remix/index.d.ts +0 -16
  224. package/plugins/remix/index.js +0 -62
  225. package/plugins/remix/package.json +0 -35
  226. package/plugins/sveltekit/README.md +0 -28
  227. package/plugins/sveltekit/index.d.ts +0 -17
  228. package/plugins/sveltekit/index.js +0 -54
  229. package/plugins/sveltekit/package.json +0 -33
  230. package/plugins/trpc/README.md +0 -25
  231. package/plugins/trpc/index.d.ts +0 -7
  232. package/plugins/trpc/index.js +0 -32
  233. package/plugins/trpc/package.json +0 -34
  234. package/src/browser/browser-functions.js +0 -219
  235. package/src/browser/csv-to-json-browser.js +0 -700
  236. package/src/browser/index.js +0 -113
  237. package/src/browser/json-to-csv-browser.js +0 -309
  238. package/src/browser/streams.js +0 -393
  239. package/src/core/delimiter-cache.js +0 -186
  240. package/src/core/plugin-system.js +0 -476
  241. package/src/core/transform-hooks.js +0 -350
  242. package/src/errors.js +0 -26
  243. package/src/utils/transform-loader.js +0 -205
  244. package/stream-csv-to-json.js +0 -542
  245. package/stream-json-to-csv.js +0 -464
  246. /package/examples/{web-workers-advanced.js → web-workers-advanced.ts} +0 -0
package/dist/jtcsv.cjs.js CHANGED
@@ -2,1496 +2,1340 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
6
5
  // Система ошибок для браузерной версии jtcsv
7
6
  // Адаптирована для работы без Node.js специфичных API
8
-
9
7
  /**
10
8
  * Базовый класс ошибки jtcsv
11
9
  */
12
10
  class JTCSVError extends Error {
13
- constructor(message, code = 'JTCSV_ERROR', details = {}) {
14
- super(message);
15
- this.name = 'JTCSVError';
16
- this.code = code;
17
- this.details = details;
18
-
19
- // Сохранение stack trace
20
- if (Error.captureStackTrace) {
21
- Error.captureStackTrace(this, JTCSVError);
11
+ constructor(message, code = 'JTCSV_ERROR', details = {}) {
12
+ super(message);
13
+ this.name = 'JTCSVError';
14
+ this.code = code;
15
+ this.details = details;
16
+ this.hint = details.hint;
17
+ this.docs = details.docs;
18
+ this.context = details.context;
19
+ // Сохранение stack trace
20
+ if (Error.captureStackTrace) {
21
+ Error.captureStackTrace(this, JTCSVError);
22
+ }
22
23
  }
23
- }
24
24
  }
25
-
26
25
  /**
27
26
  * Ошибка валидации
28
27
  */
29
28
  class ValidationError extends JTCSVError {
30
- constructor(message, details = {}) {
31
- super(message, 'VALIDATION_ERROR', details);
32
- this.name = 'ValidationError';
33
- }
29
+ constructor(message, details = {}) {
30
+ super(message, 'VALIDATION_ERROR', details);
31
+ this.name = 'ValidationError';
32
+ }
34
33
  }
35
-
36
34
  /**
37
35
  * Ошибка безопасности
38
36
  */
39
37
  class SecurityError extends JTCSVError {
40
- constructor(message, details = {}) {
41
- super(message, 'SECURITY_ERROR', details);
42
- this.name = 'SecurityError';
43
- }
38
+ constructor(message, details = {}) {
39
+ super(message, 'SECURITY_ERROR', details);
40
+ this.name = 'SecurityError';
41
+ }
44
42
  }
45
-
46
43
  /**
47
44
  * Ошибка файловой системы (адаптирована для браузера)
48
45
  */
49
46
  class FileSystemError extends JTCSVError {
50
- constructor(message, originalError = null, details = {}) {
51
- super(message, 'FILE_SYSTEM_ERROR', {
52
- ...details,
53
- originalError
54
- });
55
- this.name = 'FileSystemError';
56
- if (originalError && originalError.code) {
57
- this.code = originalError.code;
47
+ constructor(message, originalError, details = {}) {
48
+ super(message, 'FILE_SYSTEM_ERROR', { ...details, originalError });
49
+ this.name = 'FileSystemError';
50
+ if (originalError && originalError.code) {
51
+ this.code = originalError.code;
52
+ }
58
53
  }
59
- }
60
54
  }
61
-
62
55
  /**
63
56
  * Ошибка парсинга
64
57
  */
65
58
  class ParsingError extends JTCSVError {
66
- constructor(message, lineNumber = null, details = {}) {
67
- super(message, 'PARSING_ERROR', {
68
- ...details,
69
- lineNumber
70
- });
71
- this.name = 'ParsingError';
72
- this.lineNumber = lineNumber;
73
- }
59
+ constructor(message, lineNumber, details = {}) {
60
+ super(message, 'PARSING_ERROR', { ...details, lineNumber });
61
+ this.name = 'ParsingError';
62
+ this.lineNumber = lineNumber;
63
+ }
74
64
  }
75
-
76
65
  /**
77
66
  * Ошибка превышения лимита
78
67
  */
79
68
  class LimitError extends JTCSVError {
80
- constructor(message, limit, actual, details = {}) {
81
- super(message, 'LIMIT_ERROR', {
82
- ...details,
83
- limit,
84
- actual
85
- });
86
- this.name = 'LimitError';
87
- this.limit = limit;
88
- this.actual = actual;
89
- }
69
+ constructor(message, limit, actual, details = {}) {
70
+ super(message, 'LIMIT_ERROR', { ...details, limit, actual });
71
+ this.name = 'LimitError';
72
+ this.limit = limit;
73
+ this.actual = actual;
74
+ }
90
75
  }
91
-
92
76
  /**
93
77
  * Ошибка конфигурации
94
78
  */
95
79
  class ConfigurationError extends JTCSVError {
96
- constructor(message, details = {}) {
97
- super(message, 'CONFIGURATION_ERROR', details);
98
- this.name = 'ConfigurationError';
99
- }
80
+ constructor(message, details = {}) {
81
+ super(message, 'CONFIGURATION_ERROR', details);
82
+ this.name = 'ConfigurationError';
83
+ }
100
84
  }
85
+ /**
86
+ * Коды ошибок
87
+ */
101
88
  const ERROR_CODES = {
102
- JTCSV_ERROR: 'JTCSV_ERROR',
103
- VALIDATION_ERROR: 'VALIDATION_ERROR',
104
- SECURITY_ERROR: 'SECURITY_ERROR',
105
- FILE_SYSTEM_ERROR: 'FILE_SYSTEM_ERROR',
106
- PARSING_ERROR: 'PARSING_ERROR',
107
- LIMIT_ERROR: 'LIMIT_ERROR',
108
- CONFIGURATION_ERROR: 'CONFIGURATION_ERROR',
109
- INVALID_INPUT: 'INVALID_INPUT',
110
- SECURITY_VIOLATION: 'SECURITY_VIOLATION',
111
- FILE_NOT_FOUND: 'FILE_NOT_FOUND',
112
- PARSE_FAILED: 'PARSE_FAILED',
113
- SIZE_LIMIT: 'SIZE_LIMIT',
114
- INVALID_CONFIG: 'INVALID_CONFIG',
115
- UNKNOWN_ERROR: 'UNKNOWN_ERROR'
89
+ JTCSV_ERROR: 'JTCSV_ERROR',
90
+ VALIDATION_ERROR: 'VALIDATION_ERROR',
91
+ SECURITY_ERROR: 'SECURITY_ERROR',
92
+ FILE_SYSTEM_ERROR: 'FILE_SYSTEM_ERROR',
93
+ PARSING_ERROR: 'PARSING_ERROR',
94
+ LIMIT_ERROR: 'LIMIT_ERROR',
95
+ CONFIGURATION_ERROR: 'CONFIGURATION_ERROR',
96
+ INVALID_INPUT: 'INVALID_INPUT',
97
+ SECURITY_VIOLATION: 'SECURITY_VIOLATION',
98
+ FILE_NOT_FOUND: 'FILE_NOT_FOUND',
99
+ PARSE_FAILED: 'PARSE_FAILED',
100
+ SIZE_LIMIT: 'SIZE_LIMIT',
101
+ INVALID_CONFIG: 'INVALID_CONFIG',
102
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR'
116
103
  };
117
-
118
104
  /**
119
105
  * Безопасное выполнение функции с обработкой ошибок
120
- *
121
- * @param {Function} fn - Функция для выполнения
122
- * @param {string} errorCode - Код ошибки по умолчанию
123
- * @param {Object} errorDetails - Детали ошибки
124
- * @returns {*} Результат выполнения функции
106
+ *
107
+ * @param fn - Функция для выполнения
108
+ * @param errorCode - Код ошибки по умолчанию
109
+ * @param errorDetails - Детали ошибки
110
+ * @returns Результат выполнения функции
125
111
  */
126
112
  function safeExecute(fn, errorCode = 'UNKNOWN_ERROR', errorDetails = {}) {
127
- try {
128
- if (typeof fn === 'function') {
129
- return fn();
130
- }
131
- throw new ValidationError('Function expected');
132
- } catch (error) {
133
- // Если ошибка уже является JTCSVError, перебросить её
134
- if (error instanceof JTCSVError) {
135
- throw error;
136
- }
137
-
138
- // Определить тип ошибки на основе сообщения или кода
139
- let enhancedError;
140
- const errorMessage = error.message || String(error);
141
- if (errorMessage.includes('validation') || errorMessage.includes('Validation')) {
142
- enhancedError = new ValidationError(errorMessage, {
143
- ...errorDetails,
144
- originalError: error
145
- });
146
- } else if (errorMessage.includes('security') || errorMessage.includes('Security')) {
147
- enhancedError = new SecurityError(errorMessage, {
148
- ...errorDetails,
149
- originalError: error
150
- });
151
- } else if (errorMessage.includes('parsing') || errorMessage.includes('Parsing')) {
152
- enhancedError = new ParsingError(errorMessage, null, {
153
- ...errorDetails,
154
- originalError: error
155
- });
156
- } else if (errorMessage.includes('limit') || errorMessage.includes('Limit')) {
157
- enhancedError = new LimitError(errorMessage, null, null, {
158
- ...errorDetails,
159
- originalError: error
160
- });
161
- } else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
162
- enhancedError = new ConfigurationError(errorMessage, {
163
- ...errorDetails,
164
- originalError: error
165
- });
166
- } else if (errorMessage.includes('file') || errorMessage.includes('File')) {
167
- enhancedError = new FileSystemError(errorMessage, error, errorDetails);
168
- } else {
169
- // Общая ошибка
170
- enhancedError = new JTCSVError(errorMessage, errorCode, {
171
- ...errorDetails,
172
- originalError: error
173
- });
113
+ try {
114
+ if (typeof fn === 'function') {
115
+ return fn();
116
+ }
117
+ throw new ValidationError('Function expected');
174
118
  }
175
-
176
- // Сохранить оригинальный stack trace если возможно
177
- if (error.stack) {
178
- enhancedError.stack = error.stack;
119
+ catch (error) {
120
+ // Если ошибка уже является JTCSVError, перебросить её
121
+ if (error instanceof JTCSVError) {
122
+ throw error;
123
+ }
124
+ // Определить тип ошибки на основе сообщения или кода
125
+ let enhancedError;
126
+ const errorMessage = error.message || String(error);
127
+ if (errorMessage.includes('validation') || errorMessage.includes('Validation')) {
128
+ enhancedError = new ValidationError(errorMessage, { ...errorDetails, originalError: error });
129
+ }
130
+ else if (errorMessage.includes('security') || errorMessage.includes('Security')) {
131
+ enhancedError = new SecurityError(errorMessage, { ...errorDetails, originalError: error });
132
+ }
133
+ else if (errorMessage.includes('parsing') || errorMessage.includes('Parsing')) {
134
+ enhancedError = new ParsingError(errorMessage, undefined, { ...errorDetails, originalError: error });
135
+ }
136
+ else if (errorMessage.includes('limit') || errorMessage.includes('Limit')) {
137
+ enhancedError = new LimitError(errorMessage, null, null, { ...errorDetails, originalError: error });
138
+ }
139
+ else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
140
+ enhancedError = new ConfigurationError(errorMessage, { ...errorDetails, originalError: error });
141
+ }
142
+ else if (errorMessage.includes('file') || errorMessage.includes('File')) {
143
+ enhancedError = new FileSystemError(errorMessage, error, errorDetails);
144
+ }
145
+ else {
146
+ // Общая ошибка
147
+ enhancedError = new JTCSVError(errorMessage, errorCode, { ...errorDetails, originalError: error });
148
+ }
149
+ // Сохранить оригинальный stack trace если возможно
150
+ if (error.stack) {
151
+ enhancedError.stack = error.stack;
152
+ }
153
+ throw enhancedError;
179
154
  }
180
- throw enhancedError;
181
- }
182
155
  }
183
-
184
156
  /**
185
157
  * Асинхронная версия safeExecute
186
158
  */
187
159
  async function safeExecuteAsync(fn, errorCode = 'UNKNOWN_ERROR', errorDetails = {}) {
188
- try {
189
- if (typeof fn === 'function') {
190
- return await fn();
160
+ try {
161
+ if (typeof fn === 'function') {
162
+ return await fn();
163
+ }
164
+ throw new ValidationError('Function expected');
165
+ }
166
+ catch (error) {
167
+ // Если ошибка уже является JTCSVError, перебросить её
168
+ if (error instanceof JTCSVError) {
169
+ throw error;
170
+ }
171
+ // Определить тип ошибки
172
+ let enhancedError;
173
+ const errorMessage = error.message || String(error);
174
+ if (errorMessage.includes('validation') || errorMessage.includes('Validation')) {
175
+ enhancedError = new ValidationError(errorMessage, { ...errorDetails, originalError: error });
176
+ }
177
+ else if (errorMessage.includes('security') || errorMessage.includes('Security')) {
178
+ enhancedError = new SecurityError(errorMessage, { ...errorDetails, originalError: error });
179
+ }
180
+ else if (errorMessage.includes('parsing') || errorMessage.includes('Parsing')) {
181
+ enhancedError = new ParsingError(errorMessage, undefined, { ...errorDetails, originalError: error });
182
+ }
183
+ else if (errorMessage.includes('limit') || errorMessage.includes('Limit')) {
184
+ enhancedError = new LimitError(errorMessage, null, null, { ...errorDetails, originalError: error });
185
+ }
186
+ else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
187
+ enhancedError = new ConfigurationError(errorMessage, { ...errorDetails, originalError: error });
188
+ }
189
+ else if (errorMessage.includes('file') || errorMessage.includes('File')) {
190
+ enhancedError = new FileSystemError(errorMessage, error, errorDetails);
191
+ }
192
+ else {
193
+ enhancedError = new JTCSVError(errorMessage, errorCode, { ...errorDetails, originalError: error });
194
+ }
195
+ if (error.stack) {
196
+ enhancedError.stack = error.stack;
197
+ }
198
+ throw enhancedError;
191
199
  }
192
- throw new ValidationError('Function expected');
193
- } catch (error) {
194
- // Если ошибка уже является JTCSVError, перебросить её
200
+ }
201
+ /**
202
+ * Создать сообщение об ошибке
203
+ */
204
+ function createErrorMessage(error, includeStack = false) {
205
+ let message = error.message || 'Unknown error';
195
206
  if (error instanceof JTCSVError) {
196
- throw error;
207
+ message = `[${error.code}] ${message}`;
208
+ if (error instanceof ParsingError && error.lineNumber) {
209
+ message += ` (line ${error.lineNumber})`;
210
+ }
211
+ if (error instanceof LimitError && error.limit && error.actual) {
212
+ message += ` (limit: ${error.limit}, actual: ${error.actual})`;
213
+ }
214
+ if (error.hint) {
215
+ message += `\nHint: ${error.hint}`;
216
+ }
217
+ if (error.docs) {
218
+ message += `\nDocs: ${error.docs}`;
219
+ }
197
220
  }
198
-
199
- // Определить тип ошибки
200
- let enhancedError;
201
- const errorMessage = error.message || String(error);
202
- if (errorMessage.includes('validation') || errorMessage.includes('Validation')) {
203
- enhancedError = new ValidationError(errorMessage, {
204
- ...errorDetails,
205
- originalError: error
206
- });
207
- } else if (errorMessage.includes('security') || errorMessage.includes('Security')) {
208
- enhancedError = new SecurityError(errorMessage, {
209
- ...errorDetails,
210
- originalError: error
211
- });
212
- } else if (errorMessage.includes('parsing') || errorMessage.includes('Parsing')) {
213
- enhancedError = new ParsingError(errorMessage, null, {
214
- ...errorDetails,
215
- originalError: error
216
- });
217
- } else if (errorMessage.includes('limit') || errorMessage.includes('Limit')) {
218
- enhancedError = new LimitError(errorMessage, null, null, {
219
- ...errorDetails,
220
- originalError: error
221
- });
222
- } else if (errorMessage.includes('configuration') || errorMessage.includes('Configuration')) {
223
- enhancedError = new ConfigurationError(errorMessage, {
224
- ...errorDetails,
225
- originalError: error
226
- });
227
- } else if (errorMessage.includes('file') || errorMessage.includes('File')) {
228
- enhancedError = new FileSystemError(errorMessage, error, errorDetails);
229
- } else {
230
- enhancedError = new JTCSVError(errorMessage, errorCode, {
231
- ...errorDetails,
232
- originalError: error
233
- });
234
- }
235
- if (error.stack) {
236
- enhancedError.stack = error.stack;
237
- }
238
- throw enhancedError;
239
- }
221
+ if (includeStack && error.stack) {
222
+ message += `\n${error.stack}`;
223
+ }
224
+ return message;
225
+ }
226
+ /**
227
+ * Обработка ошибки
228
+ */
229
+ function handleError(error, options = {}) {
230
+ const { log = true, throw: shouldThrow = false, format = true } = options;
231
+ const message = format ? createErrorMessage(error) : error.message;
232
+ if (log) {
233
+ console.error(`[jtcsv] ${message}`);
234
+ if (error instanceof JTCSVError && error.details) {
235
+ console.error('Error details:', error.details);
236
+ }
237
+ }
238
+ if (shouldThrow) {
239
+ throw error;
240
+ }
241
+ return message;
240
242
  }
241
-
242
243
  // Экспорт для Node.js совместимости
243
244
  if (typeof module !== 'undefined' && module.exports) {
244
- module.exports = {
245
- JTCSVError,
246
- ValidationError,
247
- SecurityError,
248
- FileSystemError,
249
- ParsingError,
250
- LimitError,
251
- ConfigurationError,
252
- ERROR_CODES,
253
- safeExecute,
254
- safeExecuteAsync
255
- };
245
+ module.exports = {
246
+ JTCSVError,
247
+ ValidationError,
248
+ SecurityError,
249
+ FileSystemError,
250
+ ParsingError,
251
+ LimitError,
252
+ ConfigurationError,
253
+ ERROR_CODES,
254
+ safeExecute,
255
+ safeExecuteAsync,
256
+ createErrorMessage,
257
+ handleError
258
+ };
256
259
  }
257
260
 
258
261
  // Браузерная версия JSON to CSV конвертера
259
262
  // Адаптирована для работы в браузере без Node.js API
260
-
261
-
262
263
  /**
263
264
  * Валидация входных данных и опций
264
265
  * @private
265
266
  */
266
267
  function validateInput(data, options) {
267
- // Validate data
268
- if (!Array.isArray(data)) {
269
- throw new ValidationError('Input data must be an array');
270
- }
271
-
272
- // Validate options
273
- if (options && typeof options !== 'object') {
274
- throw new ConfigurationError('Options must be an object');
275
- }
276
-
277
- // Validate delimiter
278
- if (options !== null && options !== void 0 && options.delimiter && typeof options.delimiter !== 'string') {
279
- throw new ConfigurationError('Delimiter must be a string');
280
- }
281
- if (options !== null && options !== void 0 && options.delimiter && options.delimiter.length !== 1) {
282
- throw new ConfigurationError('Delimiter must be a single character');
283
- }
284
-
285
- // Validate renameMap
286
- if (options !== null && options !== void 0 && options.renameMap && typeof options.renameMap !== 'object') {
287
- throw new ConfigurationError('renameMap must be an object');
288
- }
289
-
290
- // Validate maxRecords
291
- if (options && options.maxRecords !== undefined) {
292
- if (typeof options.maxRecords !== 'number' || options.maxRecords <= 0) {
293
- throw new ConfigurationError('maxRecords must be a positive number');
268
+ // Validate data
269
+ if (!Array.isArray(data)) {
270
+ throw new ValidationError('Input data must be an array');
294
271
  }
295
- }
296
-
297
- // Validate preventCsvInjection
298
- if ((options === null || options === void 0 ? void 0 : options.preventCsvInjection) !== undefined && typeof options.preventCsvInjection !== 'boolean') {
299
- throw new ConfigurationError('preventCsvInjection must be a boolean');
300
- }
301
-
302
- // Validate rfc4180Compliant
303
- if ((options === null || options === void 0 ? void 0 : options.rfc4180Compliant) !== undefined && typeof options.rfc4180Compliant !== 'boolean') {
304
- throw new ConfigurationError('rfc4180Compliant must be a boolean');
305
- }
306
- return true;
307
- }
308
-
309
- /**
310
- * Конвертирует JSON данные в CSV формат
311
- *
312
- * @param {Array<Object>} data - Массив объектов для конвертации в CSV
313
- * @param {Object} [options] - Опции конфигурации
314
- * @param {string} [options.delimiter=';'] - CSV разделитель
315
- * @param {boolean} [options.includeHeaders=true] - Включать ли заголовки
316
- * @param {Object} [options.renameMap={}] - Маппинг переименования заголовков
317
- * @param {Object} [options.template={}] - Шаблон для порядка колонок
318
- * @param {number} [options.maxRecords] - Максимальное количество записей
319
- * @param {boolean} [options.preventCsvInjection=true] - Защита от CSV инъекций
320
- * @param {boolean} [options.rfc4180Compliant=true] - Соответствие RFC 4180
321
- * @returns {string} CSV строка
322
- */
323
- function jsonToCsv(data, options = {}) {
324
- return safeExecute(() => {
325
- // Валидация входных данных
326
- validateInput(data, options);
327
- const opts = options && typeof options === 'object' ? options : {};
328
- const {
329
- delimiter = ';',
330
- includeHeaders = true,
331
- renameMap = {},
332
- template = {},
333
- maxRecords,
334
- preventCsvInjection = true,
335
- rfc4180Compliant = true
336
- } = opts;
337
-
338
- // Обработка пустых данных
339
- if (data.length === 0) {
340
- return '';
272
+ // Validate options
273
+ if (options && typeof options !== 'object') {
274
+ throw new ConfigurationError('Options must be an object');
341
275
  }
342
-
343
- // Предупреждение для больших наборов данных
344
- if (data.length > 1000000 && !maxRecords && process.env.NODE_ENV !== 'production') {
345
- console.warn('⚠️ Warning: Processing >1M records in memory may be slow.\n' + '💡 Consider processing data in batches or using Web Workers for large files.\n' + '📊 Current size: ' + data.length.toLocaleString() + ' records');
276
+ // Validate delimiter
277
+ if (options?.delimiter && typeof options.delimiter !== 'string') {
278
+ throw new ConfigurationError('Delimiter must be a string');
346
279
  }
347
-
348
- // Применение ограничения по количеству записей
349
- if (maxRecords && data.length > maxRecords) {
350
- throw new LimitError(`Data size exceeds maximum limit of ${maxRecords} records`, maxRecords, data.length);
280
+ if (options?.delimiter && options.delimiter.length !== 1) {
281
+ throw new ConfigurationError('Delimiter must be a single character');
351
282
  }
352
-
353
- // Получение всех уникальных ключей
354
- const allKeys = new Set();
355
- data.forEach(item => {
356
- if (!item || typeof item !== 'object') {
357
- return;
358
- }
359
- Object.keys(item).forEach(key => allKeys.add(key));
360
- });
361
- const originalKeys = Array.from(allKeys);
362
-
363
- // Применение rename map для создания заголовков
364
- const headers = originalKeys.map(key => renameMap[key] || key);
365
-
366
- // Создание обратного маппинга
367
- const reverseRenameMap = {};
368
- originalKeys.forEach((key, index) => {
369
- reverseRenameMap[headers[index]] = key;
370
- });
371
-
372
- // Применение порядка из шаблона
373
- let finalHeaders = headers;
374
- if (Object.keys(template).length > 0) {
375
- const templateHeaders = Object.keys(template).map(key => renameMap[key] || key);
376
- const extraHeaders = headers.filter(h => !templateHeaders.includes(h));
377
- finalHeaders = [...templateHeaders, ...extraHeaders];
283
+ // Validate renameMap
284
+ if (options?.renameMap && typeof options.renameMap !== 'object') {
285
+ throw new ConfigurationError('renameMap must be an object');
378
286
  }
379
-
380
- /**
381
- * Экранирование значения для CSV с защитой от инъекций
382
- * @private
383
- */
384
- const escapeValue = value => {
385
- if (value === null || value === undefined || value === '') {
386
- return '';
387
- }
388
- const stringValue = String(value);
389
-
390
- // Защита от CSV инъекций
391
- let escapedValue = stringValue;
392
- if (preventCsvInjection && /^[=+\-@]/.test(stringValue)) {
393
- escapedValue = "'" + stringValue;
394
- }
395
-
396
- // Соответствие RFC 4180
397
- const needsQuoting = rfc4180Compliant ? escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r') : escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r');
398
- if (needsQuoting) {
399
- return `"${escapedValue.replace(/"/g, '""')}"`;
400
- }
401
- return escapedValue;
402
- };
403
-
404
- // Построение CSV строк
405
- const rows = [];
406
-
407
- // Добавление заголовков
408
- if (includeHeaders && finalHeaders.length > 0) {
409
- rows.push(finalHeaders.join(delimiter));
287
+ // Validate maxRecords
288
+ if (options && options.maxRecords !== undefined) {
289
+ if (typeof options.maxRecords !== 'number' || options.maxRecords <= 0) {
290
+ throw new ConfigurationError('maxRecords must be a positive number');
291
+ }
410
292
  }
411
-
412
- // Добавление данных
413
- for (const item of data) {
414
- if (!item || typeof item !== 'object') {
415
- continue;
416
- }
417
- const row = finalHeaders.map(header => {
418
- const originalKey = reverseRenameMap[header] || header;
419
- const value = item[originalKey];
420
- return escapeValue(value);
421
- }).join(delimiter);
422
- rows.push(row);
293
+ // Validate preventCsvInjection
294
+ if (options?.preventCsvInjection !== undefined && typeof options.preventCsvInjection !== 'boolean') {
295
+ throw new ConfigurationError('preventCsvInjection must be a boolean');
423
296
  }
424
-
425
- // Разделители строк согласно RFC 4180
426
- const lineEnding = rfc4180Compliant ? '\r\n' : '\n';
427
- return rows.join(lineEnding);
428
- }, 'PARSE_FAILED', {
429
- function: 'jsonToCsv'
430
- });
297
+ // Validate rfc4180Compliant
298
+ if (options?.rfc4180Compliant !== undefined && typeof options.rfc4180Compliant !== 'boolean') {
299
+ throw new ConfigurationError('rfc4180Compliant must be a boolean');
300
+ }
301
+ if (options?.normalizeQuotes !== undefined && typeof options.normalizeQuotes !== 'boolean') {
302
+ throw new ConfigurationError('normalizeQuotes must be a boolean');
303
+ }
304
+ return true;
431
305
  }
432
-
433
306
  /**
434
- * Глубокое разворачивание вложенных объектов и массивов
435
- *
436
- * @param {*} value - Значение для разворачивания
437
- * @param {number} [depth=0] - Текущая глубина рекурсии
438
- * @param {number} [maxDepth=5] - Максимальная глубина рекурсии
439
- * @param {Set} [visited=new Set()] - Посещенные объекты для обнаружения циклических ссылок
440
- * @returns {string} Развернутое строковое значение
441
- */
442
- function deepUnwrap(value, depth = 0, maxDepth = 5, visited = new Set()) {
443
- // Проверка глубины
444
- if (depth >= maxDepth) {
445
- return '[Too Deep]';
446
- }
447
- if (value === null || value === undefined) {
448
- return '';
449
- }
450
-
451
- // Обработка циклических ссылок
452
- if (typeof value === 'object') {
453
- if (visited.has(value)) {
454
- return '[Circular Reference]';
307
+ * Экранирование CSV значений для предотвращения инъекций
308
+ * @private
309
+ */
310
+ function escapeCsvValue(value, preventInjection = true) {
311
+ if (value === null || value === undefined) {
312
+ return '';
455
313
  }
456
- visited.add(value);
457
- }
458
-
459
- // Обработка массивов
460
- if (Array.isArray(value)) {
461
- if (value.length === 0) {
462
- return '';
463
- }
464
- const unwrappedItems = value.map(item => deepUnwrap(item, depth + 1, maxDepth, visited)).filter(item => item !== '');
465
- return unwrappedItems.join(', ');
466
- }
467
-
468
- // Обработка объектов
469
- if (typeof value === 'object') {
470
- const keys = Object.keys(value);
471
- if (keys.length === 0) {
472
- return '';
314
+ const str = String(value);
315
+ const isPotentialFormula = (input) => {
316
+ let idx = 0;
317
+ while (idx < input.length) {
318
+ const code = input.charCodeAt(idx);
319
+ if (code === 32 || code === 9 || code === 10 || code === 13 || code === 0xfeff) {
320
+ idx++;
321
+ continue;
322
+ }
323
+ break;
324
+ }
325
+ if (idx < input.length && (input[idx] === '"' || input[idx] === "'")) {
326
+ idx++;
327
+ while (idx < input.length) {
328
+ const code = input.charCodeAt(idx);
329
+ if (code === 32 || code === 9) {
330
+ idx++;
331
+ continue;
332
+ }
333
+ break;
334
+ }
335
+ }
336
+ if (idx >= input.length) {
337
+ return false;
338
+ }
339
+ const char = input[idx];
340
+ return char === '=' || char === '+' || char === '-' || char === '@';
341
+ };
342
+ // Экранирование формул для предотвращения CSV инъекций
343
+ if (preventInjection && isPotentialFormula(str)) {
344
+ return "'" + str;
473
345
  }
474
- if (depth + 1 >= maxDepth) {
475
- return '[Too Deep]';
346
+ // Экранирование кавычек и переносов строк
347
+ if (str.includes('"') || str.includes('\n') || str.includes('\r') || str.includes(',')) {
348
+ return '"' + str.replace(/"/g, '""') + '"';
476
349
  }
477
-
478
- // Сериализация сложных объектов
479
- try {
480
- return JSON.stringify(value);
481
- } catch (error) {
482
- if (error.message.includes('circular') || error.message.includes('Converting circular')) {
483
- return '[Circular Reference]';
484
- }
485
- return '[Unstringifiable Object]';
486
- }
487
- }
488
-
489
- // Примитивные значения
490
- return String(value);
350
+ return str;
491
351
  }
492
-
493
- /**
494
- * Предобработка JSON данных путем глубокого разворачивания вложенных структур
495
- *
496
- * @param {Array<Object>} data - Массив объектов для предобработки
497
- * @returns {Array<Object>} Предобработанные данные с развернутыми значениями
498
- */
499
- function preprocessData(data) {
500
- if (!Array.isArray(data)) {
501
- return [];
502
- }
503
- return data.map(item => {
504
- if (!item || typeof item !== 'object') {
505
- return {};
506
- }
507
- const processed = {};
508
- for (const key in item) {
509
- if (Object.prototype.hasOwnProperty.call(item, key)) {
510
- const value = item[key];
511
- if (value && typeof value === 'object') {
512
- processed[key] = deepUnwrap(value);
513
- } else {
514
- processed[key] = value;
515
- }
516
- }
517
- }
518
- return processed;
519
- });
352
+ function normalizeQuotesInField$2(value) {
353
+ // Не нормализуем кавычки в JSON-строках - это ломает структуру JSON
354
+ // Проверяем, выглядит ли значение как JSON (объект или массив)
355
+ if ((value.startsWith('{') && value.endsWith('}')) ||
356
+ (value.startsWith('[') && value.endsWith(']'))) {
357
+ return value; // Возвращаем как есть для JSON
358
+ }
359
+ let normalized = value.replace(/"{2,}/g, '"');
360
+ // Убираем правило, которое ломает JSON: не заменяем "," на ","
361
+ // normalized = normalized.replace(/"\s*,\s*"/g, ',');
362
+ normalized = normalized.replace(/"\n/g, '\n').replace(/\n"/g, '\n');
363
+ if (normalized.length >= 2 && normalized.startsWith('"') && normalized.endsWith('"')) {
364
+ normalized = normalized.slice(1, -1);
365
+ }
366
+ return normalized;
520
367
  }
521
-
522
- // Экспорт для Node.js совместимости
523
- if (typeof module !== 'undefined' && module.exports) {
524
- module.exports = {
525
- jsonToCsv,
526
- preprocessData,
527
- deepUnwrap
528
- };
368
+ function normalizePhoneValue$2(value) {
369
+ const trimmed = value.trim();
370
+ if (trimmed === '') {
371
+ return trimmed;
372
+ }
373
+ return trimmed.replace(/["'\\]/g, '');
374
+ }
375
+ function normalizeValueForCsv$1(value, key, normalizeQuotes) {
376
+ if (!normalizeQuotes || typeof value !== 'string') {
377
+ return value;
378
+ }
379
+ const base = normalizeQuotesInField$2(value);
380
+ if (!key) {
381
+ return base;
382
+ }
383
+ const phoneKeys = new Set(['phone', 'phonenumber', 'phone_number', 'tel', 'telephone']);
384
+ if (phoneKeys.has(String(key).toLowerCase())) {
385
+ return normalizePhoneValue$2(base);
386
+ }
387
+ return base;
529
388
  }
530
-
531
- // Браузерная версия CSV to JSON конвертера
532
- // Адаптирована для работы в браузере без Node.js API
533
-
534
-
535
389
  /**
536
- * Валидация опций парсинга
390
+ * Извлечение всех уникальных ключей из массива объектов
537
391
  * @private
538
392
  */
539
- function validateCsvOptions(options) {
540
- // Validate options
541
- if (options && typeof options !== 'object') {
542
- throw new ConfigurationError('Options must be an object');
543
- }
544
-
545
- // Validate delimiter
546
- if (options !== null && options !== void 0 && options.delimiter && typeof options.delimiter !== 'string') {
547
- throw new ConfigurationError('Delimiter must be a string');
548
- }
549
- if (options !== null && options !== void 0 && options.delimiter && options.delimiter.length !== 1) {
550
- throw new ConfigurationError('Delimiter must be a single character');
551
- }
552
-
553
- // Validate autoDetect
554
- if ((options === null || options === void 0 ? void 0 : options.autoDetect) !== undefined && typeof options.autoDetect !== 'boolean') {
555
- throw new ConfigurationError('autoDetect must be a boolean');
556
- }
557
-
558
- // Validate candidates
559
- if (options !== null && options !== void 0 && options.candidates && !Array.isArray(options.candidates)) {
560
- throw new ConfigurationError('candidates must be an array');
561
- }
562
-
563
- // Validate maxRows
564
- if ((options === null || options === void 0 ? void 0 : options.maxRows) !== undefined && (typeof options.maxRows !== 'number' || options.maxRows <= 0)) {
565
- throw new ConfigurationError('maxRows must be a positive number');
566
- }
567
- if ((options === null || options === void 0 ? void 0 : options.warnExtraFields) !== undefined && typeof options.warnExtraFields !== 'boolean') {
568
- throw new ConfigurationError('warnExtraFields must be a boolean');
569
- }
570
- return true;
393
+ function extractAllKeys(data) {
394
+ const keys = new Set();
395
+ for (const item of data) {
396
+ if (item && typeof item === 'object') {
397
+ Object.keys(item).forEach(key => keys.add(key));
398
+ }
399
+ }
400
+ return Array.from(keys);
571
401
  }
572
-
573
402
  /**
574
- * Валидация CSV ввода и опций
575
- * @private
403
+ * Конвертация массива объектов в CSV строку
404
+ *
405
+ * @param data - Массив объектов для конвертации
406
+ * @param options - Опции конвертации
407
+ * @returns CSV строка
576
408
  */
577
- function validateCsvInput(csv, options) {
578
- // Validate CSV input
579
- if (typeof csv !== 'string') {
580
- throw new ValidationError('Input must be a CSV string');
581
- }
582
- return validateCsvOptions(options);
409
+ function jsonToCsv$1(data, options = {}) {
410
+ return safeExecute(() => {
411
+ validateInput(data, options);
412
+ if (data.length === 0) {
413
+ return '';
414
+ }
415
+ // Настройки по умолчанию
416
+ const delimiter = options.delimiter || ';';
417
+ const includeHeaders = options.includeHeaders !== false;
418
+ const maxRecords = options.maxRecords || data.length;
419
+ const preventInjection = options.preventCsvInjection !== false;
420
+ const rfc4180Compliant = options.rfc4180Compliant !== false;
421
+ const normalizeQuotes = options.normalizeQuotes !== false;
422
+ // Ограничение количества записей
423
+ const limitedData = data.slice(0, maxRecords);
424
+ // Извлечение всех ключей
425
+ const allKeys = extractAllKeys(limitedData);
426
+ // Применение renameMap если есть
427
+ const renameMap = options.renameMap || {};
428
+ const finalKeys = allKeys.map(key => renameMap[key] || key);
429
+ // Создание CSV строки
430
+ const lines = [];
431
+ // Заголовки
432
+ if (includeHeaders) {
433
+ const headerLine = finalKeys.map(key => escapeCsvValue(key, preventInjection)).join(delimiter);
434
+ lines.push(headerLine);
435
+ }
436
+ // Данные
437
+ for (const item of limitedData) {
438
+ const rowValues = allKeys.map(key => {
439
+ const value = item?.[key];
440
+ const normalized = normalizeValueForCsv$1(value, key, normalizeQuotes);
441
+ return escapeCsvValue(normalized, preventInjection);
442
+ });
443
+ lines.push(rowValues.join(delimiter));
444
+ }
445
+ // RFC 4180 compliance: CRLF line endings
446
+ if (rfc4180Compliant) {
447
+ return lines.join('\r\n');
448
+ }
449
+ return lines.join('\n');
450
+ });
583
451
  }
584
-
585
452
  /**
586
- * Парсинг одной строки CSV с правильным экранированием
587
- * @private
453
+ * Асинхронная версия jsonToCsv
588
454
  */
589
- function parseCsvLine(line, lineNumber, delimiter) {
590
- const fields = [];
591
- let currentField = '';
592
- let insideQuotes = false;
593
- let escapeNext = false;
594
- for (let i = 0; i < line.length; i++) {
595
- const char = line[i];
596
- if (escapeNext) {
597
- currentField += char;
598
- escapeNext = false;
599
- continue;
600
- }
601
- if (char === '\\') {
602
- if (i + 1 === line.length) {
603
- // Обратный слеш в конце строки
604
- currentField += char;
605
- } else if (line[i + 1] === '\\') {
606
- // Двойной обратный слеш
607
- currentField += char;
608
- i++; // Пропустить следующий слеш
609
- } else {
610
- // Экранирование следующего символа
611
- escapeNext = true;
612
- }
613
- continue;
614
- }
615
- if (char === '"') {
616
- if (insideQuotes) {
617
- if (i + 1 < line.length && line[i + 1] === '"') {
618
- // Экранированная кавычка внутри кавычек
619
- currentField += '"';
620
- i++; // Пропустить следующую кавычку
621
-
622
- // Проверка конца поля
623
- let isEndOfField = false;
624
- let j = i + 1;
625
- while (j < line.length && (line[j] === ' ' || line[j] === '\t')) {
626
- j++;
627
- }
628
- if (j === line.length || line[j] === delimiter) {
629
- isEndOfField = true;
630
- }
631
- if (isEndOfField) {
632
- insideQuotes = false;
633
- }
634
- } else {
635
- // Проверка конца поля
636
- let isEndOfField = false;
637
- let j = i + 1;
638
- while (j < line.length && (line[j] === ' ' || line[j] === '\t')) {
639
- j++;
640
- }
641
- if (j === line.length || line[j] === delimiter) {
642
- isEndOfField = true;
643
- }
644
- if (isEndOfField) {
645
- insideQuotes = false;
646
- } else {
647
- currentField += '"';
648
- }
649
- }
650
- } else {
651
- // Начало поля в кавычках
652
- insideQuotes = true;
653
- }
654
- continue;
655
- }
656
- if (!insideQuotes && char === delimiter) {
657
- // Конец поля
658
- fields.push(currentField);
659
- currentField = '';
660
- continue;
661
- }
662
- currentField += char;
663
- }
664
-
665
- // Обработка незавершенного экранирования
666
- if (escapeNext) {
667
- currentField += '\\';
668
- }
669
-
670
- // Добавление последнего поля
671
- fields.push(currentField);
672
-
673
- // Проверка незакрытых кавычек
674
- if (insideQuotes) {
675
- throw new ParsingError('Unclosed quotes in CSV', lineNumber);
676
- }
677
-
678
- // Валидация количества полей
679
- if (fields.length === 0) {
680
- throw new ParsingError('No fields found', lineNumber);
681
- }
682
- return fields;
455
+ async function jsonToCsvAsync$1(data, options = {}) {
456
+ return jsonToCsv$1(data, options);
683
457
  }
684
-
685
458
  /**
686
- * Парсинг значения на основе опций
687
- * @private
459
+ * Создает итератор для потоковой конвертации JSON в CSV
460
+ *
461
+ * @param data - Массив объектов или async итератор
462
+ * @param options - Опции конвертации
463
+ * @returns AsyncIterator с CSV чанками
688
464
  */
689
- function parseCsvValue(value, options) {
690
- const {
691
- trim = true,
692
- parseNumbers = false,
693
- parseBooleans = false
694
- } = options;
695
- let result = value;
696
- if (trim) {
697
- result = result.trim();
698
- }
699
-
700
- // Удаление защиты формул Excel
701
- if (result.startsWith("'")) {
702
- result = result.substring(1);
703
- }
704
-
705
- // Парсинг чисел
706
- if (parseNumbers && /^-?\d+(\.\d+)?$/.test(result)) {
707
- const num = parseFloat(result);
708
- if (!isNaN(num)) {
709
- return num;
465
+ async function* jsonToCsvIterator(data, options = {}) {
466
+ validateInput(Array.isArray(data) ? data : [], options);
467
+ const delimiter = options.delimiter || ';';
468
+ const includeHeaders = options.includeHeaders !== false;
469
+ const preventInjection = options.preventCsvInjection !== false;
470
+ const rfc4180Compliant = options.rfc4180Compliant !== false;
471
+ const normalizeQuotes = options.normalizeQuotes !== false;
472
+ let allKeys = [];
473
+ let renameMap = {};
474
+ // Если данные - массив, обрабатываем как массив
475
+ if (Array.isArray(data)) {
476
+ if (data.length === 0) {
477
+ return;
478
+ }
479
+ allKeys = extractAllKeys(data);
480
+ renameMap = options.renameMap || {};
481
+ const finalKeys = allKeys.map(key => renameMap[key] || key);
482
+ // Заголовки
483
+ if (includeHeaders) {
484
+ const headerLine = finalKeys.map(key => escapeCsvValue(key, preventInjection)).join(delimiter);
485
+ yield headerLine + (rfc4180Compliant ? '\r\n' : '\n');
486
+ }
487
+ // Данные
488
+ for (const item of data) {
489
+ const rowValues = allKeys.map(key => {
490
+ const value = item?.[key];
491
+ const normalized = normalizeValueForCsv$1(value, key, normalizeQuotes);
492
+ return escapeCsvValue(normalized, preventInjection);
493
+ });
494
+ yield rowValues.join(delimiter) + (rfc4180Compliant ? '\r\n' : '\n');
495
+ }
710
496
  }
711
- }
712
-
713
- // Парсинг булевых значений
714
- if (parseBooleans) {
715
- const lowerValue = result.toLowerCase();
716
- if (lowerValue === 'true') {
717
- return true;
497
+ else {
498
+ // Для async итератора нужна другая логика
499
+ throw new ValidationError('Async iterators not yet implemented in browser version');
500
+ }
501
+ }
502
+ /**
503
+ * Асинхронная версия jsonToCsvIterator (псевдоним)
504
+ */
505
+ const jsonToCsvIteratorAsync = jsonToCsvIterator;
506
+ /**
507
+ * Безопасная конвертация с обработкой ошибок
508
+ *
509
+ * @param data - Массив объектов
510
+ * @param options - Опции конвертации
511
+ * @returns CSV строка или null при ошибке
512
+ */
513
+ function jsonToCsvSafe(data, options = {}) {
514
+ try {
515
+ return jsonToCsv$1(data, options);
718
516
  }
719
- if (lowerValue === 'false') {
720
- return false;
517
+ catch (error) {
518
+ console.error('JSON to CSV conversion error:', error);
519
+ return null;
721
520
  }
722
- }
723
-
724
- // Пустые строки как null
725
- if (result === '') {
726
- return null;
727
- }
728
- return result;
729
- }
730
- function isSimpleCsv(csv) {
731
- return csv.indexOf('"') === -1 && csv.indexOf('\\') === -1;
732
- }
733
- function parseSimpleCsv(csv, delimiter, options) {
734
- const {
735
- hasHeaders = true,
736
- renameMap = {},
737
- trim = true,
738
- parseNumbers = false,
739
- parseBooleans = false,
740
- maxRows
741
- } = options;
742
- const result = [];
743
- let headers = null;
744
- let fieldStart = 0;
745
- let currentRow = [];
746
- let rowHasData = false;
747
- let rowCount = 0;
748
- const finalizeRow = fields => {
749
- if (fields.length === 1 && fields[0].trim() === '') {
750
- return;
751
- }
752
- if (!headers) {
753
- if (hasHeaders) {
754
- headers = fields.map(header => {
755
- const trimmed = trim ? header.trim() : header;
756
- return renameMap[trimmed] || trimmed;
757
- });
758
- return;
759
- }
760
- headers = fields.map((_, index) => `column${index + 1}`);
761
- }
762
- rowCount++;
763
- if (maxRows && rowCount > maxRows) {
764
- throw new LimitError(`CSV size exceeds maximum limit of ${maxRows} rows`, maxRows, rowCount);
765
- }
766
- const row = {};
767
- const fieldCount = Math.min(fields.length, headers.length);
768
- for (let i = 0; i < fieldCount; i++) {
769
- row[headers[i]] = parseCsvValue(fields[i], {
770
- trim,
771
- parseNumbers,
772
- parseBooleans
773
- });
774
- }
775
- result.push(row);
776
- };
777
- let i = 0;
778
- while (i <= csv.length) {
779
- const char = i < csv.length ? csv[i] : '\n';
780
- if (char !== '\r' && char !== '\n' && char !== ' ' && char !== '\t') {
781
- rowHasData = true;
782
- }
783
- if (char === delimiter || char === '\n' || char === '\r' || i === csv.length) {
784
- const field = csv.slice(fieldStart, i);
785
- currentRow.push(field);
786
- if (char === '\n' || char === '\r' || i === csv.length) {
787
- if (rowHasData || currentRow.length > 1) {
788
- finalizeRow(currentRow);
789
- }
790
- currentRow = [];
791
- rowHasData = false;
792
- }
793
- if (char === '\r' && csv[i + 1] === '\n') {
794
- i++;
795
- }
796
- fieldStart = i + 1;
797
- }
798
- i++;
799
- }
800
- return result;
801
521
  }
802
-
803
522
  /**
804
- * Автоматическое определение разделителя CSV
805
- *
806
- * @param {string} csv - CSV строка
807
- * @param {Array} [candidates=[';', ',', '\t', '|']] - Кандидаты на разделитель
808
- * @returns {string} Определенный разделитель
809
- */
810
- function autoDetectDelimiter(csv, candidates = [';', ',', '\t', '|']) {
811
- if (!csv || typeof csv !== 'string') {
812
- return ';'; // значение по умолчанию
813
- }
814
- const lines = csv.split('\n').filter(line => line.trim().length > 0);
815
- if (lines.length === 0) {
816
- return ';'; // значение по умолчанию
817
- }
818
-
819
- // Использование первой непустой строки для определения
820
- const firstLine = lines[0];
821
- const counts = {};
822
- candidates.forEach(delim => {
823
- const escapedDelim = delim.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
824
- const regex = new RegExp(escapedDelim, 'g');
825
- const matches = firstLine.match(regex);
826
- counts[delim] = matches ? matches.length : 0;
827
- });
828
-
829
- // Поиск разделителя с максимальным количеством
830
- let maxCount = -1;
831
- let detectedDelimiter = ';'; // значение по умолчанию
832
-
833
- for (const [delim, count] of Object.entries(counts)) {
834
- if (count > maxCount) {
835
- maxCount = count;
836
- detectedDelimiter = delim;
523
+ * Асинхронная версия jsonToCsvSafe
524
+ */
525
+ async function jsonToCsvSafeAsync(data, options = {}) {
526
+ try {
527
+ return await jsonToCsvAsync$1(data, options);
837
528
  }
838
- }
839
-
840
- // Если разделитель не найден или ничья
841
- if (maxCount === 0) {
842
- return ';'; // значение по умолчанию
843
- }
844
- return detectedDelimiter;
529
+ catch (error) {
530
+ console.error('JSON to CSV conversion error:', error);
531
+ return null;
532
+ }
533
+ }
534
+ // Экспорт для Node.js совместимости
535
+ if (typeof module !== 'undefined' && module.exports) {
536
+ module.exports = {
537
+ jsonToCsv: jsonToCsv$1,
538
+ jsonToCsvAsync: jsonToCsvAsync$1,
539
+ jsonToCsvIterator,
540
+ jsonToCsvIteratorAsync,
541
+ jsonToCsvSafe,
542
+ jsonToCsvSafeAsync
543
+ };
845
544
  }
846
545
 
847
- /**
848
- * Конвертирует CSV строку в JSON массив
849
- *
850
- * @param {string} csv - CSV строка для конвертации
851
- * @param {Object} [options] - Опции конфигурации
852
- * @param {string} [options.delimiter] - CSV разделитель (по умолчанию: автоопределение)
853
- * @param {boolean} [options.autoDetect=true] - Автоопределение разделителя
854
- * @param {Array} [options.candidates=[';', ',', '\t', '|']] - Кандидаты для автоопределения
855
- * @param {boolean} [options.hasHeaders=true] - Есть ли заголовки в CSV
856
- * @param {Object} [options.renameMap={}] - Маппинг переименования заголовков
857
- * @param {boolean} [options.trim=true] - Обрезать пробелы
858
- * @param {boolean} [options.parseNumbers=false] - Парсить числовые значения
859
- * @param {boolean} [options.parseBooleans=false] - Парсить булевы значения
860
- * @param {number} [options.maxRows] - Максимальное количество строк
861
- * @returns {Array<Object>} JSON массив
862
- */
863
- function csvToJson(csv, options = {}) {
864
- return safeExecute(() => {
865
- // Валидация ввода
866
- validateCsvInput(csv, options);
867
- const opts = options && typeof options === 'object' ? options : {};
868
- const {
869
- delimiter,
870
- autoDetect = true,
871
- candidates = [';', ',', '\t', '|'],
872
- hasHeaders = true,
873
- renameMap = {},
874
- trim = true,
875
- parseNumbers = false,
876
- parseBooleans = false,
877
- maxRows,
878
- warnExtraFields = true
879
- } = opts;
546
+ var jsonToCsvBrowser = /*#__PURE__*/Object.freeze({
547
+ __proto__: null,
548
+ jsonToCsv: jsonToCsv$1,
549
+ jsonToCsvAsync: jsonToCsvAsync$1,
550
+ jsonToCsvIterator: jsonToCsvIterator,
551
+ jsonToCsvIteratorAsync: jsonToCsvIteratorAsync,
552
+ jsonToCsvSafe: jsonToCsvSafe,
553
+ jsonToCsvSafeAsync: jsonToCsvSafeAsync
554
+ });
880
555
 
881
- // Определение разделителя
882
- let finalDelimiter = delimiter;
883
- if (!finalDelimiter && autoDetect) {
884
- finalDelimiter = autoDetectDelimiter(csv, candidates);
556
+ // Браузерная версия CSV to JSON конвертера
557
+ // Адаптирована для работы в браузере без Node.js API
558
+ /**
559
+ * Валидация опций парсинга
560
+ * @private
561
+ */
562
+ function validateCsvOptions(options) {
563
+ // Validate options
564
+ if (options && typeof options !== 'object') {
565
+ throw new ConfigurationError('Options must be an object');
885
566
  }
886
- finalDelimiter = finalDelimiter || ';'; // fallback
887
-
888
- // Обработка пустого CSV
889
- if (csv.trim() === '') {
890
- return [];
891
- }
892
- if (isSimpleCsv(csv)) {
893
- return parseSimpleCsv(csv, finalDelimiter, {
894
- hasHeaders,
895
- renameMap,
896
- trim,
897
- parseNumbers,
898
- parseBooleans,
899
- maxRows
900
- });
567
+ // Validate delimiter
568
+ if (options?.delimiter && typeof options.delimiter !== 'string') {
569
+ throw new ConfigurationError('Delimiter must be a string');
901
570
  }
902
-
903
- // Парсинг CSV с обработкой кавычек и переносов строк
904
- const lines = [];
905
- let currentLine = '';
906
- let insideQuotes = false;
907
- for (let i = 0; i < csv.length; i++) {
908
- const char = csv[i];
909
- if (char === '"') {
910
- if (insideQuotes && i + 1 < csv.length && csv[i + 1] === '"') {
911
- // Экранированная кавычка внутри кавычек
912
- currentLine += '"';
913
- i++; // Пропустить следующую кавычку
914
- } else {
915
- // Переключение режима кавычек
916
- insideQuotes = !insideQuotes;
917
- }
918
- currentLine += char;
919
- continue;
920
- }
921
- if (char === '\n' && !insideQuotes) {
922
- // Конец строки (вне кавычек)
923
- lines.push(currentLine);
924
- currentLine = '';
925
- continue;
926
- }
927
- if (char === '\r') {
928
- // Игнорировать carriage return
929
- continue;
930
- }
931
- currentLine += char;
571
+ if (options?.delimiter && options.delimiter.length !== 1) {
572
+ throw new ConfigurationError('Delimiter must be a single character');
932
573
  }
933
-
934
- // Добавление последней строки
935
- if (currentLine !== '' || insideQuotes) {
936
- lines.push(currentLine);
574
+ // Validate autoDetect
575
+ if (options?.autoDetect !== undefined && typeof options.autoDetect !== 'boolean') {
576
+ throw new ConfigurationError('autoDetect must be a boolean');
937
577
  }
938
- if (lines.length === 0) {
939
- return [];
578
+ // Validate candidates
579
+ if (options?.candidates && !Array.isArray(options.candidates)) {
580
+ throw new ConfigurationError('candidates must be an array');
940
581
  }
941
-
942
- // Предупреждение для больших наборов данных
943
- if (lines.length > 1000000 && !maxRows && process.env.NODE_ENV !== 'production') {
944
- console.warn('⚠️ Warning: Processing >1M records in memory may be slow.\n' + '💡 Consider using Web Workers for better performance with large files.\n' + '📊 Current size: ' + lines.length.toLocaleString() + ' rows');
582
+ // Validate maxRows
583
+ if (options?.maxRows !== undefined && (typeof options.maxRows !== 'number' || options.maxRows <= 0)) {
584
+ throw new ConfigurationError('maxRows must be a positive number');
945
585
  }
946
-
947
- // Применение ограничения по строкам
948
- if (maxRows && lines.length > maxRows) {
949
- throw new LimitError(`CSV size exceeds maximum limit of ${maxRows} rows`, maxRows, lines.length);
586
+ if (options?.warnExtraFields !== undefined && typeof options.warnExtraFields !== 'boolean') {
587
+ throw new ConfigurationError('warnExtraFields must be a boolean');
950
588
  }
951
- let headers = [];
952
- let startIndex = 0;
953
-
954
- // Парсинг заголовков если есть
955
- if (hasHeaders && lines.length > 0) {
956
- try {
957
- headers = parseCsvLine(lines[0], 1, finalDelimiter).map(header => {
958
- const trimmed = trim ? header.trim() : header;
959
- return renameMap[trimmed] || trimmed;
960
- });
961
- startIndex = 1;
962
- } catch (error) {
963
- if (error instanceof ParsingError) {
964
- throw new ParsingError(`Failed to parse headers: ${error.message}`, 1);
965
- }
966
- throw error;
967
- }
968
- } else {
969
- // Генерация числовых заголовков из первой строки
970
- try {
971
- const firstLineFields = parseCsvLine(lines[0], 1, finalDelimiter);
972
- headers = firstLineFields.map((_, index) => `column${index + 1}`);
973
- } catch (error) {
974
- if (error instanceof ParsingError) {
975
- throw new ParsingError(`Failed to parse first line: ${error.message}`, 1);
976
- }
977
- throw error;
978
- }
589
+ if (options?.repairRowShifts !== undefined && typeof options.repairRowShifts !== 'boolean') {
590
+ throw new ConfigurationError('repairRowShifts must be a boolean');
979
591
  }
980
-
981
- // Парсинг строк данных
982
- const result = [];
983
- for (let i = startIndex; i < lines.length; i++) {
984
- const line = lines[i];
985
-
986
- // Пропуск пустых строк
987
- if (line.trim() === '') {
988
- continue;
989
- }
990
- try {
991
- const fields = parseCsvLine(line, i + 1, finalDelimiter);
992
-
993
- // Обработка несоответствия количества полей
994
- const row = {};
995
- const fieldCount = Math.min(fields.length, headers.length);
996
- for (let j = 0; j < fieldCount; j++) {
997
- row[headers[j]] = parseCsvValue(fields[j], {
998
- trim,
999
- parseNumbers,
1000
- parseBooleans
1001
- });
1002
- }
1003
-
1004
- // Предупреждение о лишних полях
1005
- const isDev = typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'development';
1006
- if (fields.length > headers.length && warnExtraFields && isDev) {
1007
- console.warn(`[jtcsv] Line ${i + 1}: ${fields.length - headers.length} extra fields ignored`);
1008
- }
1009
- result.push(row);
1010
- } catch (error) {
1011
- if (error instanceof ParsingError) {
1012
- throw new ParsingError(`Line ${i + 1}: ${error.message}`, i + 1);
592
+ if (options?.normalizeQuotes !== undefined && typeof options.normalizeQuotes !== 'boolean') {
593
+ throw new ConfigurationError('normalizeQuotes must be a boolean');
594
+ }
595
+ return true;
596
+ }
597
+ /**
598
+ * Автоматическое определение разделителя
599
+ * @private
600
+ */
601
+ function autoDetectDelimiter$1(text, candidates = [',', ';', '\t', '|']) {
602
+ if (!text || typeof text !== 'string') {
603
+ return ',';
604
+ }
605
+ const firstLine = text.split('\n')[0];
606
+ if (!firstLine) {
607
+ return ',';
608
+ }
609
+ let bestCandidate = ',';
610
+ let bestCount = 0;
611
+ for (const candidate of candidates) {
612
+ const count = (firstLine.match(new RegExp(candidate.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g')) || []).length;
613
+ if (count > bestCount) {
614
+ bestCount = count;
615
+ bestCandidate = candidate;
1013
616
  }
1014
- throw error;
1015
- }
1016
- }
1017
- return result;
1018
- }, 'PARSE_FAILED', {
1019
- function: 'csvToJson'
1020
- });
1021
- }
1022
- async function* csvToJsonIterator(input, options = {}) {
1023
- const opts = options && typeof options === 'object' ? options : {};
1024
- validateCsvOptions(opts);
1025
- if (typeof input === 'string') {
1026
- const rows = csvToJson(input, options);
1027
- for (const row of rows) {
1028
- yield row;
1029
- }
1030
- return;
1031
- }
1032
- const {
1033
- delimiter,
1034
- autoDetect = true,
1035
- candidates = [';', ',', '\t', '|'],
1036
- hasHeaders = true,
1037
- renameMap = {},
1038
- trim = true,
1039
- parseNumbers = false,
1040
- parseBooleans = false,
1041
- maxRows
1042
- } = opts;
1043
- const stream = input instanceof Blob && input.stream ? input.stream() : input;
1044
- if (!stream || typeof stream.getReader !== 'function') {
1045
- throw new ValidationError('Input must be a CSV string, Blob/File, or ReadableStream');
1046
- }
1047
- const reader = stream.getReader();
1048
- const decoder = new TextDecoder('utf-8');
1049
- let buffer = '';
1050
- let insideQuotes = false;
1051
- let headers = null;
1052
- let rowCount = 0;
1053
- let lineNumber = 0;
1054
- let finalDelimiter = delimiter;
1055
- let delimiterResolved = Boolean(finalDelimiter);
1056
- const processFields = fields => {
1057
- if (fields.length === 1 && fields[0].trim() === '') {
1058
- return null;
1059
- }
1060
- rowCount++;
1061
- if (maxRows && rowCount > maxRows) {
1062
- throw new LimitError(`CSV size exceeds maximum limit of ${maxRows} rows`, maxRows, rowCount);
1063
- }
1064
- const row = {};
1065
- const fieldCount = Math.min(fields.length, headers.length);
1066
- for (let j = 0; j < fieldCount; j++) {
1067
- row[headers[j]] = parseCsvValue(fields[j], {
1068
- trim,
1069
- parseNumbers,
1070
- parseBooleans
1071
- });
1072
- }
1073
- return row;
1074
- };
1075
- const processLine = line => {
1076
- lineNumber++;
1077
- let cleanLine = line;
1078
- if (cleanLine.endsWith('\r')) {
1079
- cleanLine = cleanLine.slice(0, -1);
1080
- }
1081
- if (!delimiterResolved) {
1082
- if (!finalDelimiter && autoDetect) {
1083
- finalDelimiter = autoDetectDelimiter(cleanLine, candidates);
1084
- }
1085
- finalDelimiter = finalDelimiter || ';';
1086
- delimiterResolved = true;
1087
- }
1088
- if (cleanLine.trim() === '') {
1089
- return null;
1090
- }
1091
- if (!headers) {
1092
- if (hasHeaders) {
1093
- headers = parseCsvLine(cleanLine, lineNumber, finalDelimiter).map(header => {
1094
- const trimmed = trim ? header.trim() : header;
1095
- return renameMap[trimmed] || trimmed;
1096
- });
1097
- return null;
1098
- }
1099
- const fields = parseCsvLine(cleanLine, lineNumber, finalDelimiter);
1100
- headers = fields.map((_, index) => `column${index + 1}`);
1101
- return processFields(fields);
1102
- }
1103
- const fields = parseCsvLine(cleanLine, lineNumber, finalDelimiter);
1104
- return processFields(fields);
1105
- };
1106
- while (true) {
1107
- const {
1108
- value,
1109
- done
1110
- } = await reader.read();
1111
- if (done) {
1112
- break;
1113
- }
1114
- buffer += decoder.decode(value, {
1115
- stream: true
1116
- });
1117
- let start = 0;
1118
- for (let i = 0; i < buffer.length; i++) {
1119
- const char = buffer[i];
1120
- if (char === '"') {
1121
- if (insideQuotes && buffer[i + 1] === '"') {
1122
- i++;
1123
- continue;
1124
- }
1125
- insideQuotes = !insideQuotes;
1126
- continue;
1127
- }
1128
- if (char === '\n' && !insideQuotes) {
1129
- const line = buffer.slice(start, i);
1130
- start = i + 1;
1131
- const row = processLine(line);
1132
- if (row) {
1133
- yield row;
1134
- }
1135
- }
1136
- }
1137
- buffer = buffer.slice(start);
1138
- }
1139
- if (buffer.length > 0) {
1140
- const row = processLine(buffer);
1141
- if (row) {
1142
- yield row;
1143
- }
1144
- }
1145
- if (insideQuotes) {
1146
- throw new ParsingError('Unclosed quotes in CSV', lineNumber);
1147
- }
617
+ }
618
+ return bestCandidate;
1148
619
  }
1149
-
1150
- // Экспорт для Node.js совместимости
1151
- if (typeof module !== 'undefined' && module.exports) {
1152
- module.exports = {
1153
- csvToJson,
1154
- autoDetectDelimiter,
1155
- csvToJsonIterator
1156
- };
620
+ function isEmptyValue(value) {
621
+ return value === undefined || value === null || value === '';
1157
622
  }
1158
-
1159
- const DEFAULT_MAX_CHUNK_SIZE = 64 * 1024;
1160
- function isReadableStream(value) {
1161
- return value && typeof value.getReader === 'function';
623
+ function hasOddQuotes(value) {
624
+ if (typeof value !== 'string') {
625
+ return false;
626
+ }
627
+ let count = 0;
628
+ for (let i = 0; i < value.length; i++) {
629
+ if (value[i] === '"') {
630
+ count++;
631
+ }
632
+ }
633
+ return count % 2 === 1;
1162
634
  }
1163
- function isAsyncIterable(value) {
1164
- return value && typeof value[Symbol.asyncIterator] === 'function';
635
+ function hasAnyQuotes(value) {
636
+ return typeof value === 'string' && value.includes('"');
1165
637
  }
1166
- function isIterable(value) {
1167
- return value && typeof value[Symbol.iterator] === 'function';
638
+ function normalizeQuotesInField$1(value) {
639
+ if (typeof value !== 'string') {
640
+ return value;
641
+ }
642
+ // Не нормализуем кавычки в JSON-строках - это ломает структуру JSON
643
+ // Проверяем, выглядит ли значение как JSON (объект или массив)
644
+ if ((value.startsWith('{') && value.endsWith('}')) ||
645
+ (value.startsWith('[') && value.endsWith(']'))) {
646
+ return value; // Возвращаем как есть для JSON
647
+ }
648
+ let normalized = value.replace(/"{2,}/g, '"');
649
+ // Убираем правило, которое ломает JSON: не заменяем "," на ","
650
+ // normalized = normalized.replace(/"\s*,\s*"/g, ',');
651
+ normalized = normalized.replace(/"\n/g, '\n').replace(/\n"/g, '\n');
652
+ if (normalized.length >= 2 && normalized.startsWith('"') && normalized.endsWith('"')) {
653
+ normalized = normalized.slice(1, -1);
654
+ }
655
+ return normalized;
1168
656
  }
1169
- function createReadableStreamFromIterator(iterator) {
1170
- return new ReadableStream({
1171
- async pull(controller) {
1172
- try {
1173
- const {
1174
- value,
1175
- done
1176
- } = await iterator.next();
1177
- if (done) {
1178
- controller.close();
1179
- return;
1180
- }
1181
- controller.enqueue(value);
1182
- } catch (error) {
1183
- controller.error(error);
1184
- }
1185
- },
1186
- cancel() {
1187
- if (iterator.return) {
1188
- iterator.return();
1189
- }
1190
- }
1191
- });
657
+ function normalizePhoneValue$1(value) {
658
+ if (typeof value !== 'string') {
659
+ return value;
660
+ }
661
+ const trimmed = value.trim();
662
+ if (trimmed === '') {
663
+ return trimmed;
664
+ }
665
+ return trimmed.replace(/["'\\]/g, '');
1192
666
  }
1193
- function detectInputFormat(input, options) {
1194
- if (options && options.inputFormat) {
1195
- return options.inputFormat;
1196
- }
1197
- if (typeof input === 'string') {
1198
- const trimmed = input.trim();
1199
- if (trimmed.startsWith('[')) {
1200
- return 'json-array';
1201
- }
1202
- if (trimmed.includes('\n')) {
1203
- return 'ndjson';
1204
- }
1205
- return 'json-array';
1206
- }
1207
- if (input instanceof Blob || isReadableStream(input)) {
1208
- return 'ndjson';
1209
- }
1210
- return 'json-array';
1211
- }
1212
- async function* parseNdjsonText(text) {
1213
- const lines = text.split(/\r?\n/);
1214
- for (const line of lines) {
1215
- const trimmed = line.trim();
1216
- if (!trimmed) {
1217
- continue;
1218
- }
1219
- yield JSON.parse(trimmed);
1220
- }
1221
- }
1222
- async function* parseNdjsonStream(stream) {
1223
- const reader = stream.getReader();
1224
- const decoder = new TextDecoder('utf-8');
1225
- let buffer = '';
1226
- while (true) {
1227
- const {
1228
- value,
1229
- done
1230
- } = await reader.read();
1231
- if (done) {
1232
- break;
1233
- }
1234
- buffer += decoder.decode(value, {
1235
- stream: true
667
+ function normalizeRowQuotes(row, headers) {
668
+ const normalized = {};
669
+ const phoneKeys = new Set(['phone', 'phonenumber', 'phone_number', 'tel', 'telephone']);
670
+ for (const header of headers) {
671
+ const baseValue = normalizeQuotesInField$1(row[header]);
672
+ if (phoneKeys.has(String(header).toLowerCase())) {
673
+ normalized[header] = normalizePhoneValue$1(baseValue);
674
+ }
675
+ else {
676
+ normalized[header] = baseValue;
677
+ }
678
+ }
679
+ return normalized;
680
+ }
681
+ function looksLikeUserAgent(value) {
682
+ if (typeof value !== 'string') {
683
+ return false;
684
+ }
685
+ return /Mozilla\/|Opera\/|MSIE|AppleWebKit|Gecko|Safari|Chrome\//.test(value);
686
+ }
687
+ function isHexColor(value) {
688
+ return typeof value === 'string' && /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(value);
689
+ }
690
+ function repairShiftedRows(rows, headers, options = {}) {
691
+ if (!Array.isArray(rows) || rows.length === 0 || headers.length === 0) {
692
+ return rows;
693
+ }
694
+ const headerCount = headers.length;
695
+ const merged = [];
696
+ let index = 0;
697
+ while (index < rows.length) {
698
+ const row = rows[index];
699
+ if (!row || typeof row !== 'object') {
700
+ merged.push(row);
701
+ index++;
702
+ continue;
703
+ }
704
+ const values = headers.map((header) => row[header]);
705
+ let lastNonEmpty = -1;
706
+ for (let i = headerCount - 1; i >= 0; i--) {
707
+ if (!isEmptyValue(values[i])) {
708
+ lastNonEmpty = i;
709
+ break;
710
+ }
711
+ }
712
+ const missingCount = headerCount - 1 - lastNonEmpty;
713
+ if (lastNonEmpty >= 0 && missingCount > 0 && index + 1 < rows.length) {
714
+ const nextRow = rows[index + 1];
715
+ if (nextRow && typeof nextRow === 'object') {
716
+ const nextValues = headers.map((header) => nextRow[header]);
717
+ const nextTrailingEmpty = nextValues
718
+ .slice(headerCount - missingCount)
719
+ .every((value) => isEmptyValue(value));
720
+ const leadValues = nextValues
721
+ .slice(0, missingCount)
722
+ .filter((value) => !isEmptyValue(value));
723
+ const shouldMerge = nextTrailingEmpty
724
+ && leadValues.length > 0
725
+ && (hasOddQuotes(values[lastNonEmpty]) || hasAnyQuotes(values[lastNonEmpty]));
726
+ if (shouldMerge) {
727
+ const toAppend = leadValues.map((value) => String(value));
728
+ if (toAppend.length > 0) {
729
+ const base = isEmptyValue(values[lastNonEmpty]) ? '' : String(values[lastNonEmpty]);
730
+ values[lastNonEmpty] = base ? `${base}\n${toAppend.join('\n')}` : toAppend.join('\n');
731
+ }
732
+ for (let i = 0; i < missingCount; i++) {
733
+ values[lastNonEmpty + 1 + i] = nextValues[missingCount + i];
734
+ }
735
+ const mergedRow = {};
736
+ for (let i = 0; i < headerCount; i++) {
737
+ mergedRow[headers[i]] = values[i];
738
+ }
739
+ merged.push(mergedRow);
740
+ index += 2;
741
+ continue;
742
+ }
743
+ }
744
+ }
745
+ if (index + 1 < rows.length && headerCount >= 6) {
746
+ const nextRow = rows[index + 1];
747
+ if (nextRow && typeof nextRow === 'object') {
748
+ const nextHex = nextRow[headers[4]];
749
+ const nextUserAgentHead = nextRow[headers[2]];
750
+ const nextUserAgentTail = nextRow[headers[3]];
751
+ const shouldMergeUserAgent = isEmptyValue(values[4])
752
+ && isEmptyValue(values[5])
753
+ && isHexColor(nextHex)
754
+ && (looksLikeUserAgent(nextUserAgentHead) || looksLikeUserAgent(nextUserAgentTail));
755
+ if (shouldMergeUserAgent) {
756
+ const addressParts = [values[3], nextRow[headers[0]], nextRow[headers[1]]]
757
+ .filter((value) => !isEmptyValue(value))
758
+ .map((value) => String(value));
759
+ values[3] = addressParts.join('\n');
760
+ const uaHead = isEmptyValue(nextUserAgentHead) ? '' : String(nextUserAgentHead);
761
+ const uaTail = isEmptyValue(nextUserAgentTail) ? '' : String(nextUserAgentTail);
762
+ const joiner = uaHead && uaTail ? (uaTail.startsWith(' ') ? '' : ',') : '';
763
+ values[4] = uaHead + joiner + uaTail;
764
+ values[5] = String(nextHex);
765
+ const mergedRow = {};
766
+ for (let i = 0; i < headerCount; i++) {
767
+ mergedRow[headers[i]] = values[i];
768
+ }
769
+ merged.push(mergedRow);
770
+ index += 2;
771
+ continue;
772
+ }
773
+ }
774
+ }
775
+ merged.push(row);
776
+ index++;
777
+ }
778
+ if (options.normalizeQuotes) {
779
+ return merged.map((row) => normalizeRowQuotes(row, headers));
780
+ }
781
+ return merged;
782
+ }
783
+ /**
784
+ * Парсинг CSV строки в массив объектов
785
+ *
786
+ * @param csvText - CSV текст для парсинга
787
+ * @param options - Опции парсинга
788
+ * @returns Массив объектов
789
+ */
790
+ function csvToJson$1(csvText, options = {}) {
791
+ return safeExecute(() => {
792
+ validateCsvOptions(options);
793
+ if (typeof csvText !== 'string') {
794
+ throw new ValidationError('CSV text must be a string');
795
+ }
796
+ if (csvText.trim() === '') {
797
+ return [];
798
+ }
799
+ // Определение разделителя
800
+ const delimiter = options.delimiter ||
801
+ (options.autoDetect !== false ? autoDetectDelimiter$1(csvText, options.candidates) : ',');
802
+ // Разделение на строки
803
+ const lines = csvText.split('\n').filter(line => line.trim() !== '');
804
+ if (lines.length === 0) {
805
+ return [];
806
+ }
807
+ // Парсинг заголовков
808
+ const headers = lines[0].split(delimiter).map(h => h.trim());
809
+ const { repairRowShifts = true, normalizeQuotes = true } = options || {};
810
+ // Ограничение количества строк
811
+ const maxRows = options.maxRows || Infinity;
812
+ const dataRows = lines.slice(1, Math.min(lines.length, maxRows + 1));
813
+ // Парсинг данных
814
+ const result = [];
815
+ for (let i = 0; i < dataRows.length; i++) {
816
+ const line = dataRows[i];
817
+ const values = line.split(delimiter);
818
+ const row = {};
819
+ for (let j = 0; j < headers.length; j++) {
820
+ const header = headers[j];
821
+ const value = j < values.length ? values[j].trim() : '';
822
+ // Попытка парсинга чисел
823
+ if (/^-?\d+(\.\d+)?$/.test(value)) {
824
+ row[header] = parseFloat(value);
825
+ }
826
+ else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
827
+ row[header] = value.toLowerCase() === 'true';
828
+ }
829
+ else {
830
+ row[header] = value;
831
+ }
832
+ }
833
+ result.push(row);
834
+ }
835
+ if (repairRowShifts) {
836
+ return repairShiftedRows(result, headers, { normalizeQuotes });
837
+ }
838
+ if (normalizeQuotes) {
839
+ return result.map((row) => normalizeRowQuotes(row, headers));
840
+ }
841
+ return result;
842
+ });
843
+ }
844
+ /**
845
+ * Асинхронная версия csvToJson
846
+ */
847
+ async function csvToJsonAsync$1(csvText, options = {}) {
848
+ return csvToJson$1(csvText, options);
849
+ }
850
+ /**
851
+ * Создает итератор для потокового парсинга CSV
852
+ *
853
+ * @param input - CSV текст, File или Blob
854
+ * @param options - Опции парсинга
855
+ * @returns AsyncGenerator
856
+ */
857
+ async function* csvToJsonIterator$1(input, options = {}) {
858
+ validateCsvOptions(options);
859
+ let csvText;
860
+ if (typeof input === 'string') {
861
+ csvText = input;
862
+ }
863
+ else if (input instanceof File || input instanceof Blob) {
864
+ csvText = await input.text();
865
+ }
866
+ else {
867
+ throw new ValidationError('Input must be string, File or Blob');
868
+ }
869
+ if (csvText.trim() === '') {
870
+ return;
871
+ }
872
+ // Определение разделителя
873
+ const delimiter = options.delimiter ||
874
+ (options.autoDetect !== false ? autoDetectDelimiter$1(csvText, options.candidates) : ',');
875
+ // Разделение на строки
876
+ const lines = csvText.split('\n').filter(line => line.trim() !== '');
877
+ if (lines.length === 0) {
878
+ return;
879
+ }
880
+ // Парсинг заголовков
881
+ const headers = lines[0].split(delimiter).map(h => h.trim());
882
+ const { repairRowShifts = true, normalizeQuotes = true } = options || {};
883
+ // Ограничение количества строк
884
+ const maxRows = options.maxRows || Infinity;
885
+ const dataRows = lines.slice(1, Math.min(lines.length, maxRows + 1));
886
+ // Возврат данных по одной строке
887
+ const parsedRows = [];
888
+ for (let i = 0; i < dataRows.length; i++) {
889
+ const line = dataRows[i];
890
+ const values = line.split(delimiter);
891
+ const row = {};
892
+ for (let j = 0; j < headers.length; j++) {
893
+ const header = headers[j];
894
+ const value = j < values.length ? values[j].trim() : '';
895
+ // Try parsing numbers
896
+ if (/^-?\d+(\.\d+)?$/.test(value)) {
897
+ row[header] = parseFloat(value);
898
+ }
899
+ else if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
900
+ row[header] = value.toLowerCase() === 'true';
901
+ }
902
+ else {
903
+ row[header] = value;
904
+ }
905
+ }
906
+ parsedRows.push(row);
907
+ }
908
+ const finalRows = repairRowShifts
909
+ ? repairShiftedRows(parsedRows, headers, { normalizeQuotes })
910
+ : (normalizeQuotes
911
+ ? parsedRows.map((row) => normalizeRowQuotes(row, headers))
912
+ : parsedRows);
913
+ for (const row of finalRows) {
914
+ yield row;
915
+ }
916
+ }
917
+ /**
918
+ * Асинхронная версия csvToJsonIterator (псевдоним)
919
+ */
920
+ const csvToJsonIteratorAsync = csvToJsonIterator$1;
921
+ /**
922
+ * Парсинг CSV с обработкой ошибок
923
+ *
924
+ * @param csvText - CSV текст
925
+ * @param options - Опции парсинга
926
+ * @returns Результат парсинга или null при ошибке
927
+ */
928
+ function parseCsvSafe(csvText, options = {}) {
929
+ try {
930
+ return csvToJson$1(csvText, options);
931
+ }
932
+ catch (error) {
933
+ console.error('CSV parsing error:', error);
934
+ return null;
935
+ }
936
+ }
937
+ /**
938
+ * Асинхронная версия parseCsvSafe
939
+ */
940
+ async function parseCsvSafeAsync(csvText, options = {}) {
941
+ try {
942
+ return await csvToJsonAsync$1(csvText, options);
943
+ }
944
+ catch (error) {
945
+ console.error('CSV parsing error:', error);
946
+ return null;
947
+ }
948
+ }
949
+ // Экспорт для Node.js совместимости
950
+ if (typeof module !== 'undefined' && module.exports) {
951
+ module.exports = {
952
+ csvToJson: csvToJson$1,
953
+ csvToJsonAsync: csvToJsonAsync$1,
954
+ csvToJsonIterator: csvToJsonIterator$1,
955
+ csvToJsonIteratorAsync,
956
+ parseCsvSafe,
957
+ parseCsvSafeAsync,
958
+ autoDetectDelimiter: autoDetectDelimiter$1
959
+ };
960
+ }
961
+
962
+ var csvToJsonBrowser = /*#__PURE__*/Object.freeze({
963
+ __proto__: null,
964
+ csvToJson: csvToJson$1,
965
+ csvToJsonAsync: csvToJsonAsync$1,
966
+ csvToJsonIterator: csvToJsonIterator$1,
967
+ csvToJsonIteratorAsync: csvToJsonIteratorAsync,
968
+ parseCsvSafe: parseCsvSafe,
969
+ parseCsvSafeAsync: parseCsvSafeAsync
970
+ });
971
+
972
+ const PHONE_KEYS = new Set(['phone', 'phonenumber', 'phone_number', 'tel', 'telephone']);
973
+ function isReadableStream(value) {
974
+ return value && typeof value.getReader === 'function';
975
+ }
976
+ function isAsyncIterable(value) {
977
+ return value && typeof value[Symbol.asyncIterator] === 'function';
978
+ }
979
+ function isIterable(value) {
980
+ return value && typeof value[Symbol.iterator] === 'function';
981
+ }
982
+ function createReadableStreamFromIterator(iterator) {
983
+ return new ReadableStream({
984
+ async pull(controller) {
985
+ try {
986
+ const { value, done } = await iterator.next();
987
+ if (done) {
988
+ controller.close();
989
+ return;
990
+ }
991
+ controller.enqueue(value);
992
+ }
993
+ catch (error) {
994
+ controller.error(error);
995
+ }
996
+ },
997
+ cancel() {
998
+ if (iterator.return) {
999
+ iterator.return();
1000
+ }
1001
+ }
1236
1002
  });
1237
- const lines = buffer.split(/\r?\n/);
1238
- buffer = lines.pop() || '';
1239
- for (const line of lines) {
1240
- const trimmed = line.trim();
1241
- if (!trimmed) {
1242
- continue;
1243
- }
1244
- yield JSON.parse(trimmed);
1245
- }
1246
- }
1247
- if (buffer.trim()) {
1248
- yield JSON.parse(buffer.trim());
1249
- }
1250
- }
1251
- async function* normalizeJsonInput(input, options = {}) {
1252
- const format = detectInputFormat(input, options);
1253
- if (Array.isArray(input)) {
1254
- for (const item of input) {
1255
- yield item;
1256
- }
1257
- return;
1258
- }
1259
- if (isAsyncIterable(input)) {
1260
- for await (const item of input) {
1261
- yield item;
1262
- }
1263
- return;
1264
- }
1265
- if (isIterable(input)) {
1266
- for (const item of input) {
1267
- yield item;
1268
- }
1269
- return;
1270
- }
1271
- if (typeof input === 'string') {
1272
- if (format === 'ndjson') {
1273
- yield* parseNdjsonText(input);
1274
- return;
1275
- }
1276
- const parsed = JSON.parse(input);
1277
- if (Array.isArray(parsed)) {
1278
- for (const item of parsed) {
1279
- yield item;
1280
- }
1281
- return;
1282
- }
1283
- yield parsed;
1284
- return;
1285
- }
1286
- if (input instanceof Blob) {
1287
- if (format === 'ndjson') {
1288
- yield* parseNdjsonStream(input.stream());
1289
- return;
1290
- }
1291
- const text = await input.text();
1292
- const parsed = JSON.parse(text);
1293
- if (Array.isArray(parsed)) {
1294
- for (const item of parsed) {
1295
- yield item;
1296
- }
1297
- return;
1298
- }
1299
- yield parsed;
1300
- return;
1301
- }
1302
- if (isReadableStream(input)) {
1303
- if (format !== 'ndjson') {
1304
- throw new ValidationError('ReadableStream input requires inputFormat="ndjson"');
1305
- }
1306
- yield* parseNdjsonStream(input);
1307
- return;
1308
- }
1309
- throw new ValidationError('Input must be an array, iterable, string, Blob, or ReadableStream');
1310
- }
1311
- function validateStreamOptions(options) {
1312
- if (options && typeof options !== 'object') {
1313
- throw new ConfigurationError('Options must be an object');
1314
- }
1315
- if (options !== null && options !== void 0 && options.delimiter && typeof options.delimiter !== 'string') {
1316
- throw new ConfigurationError('Delimiter must be a string');
1317
- }
1318
- if (options !== null && options !== void 0 && options.delimiter && options.delimiter.length !== 1) {
1319
- throw new ConfigurationError('Delimiter must be a single character');
1320
- }
1321
- if (options !== null && options !== void 0 && options.renameMap && typeof options.renameMap !== 'object') {
1322
- throw new ConfigurationError('renameMap must be an object');
1323
- }
1324
- if ((options === null || options === void 0 ? void 0 : options.maxRecords) !== undefined) {
1325
- if (typeof options.maxRecords !== 'number' || options.maxRecords <= 0) {
1326
- throw new ConfigurationError('maxRecords must be a positive number');
1327
- }
1328
- }
1329
- }
1330
- function escapeCsvValue(value, options) {
1331
- const {
1332
- delimiter,
1333
- preventCsvInjection = true,
1334
- rfc4180Compliant = true
1335
- } = options;
1336
- if (value === null || value === undefined || value === '') {
1337
- return '';
1338
- }
1339
- const stringValue = String(value);
1340
- let escapedValue = stringValue;
1341
- if (preventCsvInjection && /^[=+\-@]/.test(stringValue)) {
1342
- escapedValue = "'" + stringValue;
1343
- }
1344
- const needsQuoting = rfc4180Compliant ? escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r') : escapedValue.includes(delimiter) || escapedValue.includes('"') || escapedValue.includes('\n') || escapedValue.includes('\r');
1345
- if (needsQuoting) {
1346
- return `"${escapedValue.replace(/"/g, '""')}"`;
1347
- }
1348
- return escapedValue;
1349
- }
1350
- function buildHeaderState(keys, options) {
1351
- const renameMap = options.renameMap || {};
1352
- const template = options.template || {};
1353
- const originalKeys = Array.isArray(options.headers) ? options.headers : keys;
1354
- const headers = originalKeys.map(key => renameMap[key] || key);
1355
- const reverseRenameMap = {};
1356
- originalKeys.forEach((key, index) => {
1357
- reverseRenameMap[headers[index]] = key;
1358
- });
1359
- let finalHeaders = headers;
1360
- if (Object.keys(template).length > 0) {
1361
- const templateHeaders = Object.keys(template).map(key => renameMap[key] || key);
1362
- const extraHeaders = headers.filter(h => !templateHeaders.includes(h));
1363
- finalHeaders = [...templateHeaders, ...extraHeaders];
1364
- }
1365
- return {
1366
- headers: finalHeaders,
1367
- reverseRenameMap
1368
- };
1003
+ }
1004
+ function detectInputFormat(input, options) {
1005
+ if (options && options.inputFormat) {
1006
+ return options.inputFormat;
1007
+ }
1008
+ if (typeof input === 'string') {
1009
+ const trimmed = input.trim();
1010
+ if (trimmed === '') {
1011
+ return 'unknown';
1012
+ }
1013
+ // Проверка на NDJSON (каждая строка - валидный JSON)
1014
+ if (trimmed.includes('\n')) {
1015
+ const lines = trimmed.split('\n').filter(line => line.trim() !== '');
1016
+ if (lines.length > 0) {
1017
+ try {
1018
+ JSON.parse(lines[0]);
1019
+ return 'ndjson';
1020
+ }
1021
+ catch {
1022
+ // Не NDJSON
1023
+ }
1024
+ }
1025
+ }
1026
+ // Проверка на JSON
1027
+ try {
1028
+ const parsed = JSON.parse(trimmed);
1029
+ if (Array.isArray(parsed) || (parsed && typeof parsed === 'object')) {
1030
+ return 'json';
1031
+ }
1032
+ }
1033
+ catch {
1034
+ // Не JSON
1035
+ }
1036
+ // Проверка на CSV
1037
+ if (trimmed.includes(',') || trimmed.includes(';') || trimmed.includes('\t')) {
1038
+ return 'csv';
1039
+ }
1040
+ }
1041
+ return 'unknown';
1042
+ }
1043
+ function normalizeQuotesInField(value) {
1044
+ // Не нормализуем кавычки в JSON-строках - это ломает структуру JSON
1045
+ // Проверяем, выглядит ли значение как JSON (объект или массив)
1046
+ if ((value.startsWith('{') && value.endsWith('}')) ||
1047
+ (value.startsWith('[') && value.endsWith(']'))) {
1048
+ return value; // Возвращаем как есть для JSON
1049
+ }
1050
+ let normalized = value.replace(/"{2,}/g, '"');
1051
+ // Убираем правило, которое ломает JSON: не заменяем "," на ","
1052
+ // normalized = normalized.replace(/"\s*,\s*"/g, ',');
1053
+ normalized = normalized.replace(/"\n/g, '\n').replace(/\n"/g, '\n');
1054
+ if (normalized.length >= 2 && normalized.startsWith('"') && normalized.endsWith('"')) {
1055
+ normalized = normalized.slice(1, -1);
1056
+ }
1057
+ return normalized;
1058
+ }
1059
+ function normalizePhoneValue(value) {
1060
+ const trimmed = value.trim();
1061
+ if (trimmed === '') {
1062
+ return trimmed;
1063
+ }
1064
+ return trimmed.replace(/["'\\]/g, '');
1065
+ }
1066
+ function normalizeValueForCsv(value, key, normalizeQuotes) {
1067
+ if (!normalizeQuotes || typeof value !== 'string') {
1068
+ return value;
1069
+ }
1070
+ const base = normalizeQuotesInField(value);
1071
+ if (key && PHONE_KEYS.has(String(key).toLowerCase())) {
1072
+ return normalizePhoneValue(base);
1073
+ }
1074
+ return base;
1369
1075
  }
1370
1076
  async function* jsonToCsvChunkIterator(input, options = {}) {
1371
- validateStreamOptions(options);
1372
- const opts = options && typeof options === 'object' ? options : {};
1373
- const {
1374
- delimiter = ';',
1375
- includeHeaders = true,
1376
- maxRecords,
1377
- maxChunkSize = DEFAULT_MAX_CHUNK_SIZE,
1378
- headerMode
1379
- } = opts;
1380
- let headerState = null;
1381
- let buffer = '';
1382
- let recordCount = 0;
1383
- const lineEnding = opts.rfc4180Compliant === false ? '\n' : '\r\n';
1384
- if (Array.isArray(input) && !opts.headers && (!headerMode || headerMode === 'all')) {
1385
- const allKeys = new Set();
1386
- for (const item of input) {
1387
- if (!item || typeof item !== 'object') {
1388
- continue;
1389
- }
1390
- Object.keys(item).forEach(key => allKeys.add(key));
1391
- }
1392
- headerState = buildHeaderState(Array.from(allKeys), opts);
1393
- if (includeHeaders && headerState.headers.length > 0) {
1394
- buffer += headerState.headers.join(delimiter) + lineEnding;
1395
- }
1396
- } else if (Array.isArray(opts.headers)) {
1397
- headerState = buildHeaderState(opts.headers, opts);
1398
- if (includeHeaders && headerState.headers.length > 0) {
1399
- buffer += headerState.headers.join(delimiter) + lineEnding;
1400
- }
1401
- }
1402
- for await (const item of normalizeJsonInput(input, opts)) {
1403
- if (!item || typeof item !== 'object') {
1404
- continue;
1405
- }
1406
- if (!headerState) {
1407
- headerState = buildHeaderState(Object.keys(item), opts);
1408
- if (includeHeaders && headerState.headers.length > 0) {
1409
- buffer += headerState.headers.join(delimiter) + lineEnding;
1410
- }
1411
- }
1412
- recordCount += 1;
1413
- if (maxRecords && recordCount > maxRecords) {
1414
- throw new LimitError(`Data size exceeds maximum limit of ${maxRecords} records`, maxRecords, recordCount);
1415
- }
1416
- const row = headerState.headers.map(header => {
1417
- const originalKey = headerState.reverseRenameMap[header] || header;
1418
- return escapeCsvValue(item[originalKey], {
1419
- delimiter,
1420
- preventCsvInjection: opts.preventCsvInjection !== false,
1421
- rfc4180Compliant: opts.rfc4180Compliant !== false
1422
- });
1423
- }).join(delimiter);
1424
- buffer += row + lineEnding;
1425
- if (buffer.length >= maxChunkSize) {
1426
- yield buffer;
1427
- buffer = '';
1428
- }
1429
- }
1430
- if (buffer.length > 0) {
1431
- yield buffer;
1432
- }
1077
+ const format = detectInputFormat(input, options);
1078
+ if (format === 'csv') {
1079
+ throw new ValidationError('Input appears to be CSV, not JSON');
1080
+ }
1081
+ // Вспомогательная функция для создания асинхронного итератора
1082
+ function toAsyncIterator(iterable) {
1083
+ if (isAsyncIterable(iterable)) {
1084
+ return iterable[Symbol.asyncIterator]();
1085
+ }
1086
+ if (isIterable(iterable)) {
1087
+ const syncIterator = iterable[Symbol.iterator]();
1088
+ return {
1089
+ next: () => Promise.resolve(syncIterator.next()),
1090
+ return: syncIterator.return ? () => Promise.resolve(syncIterator.return()) : undefined,
1091
+ throw: syncIterator.throw ? (error) => Promise.resolve(syncIterator.throw(error)) : undefined
1092
+ };
1093
+ }
1094
+ throw new ValidationError('Input is not iterable');
1095
+ }
1096
+ let iterator;
1097
+ if (isAsyncIterable(input) || isIterable(input)) {
1098
+ iterator = toAsyncIterator(input);
1099
+ }
1100
+ else if (typeof input === 'string') {
1101
+ const parsed = JSON.parse(input);
1102
+ if (Array.isArray(parsed)) {
1103
+ iterator = toAsyncIterator(parsed);
1104
+ }
1105
+ else {
1106
+ iterator = toAsyncIterator([parsed]);
1107
+ }
1108
+ }
1109
+ else if (Array.isArray(input)) {
1110
+ iterator = toAsyncIterator(input);
1111
+ }
1112
+ else {
1113
+ iterator = toAsyncIterator([input]);
1114
+ }
1115
+ const delimiter = options.delimiter || ';';
1116
+ const includeHeaders = options.includeHeaders !== false;
1117
+ const preventInjection = options.preventCsvInjection !== false;
1118
+ const normalizeQuotes = options.normalizeQuotes !== false;
1119
+ const isPotentialFormula = (input) => {
1120
+ let idx = 0;
1121
+ while (idx < input.length) {
1122
+ const code = input.charCodeAt(idx);
1123
+ if (code === 32 || code === 9 || code === 10 || code === 13 || code === 0xfeff) {
1124
+ idx++;
1125
+ continue;
1126
+ }
1127
+ break;
1128
+ }
1129
+ if (idx < input.length && (input[idx] === '"' || input[idx] === "'")) {
1130
+ idx++;
1131
+ while (idx < input.length) {
1132
+ const code = input.charCodeAt(idx);
1133
+ if (code === 32 || code === 9) {
1134
+ idx++;
1135
+ continue;
1136
+ }
1137
+ break;
1138
+ }
1139
+ }
1140
+ if (idx >= input.length) {
1141
+ return false;
1142
+ }
1143
+ const char = input[idx];
1144
+ return char === '=' || char === '+' || char === '-' || char === '@';
1145
+ };
1146
+ let isFirstChunk = true;
1147
+ let headers = [];
1148
+ while (true) {
1149
+ const { value, done } = await iterator.next();
1150
+ if (done)
1151
+ break;
1152
+ const item = value;
1153
+ if (isFirstChunk) {
1154
+ // Извлечение заголовков из первого элемента
1155
+ headers = Object.keys(item);
1156
+ if (includeHeaders) {
1157
+ const headerLine = headers.map(header => {
1158
+ const escaped = header.includes('"') ? `"${header.replace(/"/g, '""')}"` : header;
1159
+ return preventInjection && isPotentialFormula(escaped) ? `'${escaped}` : escaped;
1160
+ }).join(delimiter);
1161
+ yield headerLine + '\n';
1162
+ }
1163
+ isFirstChunk = false;
1164
+ }
1165
+ const row = headers.map(header => {
1166
+ const value = item[header];
1167
+ const normalized = normalizeValueForCsv(value, header, normalizeQuotes);
1168
+ const strValue = normalized === null || normalized === undefined ? '' : String(normalized);
1169
+ if (strValue.includes('"') || strValue.includes('\n') || strValue.includes('\r') || strValue.includes(delimiter)) {
1170
+ return `"${strValue.replace(/"/g, '""')}"`;
1171
+ }
1172
+ if (preventInjection && isPotentialFormula(strValue)) {
1173
+ return `'${strValue}`;
1174
+ }
1175
+ return strValue;
1176
+ }).join(delimiter);
1177
+ yield row + '\n';
1178
+ }
1433
1179
  }
1434
1180
  async function* jsonToNdjsonChunkIterator(input, options = {}) {
1435
- validateStreamOptions(options);
1436
- for await (const item of normalizeJsonInput(input, options)) {
1437
- if (item === undefined) {
1438
- continue;
1181
+ const format = detectInputFormat(input, options);
1182
+ // Вспомогательная функция для создания асинхронного итератора
1183
+ function toAsyncIterator(iterable) {
1184
+ if (isAsyncIterable(iterable)) {
1185
+ return iterable[Symbol.asyncIterator]();
1186
+ }
1187
+ if (isIterable(iterable)) {
1188
+ const syncIterator = iterable[Symbol.iterator]();
1189
+ return {
1190
+ next: () => Promise.resolve(syncIterator.next()),
1191
+ return: syncIterator.return ? () => Promise.resolve(syncIterator.return()) : undefined,
1192
+ throw: syncIterator.throw ? (error) => Promise.resolve(syncIterator.throw(error)) : undefined
1193
+ };
1194
+ }
1195
+ throw new ValidationError('Input is not iterable');
1196
+ }
1197
+ let iterator;
1198
+ if (isAsyncIterable(input) || isIterable(input)) {
1199
+ iterator = toAsyncIterator(input);
1200
+ }
1201
+ else if (typeof input === 'string') {
1202
+ if (format === 'ndjson') {
1203
+ const lines = input.split('\n').filter(line => line.trim() !== '');
1204
+ iterator = toAsyncIterator(lines);
1205
+ }
1206
+ else {
1207
+ const parsed = JSON.parse(input);
1208
+ if (Array.isArray(parsed)) {
1209
+ iterator = toAsyncIterator(parsed);
1210
+ }
1211
+ else {
1212
+ iterator = toAsyncIterator([parsed]);
1213
+ }
1214
+ }
1215
+ }
1216
+ else if (Array.isArray(input)) {
1217
+ iterator = toAsyncIterator(input);
1218
+ }
1219
+ else {
1220
+ iterator = toAsyncIterator([input]);
1221
+ }
1222
+ while (true) {
1223
+ const { value, done } = await iterator.next();
1224
+ if (done)
1225
+ break;
1226
+ let jsonStr;
1227
+ if (typeof value === 'string') {
1228
+ try {
1229
+ // Проверяем, является ли строка валидным JSON
1230
+ JSON.parse(value);
1231
+ jsonStr = value;
1232
+ }
1233
+ catch {
1234
+ // Если нет, сериализуем как JSON
1235
+ jsonStr = JSON.stringify(value);
1236
+ }
1237
+ }
1238
+ else {
1239
+ jsonStr = JSON.stringify(value);
1240
+ }
1241
+ yield jsonStr + '\n';
1439
1242
  }
1440
- yield JSON.stringify(item) + '\n';
1441
- }
1442
1243
  }
1443
1244
  async function* csvToJsonChunkIterator(input, options = {}) {
1444
- const outputFormat = options.outputFormat || 'ndjson';
1445
- const asArray = outputFormat === 'json-array' || outputFormat === 'array' || outputFormat === 'json';
1446
- let first = true;
1447
- if (asArray) {
1448
- yield '[';
1449
- }
1450
- for await (const row of csvToJsonIterator(input, options)) {
1451
- const payload = JSON.stringify(row);
1452
- if (asArray) {
1453
- yield (first ? '' : ',') + payload;
1454
- } else {
1455
- yield payload + '\n';
1456
- }
1457
- first = false;
1458
- }
1459
- if (asArray) {
1460
- yield ']';
1461
- }
1462
- }
1463
- function jsonToCsvStream(input, options = {}) {
1464
- const iterator = jsonToCsvChunkIterator(input, options);
1465
- return createReadableStreamFromIterator(iterator);
1466
- }
1467
- function jsonToNdjsonStream(input, options = {}) {
1468
- const iterator = jsonToNdjsonChunkIterator(input, options);
1469
- return createReadableStreamFromIterator(iterator);
1470
- }
1471
- function csvToJsonStream(input, options = {}) {
1472
- const iterator = csvToJsonChunkIterator(input, options);
1473
- return createReadableStreamFromIterator(iterator);
1245
+ if (typeof input === 'string') {
1246
+ // Используем csvToJsonIterator из csv-to-json-browser
1247
+ yield* csvToJsonIterator$1(input, options);
1248
+ }
1249
+ else if (input instanceof File || input instanceof Blob) {
1250
+ const text = await input.text();
1251
+ yield* csvToJsonIterator$1(text, options);
1252
+ }
1253
+ else if (isReadableStream(input)) {
1254
+ const reader = input.getReader();
1255
+ const decoder = new TextDecoder();
1256
+ let buffer = '';
1257
+ try {
1258
+ while (true) {
1259
+ const { value, done } = await reader.read();
1260
+ if (done)
1261
+ break;
1262
+ buffer += decoder.decode(value, { stream: true });
1263
+ // Обработка буфера по строкам
1264
+ const lines = buffer.split('\n');
1265
+ buffer = lines.pop() || '';
1266
+ // TODO: Реализовать парсинг CSV из чанков
1267
+ // Пока просто возвращаем сырые строки
1268
+ for (const line of lines) {
1269
+ if (line.trim()) {
1270
+ yield { raw: line };
1271
+ }
1272
+ }
1273
+ }
1274
+ // Обработка остатка буфера
1275
+ if (buffer.trim()) {
1276
+ yield { raw: buffer };
1277
+ }
1278
+ }
1279
+ finally {
1280
+ reader.releaseLock();
1281
+ }
1282
+ }
1283
+ else {
1284
+ throw new ValidationError('Unsupported input type for CSV streaming');
1285
+ }
1286
+ }
1287
+ function jsonToCsvStream$1(input, options = {}) {
1288
+ const iterator = jsonToCsvChunkIterator(input, options);
1289
+ return createReadableStreamFromIterator(iterator);
1290
+ }
1291
+ function jsonToNdjsonStream$1(input, options = {}) {
1292
+ const iterator = jsonToNdjsonChunkIterator(input, options);
1293
+ return createReadableStreamFromIterator(iterator);
1294
+ }
1295
+ function csvToJsonStream$1(input, options = {}) {
1296
+ const iterator = csvToJsonChunkIterator(input, options);
1297
+ return createReadableStreamFromIterator(iterator);
1298
+ }
1299
+ /**
1300
+ * Асинхронная версия jsonToCsvStream
1301
+ */
1302
+ async function jsonToCsvStreamAsync(input, options = {}) {
1303
+ return jsonToCsvStream$1(input, options);
1304
+ }
1305
+ /**
1306
+ * Асинхронная версия jsonToNdjsonStream
1307
+ */
1308
+ async function jsonToNdjsonStreamAsync(input, options = {}) {
1309
+ return jsonToNdjsonStream$1(input, options);
1310
+ }
1311
+ /**
1312
+ * Асинхронная версия csvToJsonStream
1313
+ */
1314
+ async function csvToJsonStreamAsync(input, options = {}) {
1315
+ return csvToJsonStream$1(input, options);
1474
1316
  }
1317
+ // Экспорт для Node.js совместимости
1475
1318
  if (typeof module !== 'undefined' && module.exports) {
1476
- module.exports = {
1477
- jsonToCsvStream,
1478
- jsonToNdjsonStream,
1479
- csvToJsonStream
1480
- };
1319
+ module.exports = {
1320
+ jsonToCsvStream: jsonToCsvStream$1,
1321
+ jsonToCsvStreamAsync,
1322
+ jsonToNdjsonStream: jsonToNdjsonStream$1,
1323
+ jsonToNdjsonStreamAsync,
1324
+ csvToJsonStream: csvToJsonStream$1,
1325
+ csvToJsonStreamAsync,
1326
+ createReadableStreamFromIterator
1327
+ };
1481
1328
  }
1482
1329
 
1483
1330
  // Браузерные специфичные функции для jtcsv
1484
1331
  // Функции, которые работают только в браузере
1485
-
1486
-
1487
1332
  /**
1488
1333
  * Скачивает JSON данные как CSV файл
1489
- *
1490
- * @param {Array<Object>} data - Массив объектов для конвертации
1491
- * @param {string} [filename='data.csv'] - Имя файла для скачивания
1492
- * @param {Object} [options] - Опции для jsonToCsv
1493
- * @returns {void}
1494
- *
1334
+ *
1335
+ * @param data - Массив объектов для конвертации
1336
+ * @param filename - Имя файла для скачивания (по умолчанию 'data.csv')
1337
+ * @param options - Опции для jsonToCsv
1338
+ *
1495
1339
  * @example
1496
1340
  * const data = [
1497
1341
  * { id: 1, name: 'John' },
@@ -1500,770 +1344,869 @@ if (typeof module !== 'undefined' && module.exports) {
1500
1344
  * downloadAsCsv(data, 'users.csv', { delimiter: ',' });
1501
1345
  */
1502
1346
  function downloadAsCsv(data, filename = 'data.csv', options = {}) {
1503
- // Проверка что мы в браузере
1504
- if (typeof window === 'undefined') {
1505
- throw new ValidationError('downloadAsCsv() работает только в браузере. Используйте saveAsCsv() в Node.js');
1506
- }
1507
-
1508
- // Валидация имени файла
1509
- if (typeof filename !== 'string' || filename.trim() === '') {
1510
- throw new ValidationError('Filename must be a non-empty string');
1511
- }
1512
-
1513
- // Добавление расширения .csv если его нет
1514
- if (!filename.toLowerCase().endsWith('.csv')) {
1515
- filename += '.csv';
1516
- }
1517
-
1518
- // Конвертация в CSV
1519
- const csv = jsonToCsv(data, options);
1520
-
1521
- // Создание Blob
1522
- const blob = new Blob([csv], {
1523
- type: 'text/csv;charset=utf-8;'
1524
- });
1525
-
1526
- // Создание ссылки для скачивания
1527
- const link = document.createElement('a');
1528
-
1529
- // Создание URL для Blob
1530
- const url = URL.createObjectURL(blob);
1531
-
1532
- // Настройка ссылки
1533
- link.setAttribute('href', url);
1534
- link.setAttribute('download', filename);
1535
- link.style.visibility = 'hidden';
1536
-
1537
- // Добавление в DOM и клик
1538
- document.body.appendChild(link);
1539
- link.click();
1540
-
1541
- // Очистка
1542
- setTimeout(() => {
1347
+ // Проверка что мы в браузере
1348
+ if (typeof window === 'undefined') {
1349
+ throw new ValidationError('downloadAsCsv() работает только в браузере. Используйте saveAsCsv() в Node.js');
1350
+ }
1351
+ // Валидация имени файла
1352
+ if (typeof filename !== 'string' || filename.trim() === '') {
1353
+ throw new ValidationError('Filename must be a non-empty string');
1354
+ }
1355
+ // Добавление расширения .csv если его нет
1356
+ if (!filename.toLowerCase().endsWith('.csv')) {
1357
+ filename += '.csv';
1358
+ }
1359
+ // Конвертация в CSV
1360
+ const csv = jsonToCsv$1(data, options);
1361
+ // Создание Blob
1362
+ const blob = new Blob([csv], {
1363
+ type: 'text/csv;charset=utf-8;'
1364
+ });
1365
+ // Создание ссылки для скачивания
1366
+ const link = document.createElement('a');
1367
+ const url = URL.createObjectURL(blob);
1368
+ link.setAttribute('href', url);
1369
+ link.setAttribute('download', filename);
1370
+ link.style.visibility = 'hidden';
1371
+ document.body.appendChild(link);
1372
+ link.click();
1543
1373
  document.body.removeChild(link);
1544
- URL.revokeObjectURL(url);
1545
- }, 100);
1374
+ // Освобождение URL
1375
+ setTimeout(() => URL.revokeObjectURL(url), 100);
1546
1376
  }
1547
-
1548
1377
  /**
1549
- * Парсит CSV файл из input[type="file"] в JSON
1550
- *
1551
- * @param {File} file - File объект из input
1552
- * @param {Object} [options] - Опции для csvToJson
1553
- * @returns {Promise<Array<Object>>} Promise с JSON данными
1554
- *
1555
- * @example
1556
- * // HTML: <input type="file" id="csvFile" accept=".csv">
1557
- * const fileInput = document.getElementById('csvFile');
1558
- * const json = await parseCsvFile(fileInput.files[0], { delimiter: ',' });
1378
+ * Асинхронная версия downloadAsCsv
1379
+ */
1380
+ async function downloadAsCsvAsync$1(data, filename = 'data.csv', options = {}) {
1381
+ return downloadAsCsv(data, filename, options);
1382
+ }
1383
+ /**
1384
+ * Парсит CSV файл из input[type="file"]
1385
+ *
1386
+ * @param file - File объект из input
1387
+ * @param options - Опции для csvToJson
1388
+ * @returns Promise с распарсенными данными
1559
1389
  */
1560
1390
  async function parseCsvFile(file, options = {}) {
1561
- // Проверка что мы в браузере
1562
- if (typeof window === 'undefined') {
1563
- throw new ValidationError('parseCsvFile() работает только в браузере. Используйте readCsvAsJson() в Node.js');
1564
- }
1565
-
1566
- // Валидация файла
1567
- if (!(file instanceof File)) {
1568
- throw new ValidationError('Input must be a File object');
1569
- }
1570
-
1571
- // Проверка расширения файла
1572
- if (!file.name.toLowerCase().endsWith('.csv')) {
1573
- throw new ValidationError('File must have .csv extension');
1574
- }
1575
-
1576
- // Проверка размера файла (предупреждение для больших файлов)
1577
- const MAX_SIZE_WARNING = 50 * 1024 * 1024; // 50MB
1578
- if (file.size > MAX_SIZE_WARNING && process.env.NODE_ENV !== 'production') {
1579
- console.warn(`⚠️ Warning: Processing large file (${(file.size / 1024 / 1024).toFixed(2)}MB).\n` + '💡 Consider using Web Workers for better performance.\n' + '🔧 Tip: Use parseCSVWithWorker() for files > 10MB.');
1580
- }
1581
- return new Promise((resolve, reject) => {
1582
- const reader = new FileReader();
1583
- reader.onload = function (event) {
1584
- try {
1585
- const csvText = event.target.result;
1586
- const json = csvToJson(csvText, options);
1587
- resolve(json);
1588
- } catch (error) {
1589
- reject(error);
1590
- }
1591
- };
1592
- reader.onerror = function () {
1593
- reject(new ValidationError('Ошибка чтения файла'));
1594
- };
1595
- reader.onabort = function () {
1596
- reject(new ValidationError('Чтение файла прервано'));
1597
- };
1598
-
1599
- // Чтение как текст
1600
- reader.readAsText(file, 'UTF-8');
1601
- });
1391
+ if (!(file instanceof File)) {
1392
+ throw new ValidationError('parseCsvFile() ожидает объект File');
1393
+ }
1394
+ // Чтение файла как текст
1395
+ const text = await file.text();
1396
+ // Парсинг CSV
1397
+ return csvToJson$1(text, options);
1602
1398
  }
1603
-
1604
1399
  /**
1605
- * Stream CSV file as async iterator without full buffering.
1400
+ * Парсит CSV файл потоково
1606
1401
  *
1607
- * @param {File} file - File selected from input
1608
- * @param {Object} [options] - csvToJson options
1609
- * @returns {AsyncGenerator<Object>} Async iterator of rows
1402
+ * @param file - File объект
1403
+ * @param options - Опции для потокового парсинга
1404
+ * @returns AsyncIterator с данными
1610
1405
  */
1611
1406
  function parseCsvFileStream(file, options = {}) {
1612
- if (typeof window === 'undefined') {
1613
- throw new ValidationError('parseCsvFileStream() is browser-only. Use readCsvAsJson() in Node.js');
1614
- }
1615
- if (!(file instanceof File)) {
1616
- throw new ValidationError('Input must be a File object');
1617
- }
1618
- if (!file.name.toLowerCase().endsWith('.csv')) {
1619
- throw new ValidationError('File must have .csv extension');
1620
- }
1621
- return csvToJsonIterator(file, options);
1407
+ if (!(file instanceof File)) {
1408
+ throw new ValidationError('parseCsvFileStream() ожидает объект File');
1409
+ }
1410
+ // Используем csvToJsonIterator из импортированного модуля
1411
+ return csvToJsonIterator$1(file, options);
1412
+ }
1413
+ /**
1414
+ * Создает поток для конвертации JSON в CSV
1415
+ *
1416
+ * @param options - Опции для jsonToCsv
1417
+ * @returns ReadableStream
1418
+ */
1419
+ function jsonToCsvStream(options = {}) {
1420
+ return jsonToCsvStream$1(options);
1421
+ }
1422
+ /**
1423
+ * Создает поток для конвертации JSON в NDJSON
1424
+ *
1425
+ * @param options - Опции для конвертации
1426
+ * @returns ReadableStream
1427
+ */
1428
+ function jsonToNdjsonStream(options = {}) {
1429
+ return jsonToNdjsonStream$1(options);
1430
+ }
1431
+ /**
1432
+ * Создает поток для парсинга CSV в JSON
1433
+ *
1434
+ * @param options - Опции для csvToJson
1435
+ * @returns ReadableStream
1436
+ */
1437
+ function csvToJsonStream(options = {}) {
1438
+ return csvToJsonStream$1(options);
1439
+ }
1440
+ /**
1441
+ * Загружает CSV файл по URL
1442
+ *
1443
+ * @param url - URL CSV файла
1444
+ * @param options - Опции для csvToJson
1445
+ * @returns Promise с распарсенными данными
1446
+ */
1447
+ async function loadCsvFromUrl(url, options = {}) {
1448
+ if (typeof window === 'undefined') {
1449
+ throw new ValidationError('loadCsvFromUrl() работает только в браузере');
1450
+ }
1451
+ const response = await fetch(url);
1452
+ if (!response.ok) {
1453
+ throw new ValidationError(`Failed to load CSV from URL: ${response.status} ${response.statusText}`);
1454
+ }
1455
+ const text = await response.text();
1456
+ return csvToJson$1(text, options);
1457
+ }
1458
+ /**
1459
+ * Асинхронная версия loadCsvFromUrl
1460
+ */
1461
+ async function loadCsvFromUrlAsync(url, options = {}) {
1462
+ return loadCsvFromUrl(url, options);
1463
+ }
1464
+ /**
1465
+ * Экспортирует данные в CSV и открывает в новой вкладке
1466
+ *
1467
+ * @param data - Данные для экспорта
1468
+ * @param options - Опции для jsonToCsv
1469
+ */
1470
+ function openCsvInNewTab(data, options = {}) {
1471
+ if (typeof window === 'undefined') {
1472
+ throw new ValidationError('openCsvInNewTab() работает только в браузере');
1473
+ }
1474
+ const csv = jsonToCsv$1(data, options);
1475
+ const blob = new Blob([csv], { type: 'text/csv;charset=utf-8' });
1476
+ const url = URL.createObjectURL(blob);
1477
+ window.open(url, '_blank');
1478
+ // Освобождение URL через некоторое время
1479
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
1480
+ }
1481
+ /**
1482
+ * Асинхронная версия openCsvInNewTab
1483
+ */
1484
+ async function openCsvInNewTabAsync(data, options = {}) {
1485
+ return openCsvInNewTab(data, options);
1486
+ }
1487
+ /**
1488
+ * Копирует CSV в буфер обмена
1489
+ *
1490
+ * @param data - Данные для копирования
1491
+ * @param options - Опции для jsonToCsv
1492
+ * @returns Promise с результатом копирования
1493
+ */
1494
+ async function copyCsvToClipboard(data, options = {}) {
1495
+ if (typeof window === 'undefined' || !navigator.clipboard) {
1496
+ throw new ValidationError('copyCsvToClipboard() требует поддержки Clipboard API');
1497
+ }
1498
+ const csv = jsonToCsv$1(data, options);
1499
+ try {
1500
+ await navigator.clipboard.writeText(csv);
1501
+ return true;
1502
+ }
1503
+ catch (error) {
1504
+ console.error('Failed to copy to clipboard:', error);
1505
+ return false;
1506
+ }
1507
+ }
1508
+ /**
1509
+ * Сохраняет CSV в localStorage
1510
+ *
1511
+ * @param key - Ключ для сохранения
1512
+ * @param data - Данные для сохранения
1513
+ * @param options - Опции для jsonToCsv
1514
+ */
1515
+ function saveCsvToLocalStorage(key, data, options = {}) {
1516
+ if (typeof window === 'undefined' || !localStorage) {
1517
+ throw new ValidationError('saveCsvToLocalStorage() требует localStorage');
1518
+ }
1519
+ const csv = jsonToCsv$1(data, options);
1520
+ localStorage.setItem(key, csv);
1521
+ }
1522
+ /**
1523
+ * Загружает CSV из localStorage
1524
+ *
1525
+ * @param key - Ключ для загрузки
1526
+ * @param options - Опции для csvToJson
1527
+ * @returns Распарсенные данные или null
1528
+ */
1529
+ function loadCsvFromLocalStorage(key, options = {}) {
1530
+ if (typeof window === 'undefined' || !localStorage) {
1531
+ throw new ValidationError('loadCsvFromLocalStorage() требует localStorage');
1532
+ }
1533
+ const csv = localStorage.getItem(key);
1534
+ if (!csv) {
1535
+ return null;
1536
+ }
1537
+ return csvToJson$1(csv, options);
1538
+ }
1539
+ /**
1540
+ * Асинхронная версия loadCsvFromLocalStorage
1541
+ */
1542
+ async function loadCsvFromLocalStorageAsync(key, options = {}) {
1543
+ return loadCsvFromLocalStorage(key, options);
1622
1544
  }
1623
-
1624
1545
  /**
1625
1546
  * Создает CSV файл из JSON данных (альтернатива downloadAsCsv)
1626
1547
  * Возвращает Blob вместо автоматического скачивания
1627
- *
1628
- * @param {Array<Object>} data - Массив объектов
1629
- * @param {Object} [options] - Опции для jsonToCsv
1630
- * @returns {Blob} CSV Blob
1548
+ *
1549
+ * @param data - Массив объектов
1550
+ * @param options - Опции для jsonToCsv
1551
+ * @returns CSV Blob
1631
1552
  */
1632
1553
  function createCsvBlob(data, options = {}) {
1633
- const csv = jsonToCsv(data, options);
1634
- return new Blob([csv], {
1635
- type: 'text/csv;charset=utf-8;'
1636
- });
1554
+ const csv = jsonToCsv$1(data, options);
1555
+ return new Blob([csv], {
1556
+ type: 'text/csv;charset=utf-8;'
1557
+ });
1558
+ }
1559
+ /**
1560
+ * Асинхронная версия createCsvBlob
1561
+ */
1562
+ async function createCsvBlobAsync(data, options = {}) {
1563
+ return createCsvBlob(data, options);
1637
1564
  }
1638
-
1639
1565
  /**
1640
1566
  * Парсит CSV строку из Blob
1641
- *
1642
- * @param {Blob} blob - CSV Blob
1643
- * @param {Object} [options] - Опции для csvToJson
1644
- * @returns {Promise<Array<Object>>} Promise с JSON данными
1567
+ *
1568
+ * @param blob - CSV Blob
1569
+ * @param options - Опции для csvToJson
1570
+ * @returns Promise с JSON данными
1645
1571
  */
1646
1572
  async function parseCsvBlob(blob, options = {}) {
1647
- if (!(blob instanceof Blob)) {
1648
- throw new ValidationError('Input must be a Blob object');
1649
- }
1650
- return new Promise((resolve, reject) => {
1651
- const reader = new FileReader();
1652
- reader.onload = function (event) {
1653
- try {
1654
- const csvText = event.target.result;
1655
- const json = csvToJson(csvText, options);
1656
- resolve(json);
1657
- } catch (error) {
1658
- reject(error);
1659
- }
1660
- };
1661
- reader.onerror = function () {
1662
- reject(new ValidationError('Ошибка чтения Blob'));
1663
- };
1664
- reader.readAsText(blob, 'UTF-8');
1665
- });
1573
+ if (!(blob instanceof Blob)) {
1574
+ throw new ValidationError('Input must be a Blob object');
1575
+ }
1576
+ return new Promise((resolve, reject) => {
1577
+ const reader = new FileReader();
1578
+ reader.onload = function (event) {
1579
+ try {
1580
+ const csvText = event.target?.result;
1581
+ const json = csvToJson$1(csvText, options);
1582
+ resolve(json);
1583
+ }
1584
+ catch (error) {
1585
+ reject(error);
1586
+ }
1587
+ };
1588
+ reader.onerror = function () {
1589
+ reject(new ValidationError('Ошибка чтения Blob'));
1590
+ };
1591
+ reader.readAsText(blob, 'UTF-8');
1592
+ });
1593
+ }
1594
+ /**
1595
+ * Асинхронная версия parseCsvBlob
1596
+ */
1597
+ async function parseCsvBlobAsync(blob, options = {}) {
1598
+ return parseCsvBlob(blob, options);
1666
1599
  }
1667
-
1668
1600
  // Экспорт для Node.js совместимости
1669
1601
  if (typeof module !== 'undefined' && module.exports) {
1670
- module.exports = {
1671
- downloadAsCsv,
1672
- parseCsvFile,
1673
- parseCsvFileStream,
1674
- createCsvBlob,
1675
- parseCsvBlob,
1676
- jsonToCsvStream,
1677
- jsonToNdjsonStream,
1678
- csvToJsonStream
1679
- };
1602
+ module.exports = {
1603
+ downloadAsCsv,
1604
+ downloadAsCsvAsync: downloadAsCsvAsync$1,
1605
+ parseCsvFile,
1606
+ parseCsvFileStream,
1607
+ createCsvBlob,
1608
+ createCsvBlobAsync,
1609
+ parseCsvBlob,
1610
+ parseCsvBlobAsync,
1611
+ jsonToCsvStream,
1612
+ jsonToNdjsonStream,
1613
+ csvToJsonStream,
1614
+ loadCsvFromUrl,
1615
+ loadCsvFromUrlAsync,
1616
+ openCsvInNewTab,
1617
+ openCsvInNewTabAsync,
1618
+ copyCsvToClipboard,
1619
+ saveCsvToLocalStorage,
1620
+ loadCsvFromLocalStorage,
1621
+ loadCsvFromLocalStorageAsync
1622
+ };
1680
1623
  }
1681
1624
 
1682
1625
  // Worker Pool для параллельной обработки CSV
1683
1626
  // Использует Comlink для простой коммуникации с Web Workers
1684
-
1685
-
1686
1627
  // Проверка поддержки Web Workers
1687
1628
  const WORKERS_SUPPORTED = typeof Worker !== 'undefined';
1688
1629
  function isTransferableBuffer(value) {
1689
- if (!(value instanceof ArrayBuffer)) {
1690
- return false;
1691
- }
1692
- if (typeof SharedArrayBuffer !== 'undefined' && value instanceof SharedArrayBuffer) {
1693
- return false;
1694
- }
1695
- return true;
1696
- }
1697
- function collectTransferables(args) {
1698
- const transferables = [];
1699
- const collectFromValue = value => {
1700
- if (!value) {
1701
- return;
1630
+ if (!(value instanceof ArrayBuffer)) {
1631
+ return false;
1702
1632
  }
1703
- if (isTransferableBuffer(value)) {
1704
- transferables.push(value);
1705
- return;
1633
+ if (typeof SharedArrayBuffer !== 'undefined' && value instanceof SharedArrayBuffer) {
1634
+ return false;
1706
1635
  }
1707
- if (ArrayBuffer.isView(value) && isTransferableBuffer(value.buffer)) {
1708
- transferables.push(value.buffer);
1709
- return;
1710
- }
1711
- if (Array.isArray(value)) {
1712
- value.forEach(collectFromValue);
1713
- }
1714
- };
1715
- args.forEach(collectFromValue);
1716
- return transferables.length ? transferables : null;
1636
+ return true;
1717
1637
  }
1718
-
1719
- /**
1720
- * Опции для Worker Pool
1721
- * @typedef {Object} WorkerPoolOptions
1722
- * @property {number} [workerCount=4] - Количество workers в pool
1723
- * @property {number} [maxQueueSize=100] - Максимальный размер очереди задач
1724
- * @property {boolean} [autoScale=true] - Автоматическое масштабирование pool
1725
- * @property {number} [idleTimeout=60000] - Таймаут простоя worker (мс)
1638
+ function collectTransferables(args) {
1639
+ const transferables = [];
1640
+ const collectFromValue = (value) => {
1641
+ if (!value) {
1642
+ return;
1643
+ }
1644
+ if (isTransferableBuffer(value)) {
1645
+ transferables.push(value);
1646
+ return;
1647
+ }
1648
+ if (ArrayBuffer.isView(value) && isTransferableBuffer(value.buffer)) {
1649
+ transferables.push(value.buffer);
1650
+ return;
1651
+ }
1652
+ if (Array.isArray(value)) {
1653
+ value.forEach(collectFromValue);
1654
+ }
1655
+ };
1656
+ args.forEach(collectFromValue);
1657
+ return transferables.length ? transferables : null;
1658
+ }
1659
+ /**
1660
+ * Опции для Worker Pool
1661
+ * @typedef {Object} WorkerPoolOptions
1662
+ * @property {number} [workerCount=4] - Количество workers в pool
1663
+ * @property {number} [maxQueueSize=100] - Максимальный размер очереди задач
1664
+ * @property {boolean} [autoScale=true] - Автоматическое масштабирование pool
1665
+ * @property {number} [idleTimeout=60000] - Таймаут простоя worker (мс)
1726
1666
  */
1727
-
1728
- /**
1729
- * Статистика Worker Pool
1730
- * @typedef {Object} WorkerPoolStats
1731
- * @property {number} totalWorkers - Всего workers
1732
- * @property {number} activeWorkers - Активные workers
1733
- * @property {number} idleWorkers - Простаивающие workers
1734
- * @property {number} queueSize - Размер очереди
1735
- * @property {number} tasksCompleted - Завершенные задачи
1736
- * @property {number} tasksFailed - Неудачные задачи
1667
+ /**
1668
+ * Статистика Worker Pool
1669
+ * @typedef {Object} WorkerPoolStats
1670
+ * @property {number} totalWorkers - Всего workers
1671
+ * @property {number} activeWorkers - Активные workers
1672
+ * @property {number} idleWorkers - Простаивающие workers
1673
+ * @property {number} queueSize - Размер очереди
1674
+ * @property {number} tasksCompleted - Завершенные задачи
1675
+ * @property {number} tasksFailed - Неудачные задачи
1737
1676
  */
1738
-
1739
- /**
1740
- * Прогресс обработки задачи
1741
- * @typedef {Object} TaskProgress
1742
- * @property {number} processed - Обработано элементов
1743
- * @property {number} total - Всего элементов
1744
- * @property {number} percentage - Процент выполнения
1745
- * @property {number} speed - Скорость обработки (элементов/сек)
1677
+ /**
1678
+ * Прогресс обработки задачи
1679
+ * @typedef {Object} TaskProgress
1680
+ * @property {number} processed - Обработано элементов
1681
+ * @property {number} total - Всего элементов
1682
+ * @property {number} percentage - Процент выполнения
1683
+ * @property {number} speed - Скорость обработки (элементов/сек)
1746
1684
  */
1747
-
1748
- /**
1749
- * Worker Pool для параллельной обработки CSV
1685
+ /**
1686
+ * Worker Pool для параллельной обработки CSV
1750
1687
  */
1751
1688
  class WorkerPool {
1752
- /**
1753
- * Создает новый Worker Pool
1754
- * @param {string} workerScript - URL скрипта worker
1755
- * @param {WorkerPoolOptions} [options] - Опции pool
1756
- */
1757
- constructor(workerScript, options = {}) {
1758
- if (!WORKERS_SUPPORTED) {
1759
- throw new ValidationError('Web Workers не поддерживаются в этом браузере');
1760
- }
1761
- this.workerScript = workerScript;
1762
- this.options = {
1763
- workerCount: 4,
1764
- maxQueueSize: 100,
1765
- autoScale: true,
1766
- idleTimeout: 60000,
1767
- ...options
1768
- };
1769
- this.workers = [];
1770
- this.taskQueue = [];
1771
- this.activeTasks = new Map();
1772
- this.stats = {
1773
- totalWorkers: 0,
1774
- activeWorkers: 0,
1775
- idleWorkers: 0,
1776
- queueSize: 0,
1777
- tasksCompleted: 0,
1778
- tasksFailed: 0
1779
- };
1780
- this.initializeWorkers();
1781
- }
1782
-
1783
- /**
1784
- * Инициализация workers
1785
- * @private
1786
- */
1787
- initializeWorkers() {
1788
- const {
1789
- workerCount
1790
- } = this.options;
1791
- for (let i = 0; i < workerCount; i++) {
1792
- this.createWorker();
1793
- }
1794
- this.updateStats();
1795
- }
1796
-
1797
- /**
1798
- * Создает нового worker
1799
- * @private
1800
- */
1801
- createWorker() {
1802
- try {
1803
- const worker = new Worker(this.workerScript, {
1804
- type: 'module'
1805
- });
1806
- worker.id = `worker-${this.workers.length}`;
1807
- worker.status = 'idle';
1808
- worker.lastUsed = Date.now();
1809
- worker.taskId = null;
1810
-
1811
- // Обработчики событий
1812
- worker.onmessage = event => this.handleWorkerMessage(worker, event);
1813
- worker.onerror = error => this.handleWorkerError(worker, error);
1814
- worker.onmessageerror = error => this.handleWorkerMessageError(worker, error);
1815
- this.workers.push(worker);
1816
- this.stats.totalWorkers++;
1817
- this.stats.idleWorkers++;
1818
- return worker;
1819
- } catch (error) {
1820
- throw new ConfigurationError(`Не удалось создать worker: ${error.message}`);
1821
- }
1822
- }
1823
-
1824
- /**
1825
- * Обработка сообщений от worker
1826
- * @private
1827
- */
1828
- handleWorkerMessage(worker, event) {
1829
- const {
1830
- data
1831
- } = event;
1832
- if (data.type === 'PROGRESS') {
1833
- this.handleProgress(worker, data);
1834
- } else if (data.type === 'RESULT') {
1835
- this.handleResult(worker, data);
1836
- } else if (data.type === 'ERROR') {
1837
- this.handleWorkerTaskError(worker, data);
1838
- }
1839
- }
1840
-
1841
- /**
1842
- * Обработка прогресса задачи
1843
- * @private
1844
- */
1845
- handleProgress(worker, progressData) {
1846
- const taskId = worker.taskId;
1847
- if (taskId && this.activeTasks.has(taskId)) {
1848
- const task = this.activeTasks.get(taskId);
1849
- if (task.onProgress) {
1850
- task.onProgress({
1851
- processed: progressData.processed,
1852
- total: progressData.total,
1853
- percentage: progressData.processed / progressData.total * 100,
1854
- speed: progressData.speed || 0
1855
- });
1856
- }
1689
+ /**
1690
+ * Создает новый Worker Pool
1691
+ * @param {string} workerScript - URL скрипта worker
1692
+ * @param {WorkerPoolOptions} [options] - Опции pool
1693
+ */
1694
+ constructor(workerScript, options = {}) {
1695
+ if (!WORKERS_SUPPORTED) {
1696
+ throw new ValidationError('Web Workers не поддерживаются в этом браузере');
1697
+ }
1698
+ this.workerScript = workerScript;
1699
+ this.options = {
1700
+ workerCount: 4,
1701
+ maxQueueSize: 100,
1702
+ autoScale: true,
1703
+ idleTimeout: 60000,
1704
+ ...options
1705
+ };
1706
+ this.workers = [];
1707
+ this.taskQueue = [];
1708
+ this.activeTasks = new Map();
1709
+ this.stats = {
1710
+ totalWorkers: 0,
1711
+ activeWorkers: 0,
1712
+ idleWorkers: 0,
1713
+ queueSize: 0,
1714
+ tasksCompleted: 0,
1715
+ tasksFailed: 0
1716
+ };
1717
+ this.initializeWorkers();
1857
1718
  }
1858
- }
1859
-
1860
- /**
1861
- * Обработка результата задачи
1862
- * @private
1863
- */
1864
- handleResult(worker, resultData) {
1865
- const taskId = worker.taskId;
1866
- if (taskId && this.activeTasks.has(taskId)) {
1867
- const task = this.activeTasks.get(taskId);
1868
-
1869
- // Освобождение worker
1870
- worker.status = 'idle';
1871
- worker.lastUsed = Date.now();
1872
- worker.taskId = null;
1873
- this.stats.activeWorkers--;
1874
- this.stats.idleWorkers++;
1875
-
1876
- // Завершение задачи
1877
- task.resolve(resultData.data);
1878
- this.activeTasks.delete(taskId);
1879
- this.stats.tasksCompleted++;
1880
-
1881
- // Обработка следующей задачи в очереди
1882
- this.processQueue();
1883
- this.updateStats();
1719
+ /**
1720
+ * Инициализация workers
1721
+ * @private
1722
+ */
1723
+ initializeWorkers() {
1724
+ const { workerCount } = this.options;
1725
+ for (let i = 0; i < workerCount; i++) {
1726
+ this.createWorker();
1727
+ }
1728
+ this.updateStats();
1884
1729
  }
1885
- }
1886
-
1887
- /**
1888
- * Обработка ошибки задачи
1889
- * @private
1890
- */
1891
- handleWorkerTaskError(worker, errorData) {
1892
- const taskId = worker.taskId;
1893
- if (taskId && this.activeTasks.has(taskId)) {
1894
- const task = this.activeTasks.get(taskId);
1895
-
1896
- // Освобождение worker
1897
- worker.status = 'idle';
1898
- worker.lastUsed = Date.now();
1899
- worker.taskId = null;
1900
- this.stats.activeWorkers--;
1901
- this.stats.idleWorkers++;
1902
-
1903
- // Завершение с ошибкой
1904
- const workerError = new Error(errorData.message || 'Ошибка в worker');
1905
- if (errorData.code) {
1906
- workerError.code = errorData.code;
1907
- }
1908
- if (errorData.details) {
1909
- workerError.details = errorData.details;
1910
- }
1911
- task.reject(workerError);
1912
- this.activeTasks.delete(taskId);
1913
- this.stats.tasksFailed++;
1914
-
1915
- // Обработка следующей задачи
1916
- this.processQueue();
1917
- this.updateStats();
1730
+ /**
1731
+ * Создает нового worker
1732
+ * @private
1733
+ */
1734
+ createWorker() {
1735
+ try {
1736
+ const worker = new Worker(this.workerScript, { type: 'module' });
1737
+ worker.id = `worker-${this.workers.length}`;
1738
+ worker.status = 'idle';
1739
+ worker.lastUsed = Date.now();
1740
+ worker.taskId = null;
1741
+ // Обработчики событий
1742
+ worker.onmessage = (event) => this.handleWorkerMessage(worker, event);
1743
+ worker.onerror = (error) => this.handleWorkerError(worker, error);
1744
+ worker.onmessageerror = (error) => this.handleWorkerMessageError(worker, error);
1745
+ this.workers.push(worker);
1746
+ this.stats.totalWorkers++;
1747
+ this.stats.idleWorkers++;
1748
+ return worker;
1749
+ }
1750
+ catch (error) {
1751
+ throw new ConfigurationError(`Не удалось создать worker: ${error.message}`);
1752
+ }
1918
1753
  }
1919
- }
1920
-
1921
- /**
1922
- * Обработка ошибок worker
1923
- * @private
1924
- */
1925
- handleWorkerError(worker, error) {
1926
- console.error(`Worker ${worker.id} error:`, error);
1927
-
1928
- // Перезапуск worker
1929
- this.restartWorker(worker);
1930
- }
1931
-
1932
- /**
1933
- * Обработка ошибок сообщений
1934
- * @private
1935
- */
1936
- handleWorkerMessageError(worker, error) {
1937
- console.error(`Worker ${worker.id} message error:`, error);
1938
- }
1939
-
1940
- /**
1941
- * Перезапуск worker
1942
- * @private
1943
- */
1944
- restartWorker(worker) {
1945
- const index = this.workers.indexOf(worker);
1946
- if (index !== -1) {
1947
- // Завершение старого worker
1948
- worker.terminate();
1949
-
1950
- // Удаление из статистики
1951
- if (worker.status === 'active') {
1952
- this.stats.activeWorkers--;
1953
- } else {
1754
+ /**
1755
+ * Обработка сообщений от worker
1756
+ * @private
1757
+ */
1758
+ handleWorkerMessage(worker, event) {
1759
+ const { data } = event;
1760
+ if (data.type === 'PROGRESS') {
1761
+ this.handleProgress(worker, data);
1762
+ }
1763
+ else if (data.type === 'RESULT') {
1764
+ this.handleResult(worker, data);
1765
+ }
1766
+ else if (data.type === 'ERROR') {
1767
+ this.handleWorkerTaskError(worker, data);
1768
+ }
1769
+ }
1770
+ /**
1771
+ * Обработка прогресса задачи
1772
+ * @private
1773
+ */
1774
+ handleProgress(worker, progressData) {
1775
+ const taskId = worker.taskId;
1776
+ if (taskId && this.activeTasks.has(taskId)) {
1777
+ const task = this.activeTasks.get(taskId);
1778
+ if (task.onProgress) {
1779
+ task.onProgress({
1780
+ processed: progressData.processed,
1781
+ total: progressData.total,
1782
+ percentage: (progressData.processed / progressData.total) * 100,
1783
+ speed: progressData.speed || 0
1784
+ });
1785
+ }
1786
+ }
1787
+ }
1788
+ /**
1789
+ * Обработка результата задачи
1790
+ * @private
1791
+ */
1792
+ handleResult(worker, resultData) {
1793
+ const taskId = worker.taskId;
1794
+ if (taskId && this.activeTasks.has(taskId)) {
1795
+ const task = this.activeTasks.get(taskId);
1796
+ // Освобождение worker
1797
+ worker.status = 'idle';
1798
+ worker.lastUsed = Date.now();
1799
+ worker.taskId = null;
1800
+ this.stats.activeWorkers--;
1801
+ this.stats.idleWorkers++;
1802
+ // Завершение задачи
1803
+ task.resolve(resultData.data);
1804
+ this.activeTasks.delete(taskId);
1805
+ this.stats.tasksCompleted++;
1806
+ // Обработка следующей задачи в очереди
1807
+ this.processQueue();
1808
+ this.updateStats();
1809
+ }
1810
+ }
1811
+ /**
1812
+ * Обработка ошибки задачи
1813
+ * @private
1814
+ */
1815
+ handleWorkerTaskError(worker, errorData) {
1816
+ const taskId = worker.taskId;
1817
+ if (taskId && this.activeTasks.has(taskId)) {
1818
+ const task = this.activeTasks.get(taskId);
1819
+ // Освобождение worker
1820
+ worker.status = 'idle';
1821
+ worker.lastUsed = Date.now();
1822
+ worker.taskId = null;
1823
+ this.stats.activeWorkers--;
1824
+ this.stats.idleWorkers++;
1825
+ // Завершение с ошибкой
1826
+ const workerError = new Error(errorData.message || 'Ошибка в worker');
1827
+ if (errorData.code) {
1828
+ workerError.code = errorData.code;
1829
+ }
1830
+ if (errorData.details) {
1831
+ workerError.details = errorData.details;
1832
+ }
1833
+ task.reject(workerError);
1834
+ this.activeTasks.delete(taskId);
1835
+ this.stats.tasksFailed++;
1836
+ // Обработка следующей задачи
1837
+ this.processQueue();
1838
+ this.updateStats();
1839
+ }
1840
+ }
1841
+ /**
1842
+ * Обработка ошибок worker
1843
+ * @private
1844
+ */
1845
+ handleWorkerError(worker, error) {
1846
+ console.error(`Worker ${worker.id} error:`, error);
1847
+ // Перезапуск worker
1848
+ this.restartWorker(worker);
1849
+ }
1850
+ /**
1851
+ * Обработка ошибок сообщений
1852
+ * @private
1853
+ */
1854
+ handleWorkerMessageError(worker, error) {
1855
+ console.error(`Worker ${worker.id} message error:`, error);
1856
+ }
1857
+ /**
1858
+ * Перезапуск worker
1859
+ * @private
1860
+ */
1861
+ restartWorker(worker) {
1862
+ const index = this.workers.indexOf(worker);
1863
+ if (index !== -1) {
1864
+ // Завершение старого worker
1865
+ worker.terminate();
1866
+ // Удаление из статистики
1867
+ if (worker.status === 'active') {
1868
+ this.stats.activeWorkers--;
1869
+ }
1870
+ else {
1871
+ this.stats.idleWorkers--;
1872
+ }
1873
+ this.stats.totalWorkers--;
1874
+ // Создание нового worker
1875
+ const newWorker = this.createWorker();
1876
+ this.workers[index] = newWorker;
1877
+ // Перезапуск задачи если была активна
1878
+ if (worker.taskId && this.activeTasks.has(worker.taskId)) {
1879
+ const task = this.activeTasks.get(worker.taskId);
1880
+ this.executeTask(newWorker, task);
1881
+ }
1882
+ }
1883
+ }
1884
+ /**
1885
+ * Выполнение задачи на worker
1886
+ * @private
1887
+ */
1888
+ executeTask(worker, task) {
1889
+ worker.status = 'active';
1890
+ worker.lastUsed = Date.now();
1891
+ worker.taskId = task.id;
1954
1892
  this.stats.idleWorkers--;
1955
- }
1956
- this.stats.totalWorkers--;
1957
-
1958
- // Создание нового worker
1959
- const newWorker = this.createWorker();
1960
- this.workers[index] = newWorker;
1961
-
1962
- // Перезапуск задачи если была активна
1963
- if (worker.taskId && this.activeTasks.has(worker.taskId)) {
1964
- const task = this.activeTasks.get(worker.taskId);
1965
- this.executeTask(newWorker, task);
1966
- }
1893
+ this.stats.activeWorkers++;
1894
+ // Отправка задачи в worker
1895
+ const payload = {
1896
+ type: 'EXECUTE',
1897
+ taskId: task.id,
1898
+ method: task.method,
1899
+ args: task.args,
1900
+ options: task.options
1901
+ };
1902
+ if (task.transferList && task.transferList.length) {
1903
+ worker.postMessage(payload, task.transferList);
1904
+ }
1905
+ else {
1906
+ worker.postMessage(payload);
1907
+ }
1967
1908
  }
1968
- }
1969
-
1970
- /**
1971
- * Выполнение задачи на worker
1972
- * @private
1973
- */
1974
- executeTask(worker, task) {
1975
- worker.status = 'active';
1976
- worker.lastUsed = Date.now();
1977
- worker.taskId = task.id;
1978
- this.stats.idleWorkers--;
1979
- this.stats.activeWorkers++;
1980
-
1981
- // Отправка задачи в worker
1982
- const payload = {
1983
- type: 'EXECUTE',
1984
- taskId: task.id,
1985
- method: task.method,
1986
- args: task.args,
1987
- options: task.options
1988
- };
1989
- if (task.transferList && task.transferList.length) {
1990
- worker.postMessage(payload, task.transferList);
1991
- } else {
1992
- worker.postMessage(payload);
1909
+ /**
1910
+ * Обработка очереди задач
1911
+ * @private
1912
+ */
1913
+ processQueue() {
1914
+ if (this.taskQueue.length === 0) {
1915
+ return;
1916
+ }
1917
+ while (this.taskQueue.length > 0) {
1918
+ const idleWorker = this.workers.find(w => w.status === 'idle');
1919
+ if (!idleWorker) {
1920
+ if (this.options.autoScale && this.workers.length < this.options.maxQueueSize) {
1921
+ this.createWorker();
1922
+ continue;
1923
+ }
1924
+ break;
1925
+ }
1926
+ const task = this.taskQueue.shift();
1927
+ this.stats.queueSize--;
1928
+ this.executeTask(idleWorker, task);
1929
+ }
1930
+ this.updateStats();
1931
+ }
1932
+ /**
1933
+ * Обновление статистики
1934
+ * @private
1935
+ */
1936
+ updateStats() {
1937
+ this.stats.queueSize = this.taskQueue.length;
1938
+ }
1939
+ /**
1940
+ * Выполнение задачи через pool
1941
+ * @param {string} method - Метод для вызова в worker
1942
+ * @param {Array} args - Аргументы метода
1943
+ * @param {Object} [options] - Опции задачи
1944
+ * @param {Function} [onProgress] - Callback прогресса
1945
+ * @returns {Promise<unknown>} Результат выполнения
1946
+ */
1947
+ async exec(method, args = [], options = {}, onProgress = null) {
1948
+ return new Promise((resolve, reject) => {
1949
+ // Проверка размера очереди
1950
+ if (this.taskQueue.length >= this.options.maxQueueSize) {
1951
+ reject(new Error('Очередь задач переполнена'));
1952
+ return;
1953
+ }
1954
+ // Создание задачи
1955
+ const taskId = `task-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
1956
+ const { transfer, ...taskOptions } = options || {};
1957
+ const transferList = transfer || collectTransferables(args);
1958
+ const task = {
1959
+ id: taskId,
1960
+ method,
1961
+ args,
1962
+ options: taskOptions,
1963
+ transferList,
1964
+ onProgress,
1965
+ resolve,
1966
+ reject,
1967
+ createdAt: Date.now()
1968
+ };
1969
+ // Добавление в очередь
1970
+ this.taskQueue.push(task);
1971
+ this.stats.queueSize++;
1972
+ // Запуск обработки очереди
1973
+ this.processQueue();
1974
+ this.updateStats();
1975
+ });
1976
+ }
1977
+ /**
1978
+ * Получение статистики pool
1979
+ * @returns {WorkerPoolStats} Статистика
1980
+ */
1981
+ getStats() {
1982
+ return { ...this.stats };
1983
+ }
1984
+ /**
1985
+ * Очистка простаивающих workers
1986
+ */
1987
+ cleanupIdleWorkers() {
1988
+ const now = Date.now();
1989
+ const { idleTimeout } = this.options;
1990
+ for (let i = this.workers.length - 1; i >= 0; i--) {
1991
+ const worker = this.workers[i];
1992
+ if (worker.status === 'idle' && (now - worker.lastUsed) > idleTimeout) {
1993
+ // Сохранение минимального количества workers
1994
+ if (this.workers.length > 1) {
1995
+ worker.terminate();
1996
+ this.workers.splice(i, 1);
1997
+ this.stats.totalWorkers--;
1998
+ this.stats.idleWorkers--;
1999
+ }
2000
+ }
2001
+ }
2002
+ }
2003
+ /**
2004
+ * Завершение всех workers
2005
+ */
2006
+ terminate() {
2007
+ this.workers.forEach(worker => {
2008
+ worker.terminate();
2009
+ });
2010
+ this.workers = [];
2011
+ this.taskQueue = [];
2012
+ this.activeTasks.clear();
2013
+ // Сброс статистики
2014
+ this.stats = {
2015
+ totalWorkers: 0,
2016
+ activeWorkers: 0,
2017
+ idleWorkers: 0,
2018
+ queueSize: 0,
2019
+ tasksCompleted: 0,
2020
+ tasksFailed: 0
2021
+ };
1993
2022
  }
1994
- }
1995
-
1996
- /**
1997
- * Обработка очереди задач
1998
- * @private
1999
- */
2000
- processQueue() {
2001
- if (this.taskQueue.length === 0) {
2002
- return;
2003
- }
2004
- while (this.taskQueue.length > 0) {
2005
- const idleWorker = this.workers.find(w => w.status === 'idle');
2006
- if (!idleWorker) {
2007
- if (this.options.autoScale && this.workers.length < this.options.maxQueueSize) {
2008
- this.createWorker();
2009
- continue;
2010
- }
2011
- break;
2012
- }
2013
- const task = this.taskQueue.shift();
2014
- this.stats.queueSize--;
2015
- this.executeTask(idleWorker, task);
2016
- }
2017
- this.updateStats();
2018
- }
2019
-
2020
- /**
2021
- * Обновление статистики
2022
- * @private
2023
- */
2024
- updateStats() {
2025
- this.stats.queueSize = this.taskQueue.length;
2026
- }
2027
-
2028
- /**
2029
- * Выполнение задачи через pool
2030
- * @param {string} method - Метод для вызова в worker
2031
- * @param {Array} args - Аргументы метода
2032
- * @param {Object} [options] - Опции задачи
2033
- * @param {Function} [onProgress] - Callback прогресса
2034
- * @returns {Promise<any>} Результат выполнения
2035
- */
2036
- async exec(method, args = [], options = {}, onProgress = null) {
2037
- return new Promise((resolve, reject) => {
2038
- // Проверка размера очереди
2039
- if (this.taskQueue.length >= this.options.maxQueueSize) {
2040
- reject(new Error('Очередь задач переполнена'));
2041
- return;
2042
- }
2043
-
2044
- // Создание задачи
2045
- const taskId = `task-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
2046
- const {
2047
- transfer,
2048
- ...taskOptions
2049
- } = options || {};
2050
- const transferList = transfer || collectTransferables(args);
2051
- const task = {
2052
- id: taskId,
2053
- method,
2054
- args,
2055
- options: taskOptions,
2056
- transferList,
2057
- onProgress,
2058
- resolve,
2059
- reject,
2060
- createdAt: Date.now()
2061
- };
2062
-
2063
- // Добавление в очередь
2064
- this.taskQueue.push(task);
2065
- this.stats.queueSize++;
2066
-
2067
- // Запуск обработки очереди
2068
- this.processQueue();
2069
- this.updateStats();
2070
- });
2071
- }
2072
-
2073
- /**
2074
- * Получение статистики pool
2075
- * @returns {WorkerPoolStats} Статистика
2076
- */
2077
- getStats() {
2078
- return {
2079
- ...this.stats
2080
- };
2081
- }
2082
-
2083
- /**
2084
- * Очистка простаивающих workers
2085
- */
2086
- cleanupIdleWorkers() {
2087
- const now = Date.now();
2088
- const {
2089
- idleTimeout
2090
- } = this.options;
2091
- for (let i = this.workers.length - 1; i >= 0; i--) {
2092
- const worker = this.workers[i];
2093
- if (worker.status === 'idle' && now - worker.lastUsed > idleTimeout) {
2094
- // Сохранение минимального количества workers
2095
- if (this.workers.length > 1) {
2096
- worker.terminate();
2097
- this.workers.splice(i, 1);
2098
- this.stats.totalWorkers--;
2099
- this.stats.idleWorkers--;
2100
- }
2101
- }
2102
- }
2103
- }
2104
-
2105
- /**
2106
- * Завершение всех workers
2107
- */
2108
- terminate() {
2109
- this.workers.forEach(worker => {
2110
- worker.terminate();
2111
- });
2112
- this.workers = [];
2113
- this.taskQueue = [];
2114
- this.activeTasks.clear();
2115
-
2116
- // Сброс статистики
2117
- this.stats = {
2118
- totalWorkers: 0,
2119
- activeWorkers: 0,
2120
- idleWorkers: 0,
2121
- queueSize: 0,
2122
- tasksCompleted: 0,
2123
- tasksFailed: 0
2124
- };
2125
- }
2126
2023
  }
2127
-
2128
- /**
2129
- * Создает Worker Pool для обработки CSV
2130
- * @param {WorkerPoolOptions} [options] - Опции pool
2131
- * @returns {WorkerPool} Worker Pool
2024
+ /**
2025
+ * Создает Worker Pool для обработки CSV
2026
+ * @param {WorkerPoolOptions} [options] - Опции pool
2027
+ * @returns {WorkerPool} Worker Pool
2132
2028
  */
2133
2029
  function createWorkerPool(options = {}) {
2134
- // Используем встроенный worker скрипт
2135
- const workerScript = new URL('./csv-parser.worker.js', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('jtcsv.cjs.js', document.baseURI).href))).href;
2136
- return new WorkerPool(workerScript, options);
2030
+ // Используем встроенный worker скрипт
2031
+ const baseUrl = typeof document !== 'undefined'
2032
+ ? document.baseURI
2033
+ : (typeof self !== 'undefined' && self.location
2034
+ ? self.location.href
2035
+ : '');
2036
+ const workerScript = new URL('./csv-parser.worker.js', baseUrl).href;
2037
+ return new WorkerPool(workerScript, options);
2137
2038
  }
2138
-
2139
- /**
2140
- * Парсит CSV с использованием Web Workers
2141
- * @param {string|File} csvInput - CSV строка или File объект
2142
- * @param {Object} [options] - Опции парсинга
2143
- * @param {Function} [onProgress] - Callback прогресса
2144
- * @returns {Promise<Array<Object>>} JSON данные
2039
+ /**
2040
+ * Парсит CSV с использованием Web Workers
2041
+ * @param {string|File} csvInput - CSV строка или File объект
2042
+ * @param {Object} [options] - Опции парсинга
2043
+ * @param {Function} [onProgress] - Callback прогресса
2044
+ * @returns {Promise<Array<Object>>} JSON данные
2145
2045
  */
2146
2046
  async function parseCSVWithWorker(csvInput, options = {}, onProgress = null) {
2147
- // Создание pool если нужно
2148
- if (!parseCSVWithWorker.pool) {
2149
- parseCSVWithWorker.pool = createWorkerPool();
2150
- }
2151
- const pool = parseCSVWithWorker.pool;
2152
-
2153
- // Подготовка CSV строки
2154
- // ?????????? CSV ??????
2155
- let csvPayload = csvInput;
2156
- let transfer = null;
2157
- if (csvInput instanceof File) {
2158
- const buffer = await readFileAsArrayBuffer(csvInput);
2159
- csvPayload = new Uint8Array(buffer);
2160
- transfer = [buffer];
2161
- } else if (csvInput instanceof ArrayBuffer) {
2162
- csvPayload = csvInput;
2163
- transfer = [csvInput];
2164
- } else if (ArrayBuffer.isView(csvInput)) {
2165
- csvPayload = csvInput;
2166
- if (csvInput.buffer instanceof ArrayBuffer) {
2167
- transfer = [csvInput.buffer];
2168
- }
2169
- } else if (typeof csvInput !== 'string') {
2170
- throw new ValidationError('Input must be a CSV string, File, or ArrayBuffer');
2171
- }
2172
-
2173
- // ????????? ?????? ????? pool
2174
- const execOptions = transfer ? {
2175
- transfer
2176
- } : {};
2177
- return pool.exec('parseCSV', [csvPayload, options], execOptions, onProgress);
2047
+ // Создание pool если нужно
2048
+ const poolHolder = parseCSVWithWorker;
2049
+ if (!poolHolder.pool) {
2050
+ poolHolder.pool = createWorkerPool();
2051
+ }
2052
+ const pool = poolHolder.pool;
2053
+ // Подготовка CSV строки
2054
+ // ?????????? CSV ??????
2055
+ let csvPayload = csvInput;
2056
+ let transfer = null;
2057
+ if (csvInput instanceof File) {
2058
+ const buffer = await readFileAsArrayBuffer(csvInput);
2059
+ csvPayload = new Uint8Array(buffer);
2060
+ transfer = [buffer];
2061
+ }
2062
+ else if (csvInput instanceof ArrayBuffer) {
2063
+ csvPayload = csvInput;
2064
+ transfer = [csvInput];
2065
+ }
2066
+ else if (ArrayBuffer.isView(csvInput)) {
2067
+ csvPayload = csvInput;
2068
+ if (csvInput.buffer instanceof ArrayBuffer) {
2069
+ transfer = [csvInput.buffer];
2070
+ }
2071
+ }
2072
+ else if (typeof csvInput !== 'string') {
2073
+ throw new ValidationError('Input must be a CSV string, File, or ArrayBuffer');
2074
+ }
2075
+ // ????????? ?????? ????? pool
2076
+ const execOptions = transfer ? { transfer } : {};
2077
+ return pool.exec('parseCSV', [csvPayload, options], execOptions, onProgress);
2178
2078
  }
2179
-
2180
- /**
2181
- * Чтение файла как текст
2182
- * @private
2079
+ /**
2080
+ * Чтение файла как текст
2081
+ * @private
2183
2082
  */
2184
2083
  async function readFileAsArrayBuffer(file) {
2185
- return new Promise((resolve, reject) => {
2186
- const reader = new FileReader();
2187
- reader.onload = event => resolve(event.target.result);
2188
- reader.onerror = error => reject(error);
2189
- reader.readAsArrayBuffer(file);
2190
- });
2084
+ return new Promise((resolve, reject) => {
2085
+ const reader = new FileReader();
2086
+ reader.onload = (event) => resolve(event.target.result);
2087
+ reader.onerror = (error) => reject(error);
2088
+ reader.readAsArrayBuffer(file);
2089
+ });
2191
2090
  }
2192
-
2193
2091
  // Экспорт для Node.js совместимости
2194
2092
  if (typeof module !== 'undefined' && module.exports) {
2195
- module.exports = {
2196
- WorkerPool,
2197
- createWorkerPool,
2198
- parseCSVWithWorker
2199
- };
2093
+ module.exports = {
2094
+ WorkerPool,
2095
+ createWorkerPool,
2096
+ parseCSVWithWorker
2097
+ };
2200
2098
  }
2201
2099
 
2202
2100
  var workerPool = /*#__PURE__*/Object.freeze({
2203
- __proto__: null,
2204
- WorkerPool: WorkerPool,
2205
- createWorkerPool: createWorkerPool,
2206
- parseCSVWithWorker: parseCSVWithWorker
2101
+ __proto__: null,
2102
+ WorkerPool: WorkerPool,
2103
+ createWorkerPool: createWorkerPool,
2104
+ parseCSVWithWorker: parseCSVWithWorker
2207
2105
  });
2208
2106
 
2209
2107
  // Браузерный entry point для jtcsv
2210
2108
  // Экспортирует все функции с поддержкой браузера
2211
-
2109
+ const { jsonToCsv, preprocessData, deepUnwrap } = jsonToCsvBrowser;
2110
+ const { csvToJson, csvToJsonIterator, autoDetectDelimiter } = csvToJsonBrowser;
2111
+ /**
2112
+ * Ленивая инициализация Worker Pool
2113
+ */
2212
2114
  async function createWorkerPoolLazy(options = {}) {
2213
- const mod = await Promise.resolve().then(function () { return workerPool; });
2214
- return mod.createWorkerPool(options);
2115
+ const mod = await Promise.resolve().then(function () { return workerPool; });
2116
+ return mod.createWorkerPool(options);
2215
2117
  }
2216
- async function parseCSVWithWorkerLazy(csvInput, options = {}, onProgress = null) {
2217
- const mod = await Promise.resolve().then(function () { return workerPool; });
2218
- return mod.parseCSVWithWorker(csvInput, options, onProgress);
2118
+ /**
2119
+ * Ленивый парсинг CSV с использованием Worker
2120
+ */
2121
+ async function parseCSVWithWorkerLazy(csvInput, options = {}, onProgress) {
2122
+ const mod = await Promise.resolve().then(function () { return workerPool; });
2123
+ return mod.parseCSVWithWorker(csvInput, options, onProgress);
2124
+ }
2125
+ /**
2126
+ * Асинхронная версия jsonToCsv
2127
+ */
2128
+ async function jsonToCsvAsync(data, options = {}) {
2129
+ return jsonToCsv(data, options);
2130
+ }
2131
+ /**
2132
+ * Асинхронная версия csvToJson
2133
+ */
2134
+ async function csvToJsonAsync(csv, options = {}) {
2135
+ return csvToJson(csv, options);
2136
+ }
2137
+ /**
2138
+ * Асинхронная версия parseCsvFile
2139
+ */
2140
+ async function parseCsvFileAsync(file, options = {}) {
2141
+ return parseCsvFile(file, options);
2142
+ }
2143
+ /**
2144
+ * Асинхронная версия autoDetectDelimiter
2145
+ */
2146
+ async function autoDetectDelimiterAsync(csv) {
2147
+ return autoDetectDelimiter(csv);
2148
+ }
2149
+ /**
2150
+ * Асинхронная версия downloadAsCsv
2151
+ */
2152
+ async function downloadAsCsvAsync(data, filename = 'export.csv', options = {}) {
2153
+ return downloadAsCsv(data, filename, options);
2219
2154
  }
2220
-
2221
2155
  // Основной экспорт
2222
2156
  const jtcsv = {
2223
- // JSON to CSV функции
2224
- jsonToCsv,
2225
- preprocessData,
2226
- downloadAsCsv,
2227
- deepUnwrap,
2228
- // CSV to JSON функции
2229
- csvToJson,
2230
- csvToJsonIterator,
2231
- parseCsvFile,
2232
- parseCsvFileStream,
2233
- jsonToCsvStream,
2234
- jsonToNdjsonStream,
2235
- csvToJsonStream,
2236
- autoDetectDelimiter,
2237
- // Web Workers функции
2238
- createWorkerPool,
2239
- parseCSVWithWorker,
2240
- createWorkerPoolLazy,
2241
- parseCSVWithWorkerLazy,
2242
- // Error classes
2243
- ValidationError,
2244
- SecurityError,
2245
- FileSystemError,
2246
- ParsingError,
2247
- LimitError,
2248
- ConfigurationError,
2249
- ERROR_CODES,
2250
- // Удобные алиасы
2251
- parse: csvToJson,
2252
- unparse: jsonToCsv,
2253
- // Версия
2254
- version: '2.0.0-browser'
2157
+ // JSON to CSV функции
2158
+ jsonToCsv,
2159
+ preprocessData,
2160
+ downloadAsCsv,
2161
+ deepUnwrap,
2162
+ // CSV to JSON функции
2163
+ csvToJson,
2164
+ csvToJsonIterator,
2165
+ parseCsvFile,
2166
+ parseCsvFileStream,
2167
+ jsonToCsvStream,
2168
+ jsonToNdjsonStream,
2169
+ csvToJsonStream,
2170
+ autoDetectDelimiter,
2171
+ // Web Workers функции
2172
+ createWorkerPool,
2173
+ parseCSVWithWorker,
2174
+ createWorkerPoolLazy,
2175
+ parseCSVWithWorkerLazy,
2176
+ // Асинхронные функции
2177
+ jsonToCsvAsync,
2178
+ csvToJsonAsync,
2179
+ parseCsvFileAsync,
2180
+ autoDetectDelimiterAsync,
2181
+ downloadAsCsvAsync,
2182
+ // Error classes
2183
+ ValidationError,
2184
+ SecurityError,
2185
+ FileSystemError,
2186
+ ParsingError,
2187
+ LimitError,
2188
+ ConfigurationError,
2189
+ ERROR_CODES,
2190
+ // Удобные алиасы
2191
+ parse: csvToJson,
2192
+ unparse: jsonToCsv,
2193
+ parseAsync: csvToJsonAsync,
2194
+ unparseAsync: jsonToCsvAsync,
2195
+ // Версия
2196
+ version: '2.0.0-browser'
2255
2197
  };
2256
-
2257
2198
  // Экспорт для разных сред
2258
2199
  if (typeof module !== 'undefined' && module.exports) {
2259
- // Node.js CommonJS
2260
- module.exports = jtcsv;
2261
- } else if (typeof define === 'function' && define.amd) {
2262
- // AMD
2263
- define([], () => jtcsv);
2264
- } else if (typeof window !== 'undefined') {
2265
- // Браузер (глобальная переменная)
2266
- window.jtcsv = jtcsv;
2200
+ // Node.js CommonJS
2201
+ module.exports = jtcsv;
2202
+ }
2203
+ else if (typeof define === 'function' && define.amd) {
2204
+ // AMD
2205
+ define([], () => jtcsv);
2206
+ }
2207
+ else if (typeof window !== 'undefined') {
2208
+ // Браузер (глобальная переменная)
2209
+ window.jtcsv = jtcsv;
2267
2210
  }
2268
2211
 
2269
2212
  exports.ConfigurationError = ConfigurationError;
@@ -2274,20 +2217,25 @@ exports.ParsingError = ParsingError;
2274
2217
  exports.SecurityError = SecurityError;
2275
2218
  exports.ValidationError = ValidationError;
2276
2219
  exports.autoDetectDelimiter = autoDetectDelimiter;
2220
+ exports.autoDetectDelimiterAsync = autoDetectDelimiterAsync;
2277
2221
  exports.createWorkerPool = createWorkerPool;
2278
2222
  exports.createWorkerPoolLazy = createWorkerPoolLazy;
2279
2223
  exports.csvToJson = csvToJson;
2224
+ exports.csvToJsonAsync = csvToJsonAsync;
2280
2225
  exports.csvToJsonIterator = csvToJsonIterator;
2281
2226
  exports.csvToJsonStream = csvToJsonStream;
2282
2227
  exports.deepUnwrap = deepUnwrap;
2283
2228
  exports.default = jtcsv;
2284
2229
  exports.downloadAsCsv = downloadAsCsv;
2230
+ exports.downloadAsCsvAsync = downloadAsCsvAsync;
2285
2231
  exports.jsonToCsv = jsonToCsv;
2232
+ exports.jsonToCsvAsync = jsonToCsvAsync;
2286
2233
  exports.jsonToCsvStream = jsonToCsvStream;
2287
2234
  exports.jsonToNdjsonStream = jsonToNdjsonStream;
2288
2235
  exports.parseCSVWithWorker = parseCSVWithWorker;
2289
2236
  exports.parseCSVWithWorkerLazy = parseCSVWithWorkerLazy;
2290
2237
  exports.parseCsvFile = parseCsvFile;
2238
+ exports.parseCsvFileAsync = parseCsvFileAsync;
2291
2239
  exports.parseCsvFileStream = parseCsvFileStream;
2292
2240
  exports.preprocessData = preprocessData;
2293
2241
  //# sourceMappingURL=jtcsv.cjs.js.map