Merge pull request #34618 from Dangoo/feat/json2csv@4.4

Update typings for json2csv@4.4.x
This commit is contained in:
Armando Aguirre
2019-04-15 13:29:58 -07:00
committed by GitHub
3 changed files with 114 additions and 13 deletions

22
types/json2csv/JSON2CSVAsyncParser.d.ts vendored Normal file
View File

@@ -0,0 +1,22 @@
import { Transform, TransformOptions, Writable, Readable } from "stream";
import JSON2CSVBase, { json2csv } from "./JSON2CSVBase";
import JSON2CSVTransform from "./JSON2CSVTransform";
declare class JSON2CSVAsyncParser<T> extends JSON2CSVBase<T> {
public input: Transform;
public processor: Writable;
public transform: JSON2CSVTransform<T>;
constructor(opts?: json2csv.Options<T>, transformOpts?: TransformOptions);
public fromInput(input: Readable): JSON2CSVAsyncParser<T>;
public throughTransform(transform: Transform): JSON2CSVAsyncParser<T>;
public toOutput(output: Writable): JSON2CSVAsyncParser<T>;
public promise(): Promise<string>;
}
export default JSON2CSVAsyncParser;

View File

@@ -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 <https://github.com/juanjoDiaz>
// Daniel Gooß <https://github.com/dangoo>
// 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<T>(data: Readonly<T> | ReadonlyArray<T>, opts?: json2csv.Options<T>): string;
export function parse<T>(
data: Readonly<T> | ReadonlyArray<T>,
opts?: json2csv.Options<T>
): string;
export function parseAsync<T>(
data: Readonly<T> | ReadonlyArray<T> | Readable,
opts?: json2csv.Options<T>,
transformOpts?: TransformOptions
): Promise<string>;

View File

@@ -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<ExampleObj> = new Parser({});
s = parser.parse({ str: '', num: 1, obj: {} });
parser.parse([]);
const transform: Transform<ExampleObj> = new Transform<ExampleObj>({ quote: '' });
const nodeTransform: NodeTransform = transform;
// Tests using examples from Readme
interface Car {
car: string;
price: number;
}
new Parser();
const parser: Parser<ExampleObj> = new Parser({});
s = parser.parse({ str: '', num: 1, obj: {} });
const opts: json2csv.Options<Car> = {
fields: [{
label: 'Car Name',
@@ -36,11 +42,69 @@ const opts: json2csv.Options<Car> = {
}]
};
const json2csvParser = new Parser<Car>(opts);
const data = [{
car: 'VW Beetle',
price: 1395
}, {
car: 'VW T1',
price: 1357
}];
parser.parse([]);
const transform: Transform<ExampleObj> = new Transform<ExampleObj>({ 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 *