-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 12.20 and move to ESM
- Loading branch information
1 parent
ec9a3ca
commit 17f3a2b
Showing
7 changed files
with
53 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,30 @@ | ||
/// <reference types="node"/> | ||
import {Readable as ReadableStream} from 'stream'; | ||
import {Options as CsvParserOptions} from 'csv-parser'; | ||
import {Buffer} from 'node:buffer'; | ||
import {Readable as ReadableStream} from 'node:stream'; | ||
import {Options} from 'csv-parser'; | ||
|
||
declare namespace neatCsv { | ||
type Options = CsvParserOptions; | ||
|
||
type Row = Record<string, string>; | ||
} | ||
export type Row = Record<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 data - The CSV data to parse. | ||
@param options - See the [`csv-parser` options](https://github.com/mafintosh/csv-parser#options). | ||
@example | ||
``` | ||
import neatCsv = require('neat-csv'); | ||
import neatCsv from 'neat-csv'; | ||
const csv = 'type,part\nunicorn,horn\nrainbow,pink'; | ||
(async () => { | ||
console.log(await neatCsv(csv)); | ||
//=> [{type: 'unicorn', part: 'horn'}, {type: 'rainbow', part: 'pink'}] | ||
})(); | ||
console.log(await neatCsv(csv)); | ||
//=> [{type: 'unicorn', part: 'horn'}, {type: 'rainbow', part: 'pink'}] | ||
``` | ||
*/ | ||
declare function neatCsv<Row = neatCsv.Row>( | ||
export default function neatCsv<RowType = Row>( | ||
data: string | Buffer | ReadableStream, | ||
options?: neatCsv.Options | ||
): Promise<Row[]>; | ||
options?: Options | ||
): Promise<RowType[]>; | ||
|
||
export = neatCsv; | ||
export {Options} from 'csv-parser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
'use strict'; | ||
const {promisify} = require('util'); | ||
const {pipeline} = require('stream'); | ||
const toReadableStream = require('to-readable-stream'); | ||
const csvParser = require('csv-parser'); | ||
const getStream = require('get-stream'); | ||
// TODO: Use `const {pipeline: pipelinePromise} = require('stream/promises');` when targeting Node.js 16. | ||
import {promisify} from 'node:util'; | ||
import {Readable as ReadableStream, pipeline} from 'node:stream'; | ||
import process from 'node:process'; | ||
import {Buffer} from 'node:buffer'; | ||
import csvParser from 'csv-parser'; | ||
import getStream from 'get-stream'; | ||
// TODO: Use `import {pipeline as pipelinePromise} from 'node:stream/promises';` when targeting Node.js 16. | ||
|
||
const pipelinePromise = promisify(pipeline); | ||
|
||
module.exports = async (data, options) => { | ||
export default async function neatCsv(data, options) { | ||
if (typeof data === 'string' || Buffer.isBuffer(data)) { | ||
// TODO: Use https://nodejs.org/api/stream.html#stream_stream_readable_from_iterable_options when targeting Node.js 12. | ||
data = toReadableStream(data); | ||
data = ReadableStream.from(data); | ||
} | ||
|
||
const parserStream = csvParser(options); | ||
|
||
// Node.js 15.5 has a bug with `.pipeline` for large strings. It works fine in Node.js 14 and 12. | ||
if (Number(process.versions.node.split('.')[0]) >= 15) { | ||
// Node.js 16 has a bug with `.pipeline` for large strings. It works fine in Node.js 14 and 12. | ||
if (Number(process.versions.node.split('.')[0]) >= 16) { | ||
return getStream.array(data.pipe(parserStream)); | ||
} | ||
|
||
await pipelinePromise([data, parserStream]); | ||
return getStream.array(parserStream); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import {Buffer} from 'node:buffer'; | ||
import fs from 'node:fs'; | ||
import {expectType} from 'tsd'; | ||
import * as fs from 'fs'; | ||
import toReadableStream = require('to-readable-stream'); | ||
import neatCsv = require('.'); | ||
import neatCsv, {Options, Row} from './index.js'; | ||
|
||
const options: neatCsv.Options = {}; | ||
const options: Options = {}; // eslint-disable-line @typescript-eslint/no-unused-vars | ||
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: ','})); | ||
expectType<Promise<Row[]>>(neatCsv(csvText)); | ||
expectType<Promise<Row[]>>(neatCsv(Buffer.from(csvText))); | ||
expectType<Promise<Row[]>>(neatCsv(fs.createReadStream('test.csv'))); | ||
expectType<Promise<Row[]>>(neatCsv(csvText, {separator: ','})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters