From 59131a29cbf1ef581c9df48021acf21aaeada0e5 Mon Sep 17 00:00:00 2001 From: Janne Liuhtonen Date: Sat, 3 Nov 2018 17:56:06 +0200 Subject: [PATCH] Fix typing parse fn with NODE_STREAM_INPUT arg (#30251) As stated in the Papaparse documentation (https://github.com/mholt/PapaParse#papa-parse-for-node), calling Papa.parse with NODE_STREAM_INPUT argument returns a Duplex that is both readable and writable. This is for the use case of piping a ReadableStream through Papa CSV parsing and continuing the stream chain. --- types/papaparse/index.d.ts | 3 ++- types/papaparse/papaparse-tests.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/types/papaparse/index.d.ts b/types/papaparse/index.d.ts index 4e52dd66a4..c7246ecb1f 100644 --- a/types/papaparse/index.d.ts +++ b/types/papaparse/index.d.ts @@ -7,6 +7,7 @@ // Alberto Restifo // Behind The Math // 3af +// Janne Liuhtonen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -23,7 +24,7 @@ export function parse(file: File, config?: ParseConfig): ParseResult; export function parse(stream: NodeJS.ReadableStream, config?: ParseConfig): ParseResult; -export function parse(stream: typeof NODE_STREAM_INPUT, config?: ParseConfig): NodeJS.ReadableStream; +export function parse(stream: typeof NODE_STREAM_INPUT, config?: ParseConfig): NodeJS.ReadWriteStream; /** * Unparses javascript data objects and returns a csv string diff --git a/types/papaparse/papaparse-tests.ts b/types/papaparse/papaparse-tests.ts index 5db22091ca..57115dc0d5 100644 --- a/types/papaparse/papaparse-tests.ts +++ b/types/papaparse/papaparse-tests.ts @@ -9,6 +9,7 @@ import { ParseMeta, ParseResult } from "papaparse"; +import { Readable } from "stream"; /** * Parsing @@ -40,8 +41,19 @@ Papa.parse(file, { } }); +const readable = new Readable() +const rows = [ + "1,2,3", + "4,5,6" +] -Papa.parse(Papa.NODE_STREAM_INPUT); +rows.forEach(r => { + readable.push(r); +}); + +const papaStream: NodeJS.ReadWriteStream = Papa.parse(Papa.NODE_STREAM_INPUT); + +readable.pipe(papaStream); /** * Unparsing