diff --git a/types/json2csv/JSON2CSVAsyncParser.d.ts b/types/json2csv/JSON2CSVAsyncParser.d.ts new file mode 100644 index 0000000000..3b5d396542 --- /dev/null +++ b/types/json2csv/JSON2CSVAsyncParser.d.ts @@ -0,0 +1,22 @@ +import { Transform, TransformOptions, Writable, Readable } 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: Readable): JSON2CSVAsyncParser; + + public throughTransform(transform: Transform): JSON2CSVAsyncParser; + + public toOutput(output: Writable): JSON2CSVAsyncParser; + + public promise(): Promise; +} + +export default JSON2CSVAsyncParser; diff --git a/types/json2csv/index.d.ts b/types/json2csv/index.d.ts index 55aca2687c..e4efa6f264 100644 --- a/types/json2csv/index.d.ts +++ b/types/json2csv/index.d.ts @@ -1,18 +1,33 @@ -// Type definitions for json2csv 4.2 +// Type definitions for json2csv 4.4 // Project: https://github.com/zemirco/json2csv // Definitions by: Juanjo Diaz +// Daniel Gooß // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 +import { Readable, 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 | Readable, + opts?: json2csv.Options, + transformOpts?: TransformOptions +): Promise; diff --git a/types/json2csv/json2csv-tests.ts b/types/json2csv/json2csv-tests.ts index 42f8a58b70..d80cccc629 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,69 @@ 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).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)) + .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 *