From 62b1431d55872f2aa81f5ab2871455ba9d685991 Mon Sep 17 00:00:00 2001 From: dangoo Date: Wed, 10 Apr 2019 11:29:36 +0200 Subject: [PATCH 1/7] feat: adds typings for AsyncParser --- types/json2csv/JSON2CSVAsyncParser.d.ts | 23 +++++++++++++++++++++++ types/json2csv/index.d.ts | 24 +++++++++++++++++++----- types/json2csv/json2csv-tests.ts | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 types/json2csv/JSON2CSVAsyncParser.d.ts diff --git a/types/json2csv/JSON2CSVAsyncParser.d.ts b/types/json2csv/JSON2CSVAsyncParser.d.ts new file mode 100644 index 0000000000..a9bac21226 --- /dev/null +++ b/types/json2csv/JSON2CSVAsyncParser.d.ts @@ -0,0 +1,23 @@ +import { ReadStream, WriteStream } from "fs"; +import { Transform, TransformOptions, Writable } from "stream"; + +import JSON2CSVBase, { json2csv } from "./JSON2CSVBase"; +import JSON2CSVTransform from "./JSON2CSVTransform"; + +declare class JSON2CSVAsyncParser extends JSON2CSVBase { + public input: Transform; + public processor: Writable; + public transform: JSON2CSVTransform; + + constructor(opts?: json2csv.Options, transformOpts?: TransformOptions); + + public fromInput(input: ReadStream): JSON2CSVAsyncParser; + + public throughTransform(transform: Transform): JSON2CSVAsyncParser; + + public toOutput(output: WriteStream): JSON2CSVAsyncParser; + + public promise(): Promise; +} + +export default JSON2CSVAsyncParser; diff --git a/types/json2csv/index.d.ts b/types/json2csv/index.d.ts index 55aca2687c..bb8e0ba5b9 100644 --- a/types/json2csv/index.d.ts +++ b/types/json2csv/index.d.ts @@ -1,18 +1,32 @@ -// Type definitions for json2csv 4.2 +// Type definitions for json2csv 4.4 // Project: https://github.com/zemirco/json2csv -// Definitions by: Juanjo Diaz +// Definitions by: Daniel Gooß // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 +import { TransformOptions } from 'stream'; + import { json2csv } from './JSON2CSVBase'; import JSON2CSVParser from './JSON2CSVParser'; +import JSON2CSVAsyncParser from './JSON2CSVAsyncParser'; import JSON2CSVTransform from './JSON2CSVTransform'; +export default json2csv; + export { - json2csv, - JSON2CSVParser as Parser, + JSON2CSVParser as Parser, + JSON2CSVAsyncParser as AsyncParser, JSON2CSVTransform as Transform }; // Convenience method to keep the API similar to version 3.X -export function parse(data: Readonly | ReadonlyArray, opts?: json2csv.Options): string; +export function parse( + data: Readonly | ReadonlyArray, + opts?: json2csv.Options +): string; + +export function parseAsync( + data: Readonly | ReadonlyArray, + opts?: json2csv.Options, + transformOpts?: TransformOptions +): Promise; diff --git a/types/json2csv/json2csv-tests.ts b/types/json2csv/json2csv-tests.ts index 42f8a58b70..6f73ea4817 100644 --- a/types/json2csv/json2csv-tests.ts +++ b/types/json2csv/json2csv-tests.ts @@ -1,4 +1,4 @@ -import { json2csv, parse, Parser, Transform } from 'json2csv'; +import json2csv, { parse, Parser, Transform } from 'json2csv'; import { Transform as NodeTransform } from 'stream'; let s: string; From ae50a2568dee1f95e0acc12a7da5e4e6262c7b4d Mon Sep 17 00:00:00 2001 From: dangoo Date: Wed, 10 Apr 2019 11:58:00 +0200 Subject: [PATCH 2/7] feat: adds test cases for AsyncParser --- types/json2csv/json2csv-tests.ts | 75 ++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/types/json2csv/json2csv-tests.ts b/types/json2csv/json2csv-tests.ts index 6f73ea4817..930903cf75 100644 --- a/types/json2csv/json2csv-tests.ts +++ b/types/json2csv/json2csv-tests.ts @@ -1,5 +1,7 @@ -import json2csv, { parse, Parser, Transform } from 'json2csv'; import { Transform as NodeTransform } from 'stream'; +import { createReadStream, createWriteStream } from 'fs'; + +import json2csv, { AsyncParser, parse, Parser, parseAsync, Transform } from 'json2csv'; let s: string; let obj: object; @@ -17,15 +19,19 @@ parse({}); parse([]); parse({}, {}); +new Parser(); +const parser: Parser = new Parser({}); +s = parser.parse({ str: '', num: 1, obj: {} }); +parser.parse([]); +const transform: Transform = new Transform({ quote: '' }); +const nodeTransform: NodeTransform = transform; + +// Tests using examples from Readme interface Car { car: string; price: number; } -new Parser(); -const parser: Parser = new Parser({}); -s = parser.parse({ str: '', num: 1, obj: {} }); - const opts: json2csv.Options = { fields: [{ label: 'Car Name', @@ -36,11 +42,62 @@ const opts: json2csv.Options = { }] }; -const json2csvParser = new Parser(opts); +const data = [{ + car: 'VW Beetle', + price: 1395 +}, { + car: 'VW T1', + price: 1357 +}]; -parser.parse([]); -const transform: Transform = new Transform({ quote: '' }); -const nodeTransform: NodeTransform = transform; +// Test for Synchronous Parser +try { + const parser = new Parser(opts); + const csv = parser.parse({ car: '', price: 1 }); + console.log(csv); +} catch (err) { + console.error(err); +} + +// Test convenience method "parse" +try { + const csv = parse(data, opts); + console.log(csv); +} catch (err) { + console.error(err); +} + +// Test for Asynchronous Parser +const transformOpts = { highWaterMark: 8192 }; + +const asyncParser = new AsyncParser(opts, transformOpts); + +let csv = ''; +asyncParser.processor + .on('data', chunk => (csv += chunk.toString())) + .on('end', () => console.log(csv)) + .on('error', err => console.error(err)); + +// Test for transform events +asyncParser.transform + .on('header', header => console.log(header)) + .on('line', line => console.log(line)) + .on('error', err => console.log(err)); + +asyncParser.input.push(data); // This data might come from an HTTP request, etc. +asyncParser.input.push(null); // Sending `null` to a stream signal that no more data is expected and ends it. + +const input = createReadStream('/path/to/input', { encoding: 'utf8' }); +const output = createWriteStream('path/to/output', { encoding: 'utf8' }); + +asyncParser.fromInput(input).toOutput(output).promise() + .then(csv => console.log(csv)) + .catch(err => console.error(err)); + +// Test convenience method "parseAsync" +parseAsync(data, opts) + .then(csv => console.log(csv)) + .catch(err => console.error(err)); /******************** * Internal Methods * From 02279fbd13be43528ee726a5b33e967f3cb6e50c Mon Sep 17 00:00:00 2001 From: dangoo Date: Mon, 15 Apr 2019 13:17:38 +0200 Subject: [PATCH 3/7] fix: restore original authors name --- types/json2csv/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/json2csv/index.d.ts b/types/json2csv/index.d.ts index bb8e0ba5b9..96a6b5d5ec 100644 --- a/types/json2csv/index.d.ts +++ b/types/json2csv/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for json2csv 4.4 // Project: https://github.com/zemirco/json2csv -// Definitions by: Daniel Gooß +// Definitions by: Juanjo Diaz +// Daniel Gooß // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 From 131387a849d02737776ce78f702a29fcfdb892a9 Mon Sep 17 00:00:00 2001 From: dangoo Date: Mon, 15 Apr 2019 13:26:18 +0200 Subject: [PATCH 4/7] fix: changed r/w interfaces from fs to stream --- types/json2csv/JSON2CSVAsyncParser.d.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/types/json2csv/JSON2CSVAsyncParser.d.ts b/types/json2csv/JSON2CSVAsyncParser.d.ts index a9bac21226..3b5d396542 100644 --- a/types/json2csv/JSON2CSVAsyncParser.d.ts +++ b/types/json2csv/JSON2CSVAsyncParser.d.ts @@ -1,5 +1,4 @@ -import { ReadStream, WriteStream } from "fs"; -import { Transform, TransformOptions, Writable } from "stream"; +import { Transform, TransformOptions, Writable, Readable } from "stream"; import JSON2CSVBase, { json2csv } from "./JSON2CSVBase"; import JSON2CSVTransform from "./JSON2CSVTransform"; @@ -11,11 +10,11 @@ declare class JSON2CSVAsyncParser extends JSON2CSVBase { constructor(opts?: json2csv.Options, transformOpts?: TransformOptions); - public fromInput(input: ReadStream): JSON2CSVAsyncParser; + public fromInput(input: Readable): JSON2CSVAsyncParser; public throughTransform(transform: Transform): JSON2CSVAsyncParser; - public toOutput(output: WriteStream): JSON2CSVAsyncParser; + public toOutput(output: Writable): JSON2CSVAsyncParser; public promise(): Promise; } From fb2d3bac8267511166475dff4d462e1e9af0405b Mon Sep 17 00:00:00 2001 From: dangoo Date: Mon, 15 Apr 2019 13:34:35 +0200 Subject: [PATCH 5/7] fix: add missing param type for parseAsync --- types/json2csv/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/json2csv/index.d.ts b/types/json2csv/index.d.ts index 96a6b5d5ec..e4efa6f264 100644 --- a/types/json2csv/index.d.ts +++ b/types/json2csv/index.d.ts @@ -5,7 +5,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 -import { TransformOptions } from 'stream'; +import { Readable, TransformOptions } from 'stream'; import { json2csv } from './JSON2CSVBase'; import JSON2CSVParser from './JSON2CSVParser'; @@ -27,7 +27,7 @@ export function parse( ): string; export function parseAsync( - data: Readonly | ReadonlyArray, + data: Readonly | ReadonlyArray | Readable, opts?: json2csv.Options, transformOpts?: TransformOptions ): Promise; From 8669daa107d563618811f5d54158ecf916308ab9 Mon Sep 17 00:00:00 2001 From: dangoo Date: Mon, 15 Apr 2019 13:46:16 +0200 Subject: [PATCH 6/7] tests: adds testcase for parseAsync with stream --- types/json2csv/json2csv-tests.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/types/json2csv/json2csv-tests.ts b/types/json2csv/json2csv-tests.ts index 930903cf75..42202be343 100644 --- a/types/json2csv/json2csv-tests.ts +++ b/types/json2csv/json2csv-tests.ts @@ -94,11 +94,16 @@ asyncParser.fromInput(input).toOutput(output).promise() .then(csv => console.log(csv)) .catch(err => console.error(err)); -// Test convenience method "parseAsync" +// Test convenience method "parseAsync" with object input parseAsync(data, opts) .then(csv => console.log(csv)) .catch(err => console.error(err)); +// Test convenience method "parseAsync" with stream input +parseAsync(input, opts) + .then(csv => console.log(csv)) + .catch(err => console.error(err)); + /******************** * Internal Methods * ********************/ From c78162c7ed3256d81b9196afe8ae61a15e71097c Mon Sep 17 00:00:00 2001 From: dangoo Date: Mon, 15 Apr 2019 19:15:55 +0200 Subject: [PATCH 7/7] tests: divided tests for stream and promise --- types/json2csv/json2csv-tests.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types/json2csv/json2csv-tests.ts b/types/json2csv/json2csv-tests.ts index 42202be343..d80cccc629 100644 --- a/types/json2csv/json2csv-tests.ts +++ b/types/json2csv/json2csv-tests.ts @@ -90,10 +90,12 @@ asyncParser.input.push(null); // Sending `null` to a stream signal that no more const input = createReadStream('/path/to/input', { encoding: 'utf8' }); const output = createWriteStream('path/to/output', { encoding: 'utf8' }); -asyncParser.fromInput(input).toOutput(output).promise() +asyncParser.fromInput(input).promise() .then(csv => console.log(csv)) .catch(err => console.error(err)); +asyncParser.fromInput(input).toOutput(output); + // Test convenience method "parseAsync" with object input parseAsync(data, opts) .then(csv => console.log(csv))