├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── index.d.ts ├── index.js ├── readme.md ├── package.json ├── license └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {Buffer} from 'node:buffer'; 2 | import fs from 'node:fs'; 3 | import {expectType} from 'tsd'; 4 | import neatCsv, {Options, Row} from './index.js'; 5 | 6 | const options: Options = {}; // eslint-disable-line @typescript-eslint/no-unused-vars 7 | const csvText = 'type,part\nunicorn,horn\nrainbow,pink'; 8 | 9 | expectType>(neatCsv(csvText)); 10 | expectType>(neatCsv(Buffer.from(csvText))); 11 | expectType>(neatCsv(fs.createReadStream('test.csv'))); 12 | expectType>(neatCsv(csvText, {separator: ','})); 13 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import {Buffer} from 'node:buffer'; 2 | import {Readable as ReadableStream} from 'node:stream'; 3 | import {Options} from 'csv-parser'; 4 | 5 | export type Row = Record; 6 | 7 | /** 8 | Fast CSV parser. 9 | 10 | Convenience wrapper around the super-fast streaming [`csv-parser`](https://github.com/mafintosh/csv-parser) module. Use that one if you want streamed parsing. 11 | 12 | @param data - The CSV data to parse. 13 | @param options - See the [`csv-parser` options](https://github.com/mafintosh/csv-parser#options). 14 | 15 | @example 16 | ``` 17 | import neatCsv from 'neat-csv'; 18 | 19 | const csv = 'type,part\nunicorn,horn\nrainbow,pink'; 20 | 21 | console.log(await neatCsv(csv)); 22 | //=> [{type: 'unicorn', part: 'horn'}, {type: 'rainbow', part: 'pink'}] 23 | ``` 24 | */ 25 | export default function neatCsv( 26 | data: string | Buffer | ReadableStream, 27 | options?: Options 28 | ): Promise; 29 | 30 | export {Options} from 'csv-parser'; 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {promisify} from 'node:util'; 2 | import {Readable as ReadableStream, pipeline} from 'node:stream'; 3 | import process from 'node:process'; 4 | import {Buffer} from 'node:buffer'; 5 | import csvParser from 'csv-parser'; 6 | import getStream from 'get-stream'; 7 | // TODO: Use `import {pipeline as pipelinePromise} from 'node:stream/promises';` when targeting Node.js 16. 8 | 9 | const pipelinePromise = promisify(pipeline); 10 | 11 | export default async function neatCsv(data, options) { 12 | if (typeof data === 'string' || Buffer.isBuffer(data)) { 13 | data = ReadableStream.from(data); 14 | } 15 | 16 | const parserStream = csvParser(options); 17 | 18 | // Node.js 16 has a bug with `.pipeline` for large strings. It works fine in Node.js 14 and 12. 19 | if (Number(process.versions.node.split('.')[0]) >= 16) { 20 | return getStream.array(data.pipe(parserStream)); 21 | } 22 | 23 | await pipelinePromise([data, parserStream]); 24 | return getStream.array(parserStream); 25 | } 26 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # neat-csv 2 | 3 | > Fast CSV parser 4 | 5 | Convenience wrapper around the super-fast streaming [`csv-parser`](https://github.com/mafintosh/csv-parser) module. Use that one if you want streamed parsing. 6 | 7 | Parsing-related issues should be reported to [`csv-parser`](https://github.com/mafintosh/csv-parser/issues). 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install neat-csv 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import neatCsv from 'neat-csv'; 19 | 20 | const csv = 'type,part\nunicorn,horn\nrainbow,pink'; 21 | 22 | console.log(await neatCsv(csv)); 23 | //=> [{type: 'unicorn', part: 'horn'}, {type: 'rainbow', part: 'pink'}] 24 | ``` 25 | 26 | ## API 27 | 28 | ### neatCsv(data, options?) 29 | 30 | Returns a `Promise` with the parsed CSV. 31 | 32 | #### data 33 | 34 | Type: `string | Buffer | stream.Readable` 35 | 36 | The CSV data to parse. 37 | 38 | #### options 39 | 40 | Type: `object` 41 | 42 | See the [`csv-parser` options](https://github.com/mafintosh/csv-parser#options). 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "neat-csv", 3 | "version": "7.0.0", 4 | "description": "Fast CSV parser", 5 | "license": "MIT", 6 | "repository": "sindresorhus/neat-csv", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "parse", 27 | "csv", 28 | "comma", 29 | "separated", 30 | "values", 31 | "tab", 32 | "delimiter", 33 | "separator", 34 | "text", 35 | "string", 36 | "buffer", 37 | "stream", 38 | "parser" 39 | ], 40 | "dependencies": { 41 | "csv-parser": "^3.0.0", 42 | "get-stream": "^6.0.1" 43 | }, 44 | "devDependencies": { 45 | "ava": "^3.15.0", 46 | "tsd": "^0.18.0", 47 | "xo": "^0.45.0" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import {Buffer} from 'node:buffer'; 2 | import {Readable as ReadableStream} from 'node:stream'; 3 | import test from 'ava'; 4 | import neatCsv from './index.js'; 5 | 6 | test('buffer', async t => { 7 | const data = await neatCsv(Buffer.from('name,val\nfoo,1\nbar,2')); 8 | t.is(data[0].name, 'foo'); 9 | t.is(data[1].name, 'bar'); 10 | }); 11 | 12 | test('string', async t => { 13 | const data = await neatCsv('name;val\nfoo;1\nbar;2', {separator: ';'}); 14 | t.is(data[0].name, 'foo'); 15 | t.is(data[1].name, 'bar'); 16 | }); 17 | 18 | test('stream', async t => { 19 | const data = await neatCsv(ReadableStream.from('name,val\nfoo,1\nbar,2')); 20 | t.is(data[0].name, 'foo'); 21 | t.is(data[1].name, 'bar'); 22 | }); 23 | 24 | test('error', async t => { 25 | await t.throwsAsync(neatCsv('name,val\nfoo,1,3\nbar,2', {strict: true}), { 26 | message: /Row length does not match headers/, 27 | }); 28 | }); 29 | 30 | const largeStringFixture = `provider_name,provider_id,url,mds_api_url,gbfs_api_url 31 | JUMP,c20e08cf-8488-46a6-a66c-5d8fb827f7e0,https://jump.com,https://api.uber.com/v0.2/emobility/mds, 32 | Lime,63f13c48-34ff-49d2-aca7-cf6a5b6171c3,https://li.me,https://data.lime.bike/api/partners/v1/mds, 33 | Bird,2411d395-04f2-47c9-ab66-d09e9e3c3251,https://www.bird.co,https://mds.bird.co,https://mds.bird.co/gbfs 34 | Razor,6ddcc0ad-1d66-4046-bba4-d1d96bb8ca4d,https://www.razor.com/share,https://razor-200806.appspot.com/api/v2/mds, 35 | Lyft,e714f168-ce56-4b41-81b7-0b6a4bd26128,https://www.lyft.com,https://api.lyft.com/v1/last-mile/mds, 36 | Skip,d73fcf80-22b1-450f-b535-042b4e30aac7,https://www.skipscooters.com,https://api.skipscooters.com/mds, 37 | HOPR,2e4cb206-b475-4a9d-80fb-0880c9a033e0,https://gohopr.com,https://gbfs.hopr.city/api/mds, 38 | Wheels,b79f8687-526d-4ae6-80bf-89b4c44dc071,https://wheels.co,https://mds.getwheelsapp.com, 39 | Spin,70aa475d-1fcd-4504-b69c-2eeb2107f7be,https://www.spin.app,https://web.spin.pm/api/mds/v1, 40 | WIND,d56d2df6-fa92-43de-ab61-92c3a84acd7d,https://www.wind.co,https://partners.wind.co/v1/mds, 41 | Tier,264aad41-b47c-415d-8585-0208d436516e,https://www.tier.app,https://partner.tier-services.io/mds, 42 | Cloud,bf95148b-d1d1-464e-a140-6d2563ac43d4,https://www.cloud.tt,https://mds.cloud.tt,https://mds.cloud.tt/gbfs 43 | BlueLA,f3c5a65d-fd85-463e-9564-fc95ea473f7d,https://www.bluela.com,https://api.bluela.com/mds/v0,https://api.bluela.com/gbfs/v1/meta 44 | Bolt,3291c288-c9c8-42f1-bc3e-8502b077cd7f,https://www.micromobility.com/,https://bolt.miami/bolt2/api/mds, 45 | CLEVR,daecbe87-a9f2-4a5a-b5df-8e3e14180513,https://clevrmobility.com,https://portal.clevrmobility.com/api/mds, 46 | SherpaLA,3c95765d-4da6-41c6-b61e-1954472ec6c9,,https://mds.bird.co,https://mds.bird.co/gbfs/platform-partner/sherpa-la 47 | OjO Electric,8d293326-8464-4256-8312-617ebcd0efad,https://www.ojoelectric.com,https://api.ojoelectric.com/api/mds,https://api.ojoelectric.com/api/mds/gbfs.json 48 | VOI,1f129e3f-158f-4df5-af9c-a80de238e334,https://www.voiscooters.com/,https://mds.voiapp.io,https://mds.voiapp.io/v1/gbfs 49 | MOVO,40dad54c-e199-4fd8-9f5b-cff93ffced63,https://movo.me,https://mds.movo.me,https://gbfs.movo.me 50 | Baus,638ba503-4006-4c53-bf34-cd625cc03d61,https://bausmoves.com,https://mds.bird.co,https://mds.bird.co/gbfs/platform-partner/v2/baus 51 | Bolt Technology,7ea695ca-9de7-4b3b-9f3c-241b2045a1fe,https://bolt.eu,https://mds.bolt.eu,https://mds.bolt.eu/gbfs/1 52 | Grin,8a0ecfce-5fb3-451e-8069-434b79c8b01a,https://ongrin.com,https://open-data.grow.mobi/mds/,https://open-data.grow.mobi/api/v1/gbfs/ 53 | Dott,0a899e3a-705a-46f2-9189-d78cc83a2db4,https://ridedott.com,https://mds.api.ridedott.com,https://gbfs.api.ridedott.com 54 | Superpedestrian,420e6e94-55a6-4946-b6b3-4398fe22e912,https://www.superpedestrian.com,https://wrangler-mds-production.herokuapp.com/mds, 55 | Circ,03d5d605-e5c9-45a1-a1dd-144aa8649525,https://www.circ.com,https://mds.bird.co, 56 | GIG,42475742-8618-4fc0-aa8f-3b3948b84b85,https://gigcarshare.com/,https://global.us.prod.svc.ridecell.io/reporting/api/mds, 57 | Zisch,fb62cfe6-757c-4e5b-a264-18b43b3fc40b,https://www.e-zisch.ch,https://mds.bird.co, 58 | Seven Group,7fca7812-c718-48dd-b9f2-d1ae160f2ae4,https://www.group-seven.ch,https://mds.bird.co, 59 | Pony,f190d330-b49e-4590-871b-0bcbec565a8c,https://getapony.com/,https://mds.getapony.com/v1`; 60 | 61 | test('large string', async t => { 62 | const data = await neatCsv(largeStringFixture); 63 | t.is(data[0].provider_name, 'JUMP'); 64 | t.is(data[data.length - 1].provider_name, 'Pony'); 65 | }); 66 | --------------------------------------------------------------------------------