You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -108,7 +112,6 @@ This package is compatible with .
110
114
111
-
### Install
112
115
Install package in your *package.json*
113
116
```bash
114
117
$ npm install convert-csv-to-json --save
@@ -357,10 +360,138 @@ let jsonArray = csvToJson
357
360
.csvStringToJson(csvString);
358
361
```
359
362
363
+
### Sync API (TypeScript)
364
+
365
+
TypeScript typings are available via the included `index.d.ts`. You can importthedefaultconverterorusenamedimports. BelowarecommonpatternswhenusingthesynchronousAPIfrom TypeScript.
366
+
367
+
```ts
368
+
// Named import (recommended when using ES modules)
369
+
import converter, { /* or */ } from 'convert-csv-to-json';
370
+
// Access the default converter
371
+
const csvToJson = require('convert-csv-to-json');
372
+
373
+
// Define a type for your CSV records
374
+
interface Person {
375
+
name: string;
376
+
age: number;
377
+
}
378
+
379
+
// Parse CSV string synchronously and assert the returned type
380
+
const csv = 'name,age\nAlice,30';
381
+
const parsed = csvToJson.csvStringToJson(csv) as Person[];
382
+
383
+
// Chain configuration and call sync methods
384
+
const result = csvToJson
385
+
.fieldDelimiter(',')
386
+
.formatValueByType()
387
+
.csvStringToJson('name,age\nBob,25') as Person[];
388
+
```
389
+
390
+
## Browser API Usage
391
+
392
+
The package exposes a `browser` helper that reuses the library's parsing logic but provides browser-friendly helpers for parsing CSV strings and `File`/`Blob` objects. The API mirrors the synchronous and asynchronous Node APIs and supports method chaining for configuration.
`parseFile` accepts an optional `options` object with `encoding` (passed to `FileReader.readAsText`). If `FileReader` is not available, `parseFile` will reject.
428
+
429
+
### Notes
430
+
431
+
- The `browser` API proxies the same configuration methods as the Node API and follows the same behavior for quoted fields, sub-array parsing, trimming, and value formatting.
432
+
- `parseFile` depends on the browser `FileReader` API; calling it in Node.js will reject with an informative error.
433
+
434
+
### Browser API (TypeScript)
435
+
436
+
TypeScript typings are provided via the included `index.d.ts`. You can import the default converter and access the `browser` helper, or import `browser` directly. Below are common usage patterns.
437
+
438
+
```ts
439
+
// Named import (recommended for direct use)
440
+
import { browser } from 'convert-csv-to-json';
441
+
442
+
// Or default import and access the browser helper
443
+
import converter from 'convert-csv-to-json';
444
+
const browserApi = converter.browser;
445
+
446
+
// Define a type for your CSV records
447
+
interface Person {
448
+
name: string;
449
+
age: number;
450
+
}
451
+
452
+
// Synchronous parse (assert the returned type)
453
+
const csv = 'name,age\nAlice,30';
454
+
const parsed = browser.csvStringToJson(csv) as Person[];
455
+
456
+
// Async parse
457
+
const parsedAsync = await browser.csvStringToJsonAsync(csv) as Person[];
458
+
459
+
// Parse a File in the browser
460
+
const inputEl = document.querySelector('input[type=file]') as HTMLInputElement;
461
+
const file = inputEl.files![0];
462
+
const data = await browser.parseFile(file) as Person[];
463
+
```
464
+
465
+
The `BrowserApi` interface in `index.d.ts` exposes typed method signatures for IDE autocompletion and compile-time checks.
466
+
360
467
## Async API Usage
361
468
362
469
This library provides a Promise-based async API that's perfect for modern Node.jsapplications. For a detailed migration guide from sync to asyncAPI, see [MIGRATION.md](MIGRATION.md).
363
470
471
+
### Async API (TypeScript)
472
+
473
+
The asyncAPI also has TypeScript typings. Typical usage in TypeScript looks like this:
0 commit comments