jtcsv 1.2.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +272 -329
- package/bin/jtcsv.js +1092 -97
- package/cli-tui.js +0 -0
- package/csv-to-json.js +385 -311
- package/dist/jtcsv.cjs.js +1619 -0
- package/dist/jtcsv.cjs.js.map +1 -0
- package/dist/jtcsv.esm.js +1599 -0
- package/dist/jtcsv.esm.js.map +1 -0
- package/dist/jtcsv.umd.js +1625 -0
- package/dist/jtcsv.umd.js.map +1 -0
- package/examples/cli-tool.js +186 -0
- package/examples/express-api.js +167 -0
- package/examples/large-dataset-example.js +185 -0
- package/examples/plugin-excel-exporter.js +407 -0
- package/examples/simple-usage.js +280 -0
- package/examples/streaming-example.js +419 -0
- package/index.d.ts +288 -1
- package/index.js +23 -0
- package/json-save.js +1 -1
- package/json-to-csv.js +130 -89
- package/package.json +139 -13
- package/plugins/README.md +373 -0
- package/plugins/express-middleware/README.md +306 -0
- package/plugins/express-middleware/example.js +136 -0
- package/plugins/express-middleware/index.d.ts +114 -0
- package/plugins/express-middleware/index.js +360 -0
- package/plugins/express-middleware/package.json +52 -0
- package/plugins/fastify-plugin/index.js +406 -0
- package/plugins/fastify-plugin/package.json +55 -0
- package/plugins/nextjs-api/README.md +452 -0
- package/plugins/nextjs-api/examples/ConverterComponent.jsx +386 -0
- package/plugins/nextjs-api/examples/api-convert.js +69 -0
- package/plugins/nextjs-api/index.js +388 -0
- package/plugins/nextjs-api/package.json +63 -0
- package/plugins/nextjs-api/route.js +372 -0
- package/src/browser/browser-functions.js +189 -0
- package/src/browser/csv-to-json-browser.js +442 -0
- package/src/browser/errors-browser.js +194 -0
- package/src/browser/index.js +79 -0
- package/src/browser/json-to-csv-browser.js +309 -0
- package/src/browser/workers/csv-parser.worker.js +359 -0
- package/src/browser/workers/worker-pool.js +467 -0
- package/src/core/delimiter-cache.js +186 -0
- package/src/core/plugin-system.js +472 -0
- package/src/core/transform-hooks.js +350 -0
- package/src/engines/fast-path-engine-new.js +338 -0
- package/src/engines/fast-path-engine.js +836 -0
- package/src/formats/ndjson-parser.js +419 -0
- package/src/formats/tsv-parser.js +336 -0
- package/src/index-with-plugins.js +371 -0
- package/stream-csv-to-json.js +1 -1
- package/stream-json-to-csv.js +1 -1
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Пример React компонента для конвертации CSV/JSON
|
|
3
|
+
* Использование в Next.js приложении
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import React, { useState } from 'react';
|
|
7
|
+
import { useJtcsv, CsvFileUploader, downloadCsv } from '../index';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Компонент для конвертации CSV ↔ JSON
|
|
11
|
+
*/
|
|
12
|
+
export default function ConverterComponent() {
|
|
13
|
+
const [input, setInput] = useState('');
|
|
14
|
+
const [output, setOutput] = useState('');
|
|
15
|
+
const [format, setFormat] = useState('csv'); // 'csv' или 'json'
|
|
16
|
+
const [delimiter, setDelimiter] = useState(',');
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
convertCsvToJson,
|
|
20
|
+
convertJsonToCsv,
|
|
21
|
+
isLoading,
|
|
22
|
+
error,
|
|
23
|
+
stats
|
|
24
|
+
} = useJtcsv({
|
|
25
|
+
delimiter,
|
|
26
|
+
parseNumbers: true,
|
|
27
|
+
parseBooleans: true,
|
|
28
|
+
preventCsvInjection: true
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const handleConvert = async () => {
|
|
32
|
+
if (!input.trim()) return;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
if (format === 'csv') {
|
|
36
|
+
// Конвертируем CSV в JSON
|
|
37
|
+
const result = await convertCsvToJson(input);
|
|
38
|
+
setOutput(JSON.stringify(result, null, 2));
|
|
39
|
+
} else {
|
|
40
|
+
// Конвертируем JSON в CSV
|
|
41
|
+
const json = JSON.parse(input);
|
|
42
|
+
const result = await convertJsonToCsv(json);
|
|
43
|
+
setOutput(result);
|
|
44
|
+
}
|
|
45
|
+
} catch (err) {
|
|
46
|
+
setOutput(`Error: ${err.message}`);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const handleFileUpload = (result, fileStats) => {
|
|
51
|
+
setInput(JSON.stringify(result, null, 2));
|
|
52
|
+
setFormat('json');
|
|
53
|
+
|
|
54
|
+
console.log('File converted:', fileStats);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const handleDownload = async () => {
|
|
58
|
+
if (!output) return;
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
if (format === 'csv') {
|
|
62
|
+
// Скачиваем как CSV
|
|
63
|
+
const json = JSON.parse(input);
|
|
64
|
+
await downloadCsv(json, 'converted.csv', { delimiter });
|
|
65
|
+
} else {
|
|
66
|
+
// Скачиваем как JSON
|
|
67
|
+
const blob = new Blob([output], { type: 'application/json' });
|
|
68
|
+
const url = URL.createObjectURL(blob);
|
|
69
|
+
const link = document.createElement('a');
|
|
70
|
+
link.href = url;
|
|
71
|
+
link.download = 'converted.json';
|
|
72
|
+
link.click();
|
|
73
|
+
URL.revokeObjectURL(url);
|
|
74
|
+
}
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.error('Download error:', err);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const handleExample = () => {
|
|
81
|
+
if (format === 'csv') {
|
|
82
|
+
setInput('name,email,age\nJohn Doe,john@example.com,30\nJane Smith,jane@example.com,25');
|
|
83
|
+
} else {
|
|
84
|
+
setInput(JSON.stringify([
|
|
85
|
+
{ name: 'John Doe', email: 'john@example.com', age: 30 },
|
|
86
|
+
{ name: 'Jane Smith', email: 'jane@example.com', age: 25 }
|
|
87
|
+
], null, 2));
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const handleClear = () => {
|
|
92
|
+
setInput('');
|
|
93
|
+
setOutput('');
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<div style={styles.container}>
|
|
98
|
+
<h1 style={styles.title}>🔄 JTCSV Converter</h1>
|
|
99
|
+
|
|
100
|
+
<div style={styles.controls}>
|
|
101
|
+
<div style={styles.formatSelector}>
|
|
102
|
+
<label>
|
|
103
|
+
<input
|
|
104
|
+
type="radio"
|
|
105
|
+
value="csv"
|
|
106
|
+
checked={format === 'csv'}
|
|
107
|
+
onChange={(e) => setFormat(e.target.value)}
|
|
108
|
+
/>
|
|
109
|
+
CSV → JSON
|
|
110
|
+
</label>
|
|
111
|
+
<label>
|
|
112
|
+
<input
|
|
113
|
+
type="radio"
|
|
114
|
+
value="json"
|
|
115
|
+
checked={format === 'json'}
|
|
116
|
+
onChange={(e) => setFormat(e.target.value)}
|
|
117
|
+
/>
|
|
118
|
+
JSON → CSV
|
|
119
|
+
</label>
|
|
120
|
+
</div>
|
|
121
|
+
|
|
122
|
+
<div style={styles.delimiterSelector}>
|
|
123
|
+
<label>
|
|
124
|
+
Разделитель:
|
|
125
|
+
<select
|
|
126
|
+
value={delimiter}
|
|
127
|
+
onChange={(e) => setDelimiter(e.target.value)}
|
|
128
|
+
style={styles.select}
|
|
129
|
+
>
|
|
130
|
+
<option value=",">Запятая (,)</option>
|
|
131
|
+
<option value=";">Точка с запятой (;)</option>
|
|
132
|
+
<option value="\t">Табуляция (\t)</option>
|
|
133
|
+
<option value="|">Вертикальная черта (|)</option>
|
|
134
|
+
</select>
|
|
135
|
+
</label>
|
|
136
|
+
</div>
|
|
137
|
+
</div>
|
|
138
|
+
|
|
139
|
+
<div style={styles.inputSection}>
|
|
140
|
+
<div style={styles.inputHeader}>
|
|
141
|
+
<h3 style={styles.sectionTitle}>
|
|
142
|
+
{format === 'csv' ? 'CSV Input' : 'JSON Input'}
|
|
143
|
+
</h3>
|
|
144
|
+
<div style={styles.inputActions}>
|
|
145
|
+
<CsvFileUploader
|
|
146
|
+
onConvert={handleFileUpload}
|
|
147
|
+
options={{ delimiter }}
|
|
148
|
+
>
|
|
149
|
+
<button style={styles.buttonSecondary}>📁 Upload CSV</button>
|
|
150
|
+
</CsvFileUploader>
|
|
151
|
+
<button
|
|
152
|
+
onClick={handleExample}
|
|
153
|
+
style={styles.buttonSecondary}
|
|
154
|
+
>
|
|
155
|
+
📋 Example
|
|
156
|
+
</button>
|
|
157
|
+
<button
|
|
158
|
+
onClick={handleClear}
|
|
159
|
+
style={styles.buttonSecondary}
|
|
160
|
+
>
|
|
161
|
+
🗑️ Clear
|
|
162
|
+
</button>
|
|
163
|
+
</div>
|
|
164
|
+
</div>
|
|
165
|
+
|
|
166
|
+
<textarea
|
|
167
|
+
value={input}
|
|
168
|
+
onChange={(e) => setInput(e.target.value)}
|
|
169
|
+
placeholder={format === 'csv'
|
|
170
|
+
? 'Введите CSV данные...\nПример:\nname,email,age\nJohn,john@example.com,30'
|
|
171
|
+
: 'Введите JSON данные...\nПример:\n[{"name":"John","age":30}]'
|
|
172
|
+
}
|
|
173
|
+
style={styles.textarea}
|
|
174
|
+
rows={10}
|
|
175
|
+
/>
|
|
176
|
+
</div>
|
|
177
|
+
|
|
178
|
+
<div style={styles.convertButtonContainer}>
|
|
179
|
+
<button
|
|
180
|
+
onClick={handleConvert}
|
|
181
|
+
disabled={isLoading || !input.trim()}
|
|
182
|
+
style={{
|
|
183
|
+
...styles.buttonPrimary,
|
|
184
|
+
opacity: isLoading || !input.trim() ? 0.6 : 1,
|
|
185
|
+
cursor: isLoading || !input.trim() ? 'not-allowed' : 'pointer'
|
|
186
|
+
}}
|
|
187
|
+
>
|
|
188
|
+
{isLoading ? '🔄 Converting...' : '🚀 Convert'}
|
|
189
|
+
</button>
|
|
190
|
+
|
|
191
|
+
{stats && (
|
|
192
|
+
<div style={styles.stats}>
|
|
193
|
+
<span>⏱️ {stats.processingTime}ms</span>
|
|
194
|
+
<span>📊 {stats.rows || stats.size} {stats.rows ? 'rows' : 'chars'}</span>
|
|
195
|
+
</div>
|
|
196
|
+
)}
|
|
197
|
+
</div>
|
|
198
|
+
|
|
199
|
+
{error && (
|
|
200
|
+
<div style={styles.error}>
|
|
201
|
+
❌ Error: {error}
|
|
202
|
+
</div>
|
|
203
|
+
)}
|
|
204
|
+
|
|
205
|
+
<div style={styles.outputSection}>
|
|
206
|
+
<div style={styles.outputHeader}>
|
|
207
|
+
<h3 style={styles.sectionTitle}>
|
|
208
|
+
{format === 'csv' ? 'JSON Output' : 'CSV Output'}
|
|
209
|
+
</h3>
|
|
210
|
+
<div style={styles.outputActions}>
|
|
211
|
+
<button
|
|
212
|
+
onClick={handleDownload}
|
|
213
|
+
disabled={!output}
|
|
214
|
+
style={styles.buttonSecondary}
|
|
215
|
+
>
|
|
216
|
+
⬇️ Download
|
|
217
|
+
</button>
|
|
218
|
+
<button
|
|
219
|
+
onClick={() => navigator.clipboard.writeText(output)}
|
|
220
|
+
disabled={!output}
|
|
221
|
+
style={styles.buttonSecondary}
|
|
222
|
+
>
|
|
223
|
+
📋 Copy
|
|
224
|
+
</button>
|
|
225
|
+
</div>
|
|
226
|
+
</div>
|
|
227
|
+
|
|
228
|
+
<pre style={styles.output}>
|
|
229
|
+
{output || 'Результат появится здесь...'}
|
|
230
|
+
</pre>
|
|
231
|
+
</div>
|
|
232
|
+
|
|
233
|
+
<div style={styles.info}>
|
|
234
|
+
<p>💡 <strong>Подсказки:</strong></p>
|
|
235
|
+
<ul style={styles.tipsList}>
|
|
236
|
+
<li>Используйте кнопку "Example" для быстрого заполнения</li>
|
|
237
|
+
<li>Загружайте CSV файлы через кнопку "Upload CSV"</li>
|
|
238
|
+
<li>Выберите разделитель соответствующий вашим данным</li>
|
|
239
|
+
<li>Скачивайте результат в нужном формате</li>
|
|
240
|
+
</ul>
|
|
241
|
+
</div>
|
|
242
|
+
</div>
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const styles = {
|
|
247
|
+
container: {
|
|
248
|
+
maxWidth: '1200px',
|
|
249
|
+
margin: '0 auto',
|
|
250
|
+
padding: '20px',
|
|
251
|
+
fontFamily: 'system-ui, -apple-system, sans-serif'
|
|
252
|
+
},
|
|
253
|
+
title: {
|
|
254
|
+
color: '#333',
|
|
255
|
+
textAlign: 'center',
|
|
256
|
+
marginBottom: '30px'
|
|
257
|
+
},
|
|
258
|
+
controls: {
|
|
259
|
+
display: 'flex',
|
|
260
|
+
justifyContent: 'space-between',
|
|
261
|
+
alignItems: 'center',
|
|
262
|
+
marginBottom: '20px',
|
|
263
|
+
flexWrap: 'wrap',
|
|
264
|
+
gap: '20px'
|
|
265
|
+
},
|
|
266
|
+
formatSelector: {
|
|
267
|
+
display: 'flex',
|
|
268
|
+
gap: '20px'
|
|
269
|
+
},
|
|
270
|
+
delimiterSelector: {
|
|
271
|
+
display: 'flex',
|
|
272
|
+
alignItems: 'center',
|
|
273
|
+
gap: '10px'
|
|
274
|
+
},
|
|
275
|
+
select: {
|
|
276
|
+
padding: '5px 10px',
|
|
277
|
+
marginLeft: '10px',
|
|
278
|
+
borderRadius: '4px',
|
|
279
|
+
border: '1px solid #ccc'
|
|
280
|
+
},
|
|
281
|
+
inputSection: {
|
|
282
|
+
marginBottom: '20px'
|
|
283
|
+
},
|
|
284
|
+
inputHeader: {
|
|
285
|
+
display: 'flex',
|
|
286
|
+
justifyContent: 'space-between',
|
|
287
|
+
alignItems: 'center',
|
|
288
|
+
marginBottom: '10px'
|
|
289
|
+
},
|
|
290
|
+
inputActions: {
|
|
291
|
+
display: 'flex',
|
|
292
|
+
gap: '10px'
|
|
293
|
+
},
|
|
294
|
+
sectionTitle: {
|
|
295
|
+
margin: '0',
|
|
296
|
+
color: '#555'
|
|
297
|
+
},
|
|
298
|
+
textarea: {
|
|
299
|
+
width: '100%',
|
|
300
|
+
padding: '15px',
|
|
301
|
+
border: '1px solid #ddd',
|
|
302
|
+
borderRadius: '8px',
|
|
303
|
+
fontFamily: 'monospace',
|
|
304
|
+
fontSize: '14px',
|
|
305
|
+
resize: 'vertical',
|
|
306
|
+
boxSizing: 'border-box'
|
|
307
|
+
},
|
|
308
|
+
convertButtonContainer: {
|
|
309
|
+
display: 'flex',
|
|
310
|
+
flexDirection: 'column',
|
|
311
|
+
alignItems: 'center',
|
|
312
|
+
gap: '10px',
|
|
313
|
+
margin: '20px 0'
|
|
314
|
+
},
|
|
315
|
+
buttonPrimary: {
|
|
316
|
+
padding: '12px 30px',
|
|
317
|
+
background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
|
|
318
|
+
color: 'white',
|
|
319
|
+
border: 'none',
|
|
320
|
+
borderRadius: '25px',
|
|
321
|
+
fontSize: '16px',
|
|
322
|
+
fontWeight: 'bold',
|
|
323
|
+
cursor: 'pointer',
|
|
324
|
+
transition: 'transform 0.2s'
|
|
325
|
+
},
|
|
326
|
+
buttonSecondary: {
|
|
327
|
+
padding: '8px 16px',
|
|
328
|
+
background: '#f0f0f0',
|
|
329
|
+
color: '#333',
|
|
330
|
+
border: '1px solid #ddd',
|
|
331
|
+
borderRadius: '4px',
|
|
332
|
+
cursor: 'pointer',
|
|
333
|
+
transition: 'background 0.2s'
|
|
334
|
+
},
|
|
335
|
+
stats: {
|
|
336
|
+
display: 'flex',
|
|
337
|
+
gap: '20px',
|
|
338
|
+
color: '#666',
|
|
339
|
+
fontSize: '14px'
|
|
340
|
+
},
|
|
341
|
+
error: {
|
|
342
|
+
padding: '15px',
|
|
343
|
+
background: '#fee',
|
|
344
|
+
border: '1px solid #f99',
|
|
345
|
+
borderRadius: '8px',
|
|
346
|
+
color: '#c00',
|
|
347
|
+
marginBottom: '20px'
|
|
348
|
+
},
|
|
349
|
+
outputSection: {
|
|
350
|
+
marginTop: '20px'
|
|
351
|
+
},
|
|
352
|
+
outputHeader: {
|
|
353
|
+
display: 'flex',
|
|
354
|
+
justifyContent: 'space-between',
|
|
355
|
+
alignItems: 'center',
|
|
356
|
+
marginBottom: '10px'
|
|
357
|
+
},
|
|
358
|
+
outputActions: {
|
|
359
|
+
display: 'flex',
|
|
360
|
+
gap: '10px'
|
|
361
|
+
},
|
|
362
|
+
output: {
|
|
363
|
+
padding: '15px',
|
|
364
|
+
background: '#f8f8f8',
|
|
365
|
+
border: '1px solid #ddd',
|
|
366
|
+
borderRadius: '8px',
|
|
367
|
+
minHeight: '200px',
|
|
368
|
+
overflow: 'auto',
|
|
369
|
+
whiteSpace: 'pre-wrap',
|
|
370
|
+
wordBreak: 'break-all',
|
|
371
|
+
fontFamily: 'monospace',
|
|
372
|
+
fontSize: '14px'
|
|
373
|
+
},
|
|
374
|
+
info: {
|
|
375
|
+
marginTop: '30px',
|
|
376
|
+
padding: '20px',
|
|
377
|
+
background: '#f0f8ff',
|
|
378
|
+
border: '1px solid #cce5ff',
|
|
379
|
+
borderRadius: '8px'
|
|
380
|
+
},
|
|
381
|
+
tipsList: {
|
|
382
|
+
margin: '10px 0 0 20px',
|
|
383
|
+
color: '#555'
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Пример API route для Next.js
|
|
3
|
+
* Сохраните этот файл как pages/api/convert.js
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* // Запросы к API:
|
|
7
|
+
* // POST /api/convert с JSON телом → получите CSV
|
|
8
|
+
* // POST /api/convert с CSV телом → получите JSON
|
|
9
|
+
* // GET /api/convert/health → проверка состояния
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
handler as jtcsvHandler,
|
|
14
|
+
csvToJsonHandler,
|
|
15
|
+
jsonToCsvHandler,
|
|
16
|
+
healthCheckHandler
|
|
17
|
+
} from '../../route';
|
|
18
|
+
|
|
19
|
+
// Основной endpoint для автоматической конвертации
|
|
20
|
+
export default jtcsvHandler;
|
|
21
|
+
|
|
22
|
+
// Специализированные endpoints
|
|
23
|
+
export const config = {
|
|
24
|
+
api: {
|
|
25
|
+
bodyParser: {
|
|
26
|
+
sizeLimit: '50mb'
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Альтернативная реализация с отдельными путями
|
|
32
|
+
// export default async function handler(req, res) {
|
|
33
|
+
// const { path } = req.query;
|
|
34
|
+
//
|
|
35
|
+
// switch (path) {
|
|
36
|
+
// case 'csv-to-json':
|
|
37
|
+
// return csvToJsonHandler(req, res);
|
|
38
|
+
// case 'json-to-csv':
|
|
39
|
+
// return jsonToCsvHandler(req, res);
|
|
40
|
+
// case 'health':
|
|
41
|
+
// return healthCheckHandler(req, res);
|
|
42
|
+
// default:
|
|
43
|
+
// return jtcsvHandler(req, res);
|
|
44
|
+
// }
|
|
45
|
+
// }
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Пример использования с кастомной логикой
|
|
49
|
+
*
|
|
50
|
+
* export default async function handler(req, res) {
|
|
51
|
+
* // Добавляем логирование
|
|
52
|
+
* console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
|
|
53
|
+
*
|
|
54
|
+
* // Проверяем аутентификацию
|
|
55
|
+
* const apiKey = req.headers['x-api-key'];
|
|
56
|
+
* if (!apiKey || apiKey !== process.env.API_KEY) {
|
|
57
|
+
* return res.status(401).json({ error: 'Unauthorized' });
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* // Ограничение по частоте запросов
|
|
61
|
+
* const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
|
62
|
+
* // ... rate limiting logic
|
|
63
|
+
*
|
|
64
|
+
* // Вызываем основной обработчик
|
|
65
|
+
* return jtcsvHandler(req, res);
|
|
66
|
+
* }
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
|