From e3692acebbb1a51f948ccb1b7cedd709d236ab06 Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Mon, 5 Mar 2018 23:50:08 +0200 Subject: [PATCH] Add json2csv types --- types/json2csv/JSON2CSVBase.d.ts | 108 ++++++++++++++++++++++++++ types/json2csv/JSON2CSVParser.d.ts | 30 +++++++ types/json2csv/JSON2CSVTransform.d.ts | 97 +++++++++++++++++++++++ types/json2csv/index.d.ts | 18 +++++ types/json2csv/json2csv-tests.ts | 98 +++++++++++++++++++++++ types/json2csv/tsconfig.json | 23 ++++++ types/json2csv/tslint.json | 1 + 7 files changed, 375 insertions(+) create mode 100644 types/json2csv/JSON2CSVBase.d.ts create mode 100644 types/json2csv/JSON2CSVParser.d.ts create mode 100644 types/json2csv/JSON2CSVTransform.d.ts create mode 100644 types/json2csv/index.d.ts create mode 100644 types/json2csv/json2csv-tests.ts create mode 100644 types/json2csv/tsconfig.json create mode 100644 types/json2csv/tslint.json diff --git a/types/json2csv/JSON2CSVBase.d.ts b/types/json2csv/JSON2CSVBase.d.ts new file mode 100644 index 0000000000..165497b4c8 --- /dev/null +++ b/types/json2csv/JSON2CSVBase.d.ts @@ -0,0 +1,108 @@ +export declare namespace json2csv { + export interface FieldValueCallback { + (row: T, field: string): string; + } + + export interface FieldInfo { + label: string; + default?: string; + value?: string | FieldValueCallback; + } + + export interface Options { + fields?: (string | FieldInfo); + ndjson?: boolean; + unwind?: string | string[]; + flatten?: boolean; + defaultValue?: string; + quote?: string; + doubleQuote?: string; + delimiter?: string; + eol?: string; + excelStrings?: boolean; + header?: boolean; + includeEmptyRows?: boolean; + withBOM?: boolean; + } +} + +declare abstract class JSON2CSVBase { + constructor(opts?: json2csv.Options); + + /** + * Check passing opts and set defaults. + * + * @param {json2csv.Options} opts Options object containing fields, + * delimiter, default value, quote mark, header, etc. + * @returns {json2csv.Options} preprocessed Options object + */ + protected preprocessOpts(opts?: json2csv.Options) : json2csv.Options; + + /** + * Create the title row with all the provided fields as column headings + * + * @returns {string} titles as a string + */ + protected getHeader(): string; + + /** + * Preprocess each object according to the give opts (unwind, flatten, etc.). + * + * @param {object} row JSON object to be converted in a CSV row + */ + protected preprocessRow(row: T): object; + + /** + * Create the content of a specific CSV row + * + * @param {object} row JSON object to be converted in a CSV row + * @returns {string} CSV string (row) + */ + protected processRow(row: T): string; + + /** + * Create the content of a specfic CSV row cell + * + * @param {object} row JSON object representing the CSV row that the cell belongs to + * @param {object} fieldInfo Details of the field to process to be a CSV cell + * @returns {string} CSV string (cell) + */ + protected processCell(row: T, fieldInfo: json2csv.FieldInfo) : string; + + /** + * Create the content of a specfic CSV row cell + * + * @param {object} row JSON object representing the CSV row that the cell belongs to + * @param {json2csv.FieldInfo} fieldInfo Details of the field to process to be a CSV cell + * @returns {any} Field value + */ + protected getValue(row: T, fieldInfo: json2csv.FieldInfo): any; + + /** + * Create the content of a specfic CSV row cell + * + * @param {any} value Value to be included in a CSV cell + * @param {Boolean} stringify Details of the field to process to be a CSV cell + * @returns {string} Value stringified and processed + */ + protected processValue(value: any, stringify: Boolean): string; + + /** + * Performs the flattening of a data row recursively + * + * @param {object} dataRow Original JSON object + * @returns {object} Flattened object + */ + protected flatten(dataRow: T): object; + + /** + * Performs the unwind recursively in specified sequence + * + * @param {object[]} dataRow Original JSON object + * @param {string[]} unwindPaths The paths as strings to be used to deconstruct the array + * @returns {Array} Array of objects containing all rows after unwind of chosen paths + */ + protected unwindData(dataRow: Array, unwindPaths: Array): Array; +} + +export default JSON2CSVBase; diff --git a/types/json2csv/JSON2CSVParser.d.ts b/types/json2csv/JSON2CSVParser.d.ts new file mode 100644 index 0000000000..1f134f874c --- /dev/null +++ b/types/json2csv/JSON2CSVParser.d.ts @@ -0,0 +1,30 @@ +import JSON2CSVBase from './JSON2CSVBase'; + +declare class JSON2CSVParser extends JSON2CSVBase { + /** + * Main function that converts json to csv. + * + * @param {object|object[]} data Array of JSON objects to be converted to CSV + * @returns {string} The CSV formated data as a string + */ + public parse(data: Readonly | ReadonlyArray): string; + + /** + * Preprocess the data according to the give opts (unwind, flatten, etc.) + and calculate the fields and field names if they are not provided. + * + * @param {object|object[]} data Array or object to be converted to CSV + * @returns {object[]} Preprocessed data ready to be processed + */ + protected preprocessData(data: T | Array): Array; + + /** + * Create the content row by row below the header + * + * @param {object[]} data Array of JSON objects to be converted to CSV + * @returns {string} CSV string (body) + */ + protected processData(data: Array): string; +} + +export default JSON2CSVParser; diff --git a/types/json2csv/JSON2CSVTransform.d.ts b/types/json2csv/JSON2CSVTransform.d.ts new file mode 100644 index 0000000000..1785e09214 --- /dev/null +++ b/types/json2csv/JSON2CSVTransform.d.ts @@ -0,0 +1,97 @@ +/// + +import { json2csv } from './JSON2CSVBase'; +import JSON2CSVBase from './JSON2CSVBase'; +import { Transform, TransformOptions } from 'stream'; + +declare class JSON2CSVTransform extends Transform { // implements JSON2CSVBase + constructor(opts?: json2csv.Options, transformOpts?: TransformOptions); + + /** + * Transforms an incoming json data to csv and pushes it downstream. + * + * @param {object} data JSON object to be converted in a CSV row + */ + protected pushLine(data: T): void; + + /******************************************************************************* + * Everything below is copy-pasted from JSON2CSVBase and should be keep in sync * + ********************************************************************************/ + + /** + * Check passing opts and set defaults. + * + * @param {json2csv.Options} opts Options object containing fields, + * delimiter, default value, quote mark, header, etc. + * @returns {json2csv.Options} preprocessed Options object + */ + protected preprocessOpts(opts?: json2csv.Options) : json2csv.Options; + + /** + * Create the title row with all the provided fields as column headings + * + * @returns {string} titles as a string + */ + protected getHeader(): string; + + /** + * Preprocess each object according to the give opts (unwind, flatten, etc.). + * + * @param {object} row JSON object to be converted in a CSV row + */ + protected preprocessRow(row: T): object; + + /** + * Create the content of a specific CSV row + * + * @param {object} row JSON object to be converted in a CSV row + * @returns {string} CSV string (row) + */ + protected processRow(row: T): string; + + /** + * Create the content of a specfic CSV row cell + * + * @param {object} row JSON object representing the CSV row that the cell belongs to + * @param {object} fieldInfo Details of the field to process to be a CSV cell + * @returns {string} CSV string (cell) + */ + protected processCell(row: T, fieldInfo: json2csv.FieldInfo) : string; + + /** + * Create the content of a specfic CSV row cell + * + * @param {object} row JSON object representing the CSV row that the cell belongs to + * @param {json2csv.FieldInfo} fieldInfo Details of the field to process to be a CSV cell + * @returns {any} Field value + */ + protected getValue(row: T, fieldInfo: json2csv.FieldInfo): any; + + /** + * Create the content of a specfic CSV row cell + * + * @param {any} value Value to be included in a CSV cell + * @param {Boolean} stringify Details of the field to process to be a CSV cell + * @returns {string} Value stringified and processed + */ + protected processValue(value: any, stringify: Boolean): string; + + /** + * Performs the flattening of a data row recursively + * + * @param {object} dataRow Original JSON object + * @returns {object} Flattened object + */ + protected flatten(dataRow: T): object; + + /** + * Performs the unwind recursively in specified sequence + * + * @param {object[]} dataRow Original JSON object + * @param {string[]} unwindPaths The paths as strings to be used to deconstruct the array + * @returns {Array} Array of objects containing all rows after unwind of chosen paths + */ + protected unwindData(dataRow: Array, unwindPaths: Array): Array; +} + +export default JSON2CSVTransform; diff --git a/types/json2csv/index.d.ts b/types/json2csv/index.d.ts new file mode 100644 index 0000000000..159dec6eb1 --- /dev/null +++ b/types/json2csv/index.d.ts @@ -0,0 +1,18 @@ +// Type definitions for json2csv 4.0 +// Project: https://github.com/zemirco/json2csv +// Definitions by: Juanjo Diaz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import { json2csv } from './JSON2CSVBase'; +import JSON2CSVParser from './JSON2CSVParser'; +import JSON2CSVTransform from './JSON2CSVTransform'; + +export { + json2csv, + JSON2CSVParser as Parser, + JSON2CSVTransform as Transform +}; + +// Convenience method to keep the API similar to version 3.X +export function parse(data: Readonly | ReadonlyArray, opts?: json2csv.Options): string; diff --git a/types/json2csv/json2csv-tests.ts b/types/json2csv/json2csv-tests.ts new file mode 100644 index 0000000000..566cc06cee --- /dev/null +++ b/types/json2csv/json2csv-tests.ts @@ -0,0 +1,98 @@ +import { json2csv, parse, Parser, Transform } from 'json2csv'; +import { Transform as NodeTransform } from 'stream'; + +let s: string; +let obj: object; + +interface ExampleObj { + str?: string; + num?: number; + obj?: object; +} + +/************** + * Public API * + **************/ +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; + +/******************** + * Internal Methods * + ********************/ +class ParserExt extends Parser { + constructor() { + super(); + // Parser methods + obj = this.preprocessData({}); + obj = this.preprocessData({ str: '', num: 1, obj: {} }); + obj = this.preprocessData([]); + s = this.processData([]); + + // JSON2CSVBase methods + let opts: json2csv.Options; + opts = this.preprocessOpts(); + opts = this.preprocessOpts(opts); + s = this.getHeader(); + obj = this.preprocessRow({}); + obj = this.preprocessRow({ str: '', num: 1, obj: {} }); + s = this.processRow({}); + s = this.processRow({ str: '', num: 1, obj: {} }); + s = this.processCell({}, { label: 'test', default: 'test2', value: 'field' }); + s = this.processCell({ str: '', num: 1, obj: {} }, { label: 'test', default: 'test2', value: 'field' }); + s = this.processCell({}, { label: 'test', default: 'test2', value: (row: object, field: string) => 'string' }); + s = this.processCell({ str: '', num: 1, obj: {} }, { label: 'test', default: 'test2', value: (row: object, field: string) => 'string' }); + this.getValue({}, { label: 'test' }); + this.getValue({ str: '', num: 1, obj: {} }, { label: 'test' }); + s = this.processValue(undefined, true); + s = this.processValue(null, true); + s = this.processValue(1, true); + s = this.processValue('test', true); + s = this.processValue(new Date(), true); + s = this.processValue({}, true); + s = this.processValue([], true); + const flattenedData: object = this.flatten({}); + const unwindedData: object[] = this.unwindData([], []); + } +} + +class TransformExt extends Transform { + constructor() { + super(); + // Transform methods + this.pushLine({}); + this.pushLine({ str: '', num: 1, obj: {} }); + + // JSON2CSVBase methods + let opts: json2csv.Options; + opts = this.preprocessOpts(); + opts = this.preprocessOpts(opts); + s = this.getHeader(); + obj = this.preprocessRow({}); + obj = this.preprocessRow({ str: '', num: 1, obj: {} }); + s = this.processRow({}); + s = this.processRow({ str: '', num: 1, obj: {} }); + s = this.processCell({}, { label: 'test', default: 'test2', value: 'field' }); + s = this.processCell({ str: '', num: 1, obj: {} }, { label: 'test', default: 'test2', value: 'field' }); + s = this.processCell({}, { label: 'test', default: 'test2', value: (row: object, field: string) => 'string' }); + s = this.processCell({ str: '', num: 1, obj: {} }, { label: 'test', default: 'test2', value: (row: object, field: string) => 'string' }); + this.getValue({}, { label: 'test' }); + this.getValue({ str: '', num: 1, obj: {} }, { label: 'test' }); + s = this.processValue(undefined, true); + s = this.processValue(null, true); + s = this.processValue(1, true); + s = this.processValue('test', true); + s = this.processValue(new Date(), true); + s = this.processValue({}, true); + s = this.processValue([], true); + const flattenedData: object = this.flatten({}); + const unwindedData: object[] = this.unwindData([], []); + } +} diff --git a/types/json2csv/tsconfig.json b/types/json2csv/tsconfig.json new file mode 100644 index 0000000000..23d379918c --- /dev/null +++ b/types/json2csv/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "json2csv-tests.ts" + ] +} \ No newline at end of file diff --git a/types/json2csv/tslint.json b/types/json2csv/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/json2csv/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }