diff --git a/types/json2csv/JSON2CSVBase.d.ts b/types/json2csv/JSON2CSVBase.d.ts index 1faf2a5318..022c7cab77 100644 --- a/types/json2csv/JSON2CSVBase.d.ts +++ b/types/json2csv/JSON2CSVBase.d.ts @@ -1,12 +1,24 @@ export declare namespace json2csv { - export interface FieldValueCallback { - (row: T, field: string): string; + export interface FieldValueCallbackInfo { + label: string; + default?: string; + } + + export type FieldValueCallback = FieldValueCallbackWithoutField | FieldValueCallbackWithField; + + export interface FieldValueCallbackWithoutField { + (row: T): any; + } + + export interface FieldValueCallbackWithField { + (row: T, field: FieldValueCallbackInfo): any; } export interface FieldInfo { - label: string; + label?: string; default?: string; - value?: string | FieldValueCallback; + value: string | FieldValueCallback; + stringify?: boolean; } export interface Options { diff --git a/types/json2csv/index.d.ts b/types/json2csv/index.d.ts index e4efa6f264..edf693cc3d 100644 --- a/types/json2csv/index.d.ts +++ b/types/json2csv/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for json2csv 4.4 +// Type definitions for json2csv 4.5 // Project: https://github.com/zemirco/json2csv // Definitions by: Juanjo Diaz // Daniel Gooß diff --git a/types/json2csv/json2csv-tests.ts b/types/json2csv/json2csv-tests.ts index d80cccc629..cad3745d68 100644 --- a/types/json2csv/json2csv-tests.ts +++ b/types/json2csv/json2csv-tests.ts @@ -33,13 +33,40 @@ interface Car { } const opts: json2csv.Options = { - fields: [{ - label: 'Car Name', - value: 'car' - }, { - label: 'Price USD', - value: 'price' - }] + fields: [ + // Supports pathname -> pathvalue + 'simplepath', // equivalent to {value:'simplepath'} + 'path.to.value', // also equivalent to {value:'path.to.value'} + + // Supports label -> simple path + { + label: 'some label', // (optional, column will be labeled 'path.to.something' if not defined) + value: 'path.to.something', // data.path.to.something + default: 'NULL' // default if value is not found (optional, overrides `defaultValue` for column) + }, + + // Supports label -> derived value + { + label: 'some label', // Soptional, column will be labeled with the function name or empty if the function is anonymous) + value: (row: Car, field: json2csv.FieldValueCallbackInfo) => { + if (field) { + return (row as any)[field.label].toLowerCase() || field.default; + } + }, + default: 'NULL', // default if value function returns null or undefined + }, + + // Supports label -> derived value + { + value: (row: Car) => row.car + }, + + // Supports label -> derived value + { + value: (row: Car) => `"${row.car}"`, + stringify: false // This flag signals if the resulting string should be quoted (stringified) or not (optional, default: true) + }, + ] }; const data = [{ @@ -129,10 +156,10 @@ class ParserExt extends Parser { 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.processCell({}, { label: 'test', default: 'test2', value: (row: object, field: json2csv.FieldValueCallbackInfo) => 'string' }); + s = this.processCell({ str: '', num: 1, obj: {} }, { label: 'test', default: 'test2', value: (row: object) => 'string' }); + this.getValue({}, { value: 'test' }); + this.getValue({ str: '', num: 1, obj: {} }, { value: 'test' }); s = this.processValue(undefined, true); s = this.processValue(null, true); s = this.processValue(1, true); @@ -163,10 +190,10 @@ class TransformExt extends Transform { 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.processCell({}, { label: 'test', default: 'test2', value: (row: object, field: json2csv.FieldValueCallbackInfo) => 'string' }); + s = this.processCell({ str: '', num: 1, obj: {} }, { label: 'test', default: 'test2', value: (row: object) => 'string' }); + this.getValue({}, { value: 'test' }); + this.getValue({ str: '', num: 1, obj: {} }, { value: 'test' }); s = this.processValue(undefined, true); s = this.processValue(null, true); s = this.processValue(1, true);