Skip to content

Commit

Permalink
Add TypeScript definition (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Jul 7, 2019
1 parent 4a079ca commit ea80e37
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
38 changes: 38 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/// <reference types="node"/>
import {Readable as ReadableStream} from 'stream';
import {Options as CsvParserOptions} from 'csv-parser';

declare namespace neatCsv {
type Options = CsvParserOptions;

interface Row {
[header: string]: string;
}
}

/**
Fast CSV parser.
Convenience wrapper around the super-fast streaming [`csv-parser`](https://github.com/mafintosh/csv-parser) module. Use that one if you want streamed parsing.
@param data - CSV data to parse.
@param options - See the `csv-parser` [options](https://github.com/mafintosh/csv-parser#options).
@example
```
import neatCsv = require('neat-csv');
const csv = 'type,part\nunicorn,horn\nrainbow,pink';
(async () => {
console.log(await neatCsv(csv));
//=> [{type: 'unicorn', part: 'horn'}, {type: 'rainbow', part: 'pink'}]
})();
```
*/
declare function neatCsv(
data: string | Buffer | ReadableStream,
options?: neatCsv.Options
): Promise<neatCsv.Row[]>;

export = neatCsv;
13 changes: 13 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {expectType} from 'tsd';
import * as fs from 'fs';
import toReadableStream = require('to-readable-stream');
import neatCsv = require('.');

const options: neatCsv.Options = {};
const csvText = 'type,part\nunicorn,horn\nrainbow,pink';

expectType<Promise<neatCsv.Row[]>>(neatCsv(csvText));
expectType<Promise<neatCsv.Row[]>>(neatCsv(Buffer.from(csvText)));
expectType<Promise<neatCsv.Row[]>>(neatCsv(toReadableStream(csvText)));
expectType<Promise<neatCsv.Row[]>>(neatCsv(fs.createReadStream('test.csv')));
expectType<Promise<neatCsv.Row[]>>(neatCsv(csvText, {separator: ','}));
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"parse",
Expand All @@ -34,12 +35,13 @@
"parser"
],
"dependencies": {
"csv-parser": "^2.1.0",
"get-stream": "^4.1.0",
"to-readable-stream": "^1.0.0"
"csv-parser": "^2.3.0",
"get-stream": "^5.1.0",
"to-readable-stream": "^2.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"ava": "^2.1.0",
"tsd": "^0.7.3",
"xo": "^0.24.0"
}
}

0 comments on commit ea80e37

Please sign in to comment.