mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Fix some issues with the json2csv types (#35749)
This commit is contained in:
parent
f8efb2f7af
commit
4673dba6fe
20
types/json2csv/JSON2CSVBase.d.ts
vendored
20
types/json2csv/JSON2CSVBase.d.ts
vendored
@ -1,12 +1,24 @@
|
||||
export declare namespace json2csv {
|
||||
export interface FieldValueCallback<T> {
|
||||
(row: T, field: string): string;
|
||||
export interface FieldValueCallbackInfo {
|
||||
label: string;
|
||||
default?: string;
|
||||
}
|
||||
|
||||
export type FieldValueCallback<T> = FieldValueCallbackWithoutField<T> | FieldValueCallbackWithField<T>;
|
||||
|
||||
export interface FieldValueCallbackWithoutField<T> {
|
||||
(row: T): any;
|
||||
}
|
||||
|
||||
export interface FieldValueCallbackWithField<T> {
|
||||
(row: T, field: FieldValueCallbackInfo): any;
|
||||
}
|
||||
|
||||
export interface FieldInfo<T> {
|
||||
label: string;
|
||||
label?: string;
|
||||
default?: string;
|
||||
value?: string | FieldValueCallback<T>;
|
||||
value: string | FieldValueCallback<T>;
|
||||
stringify?: boolean;
|
||||
}
|
||||
|
||||
export interface Options<T> {
|
||||
|
||||
2
types/json2csv/index.d.ts
vendored
2
types/json2csv/index.d.ts
vendored
@ -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 <https://github.com/juanjoDiaz>
|
||||
// Daniel Gooß <https://github.com/dangoo>
|
||||
|
||||
@ -33,13 +33,40 @@ interface Car {
|
||||
}
|
||||
|
||||
const opts: json2csv.Options<Car> = {
|
||||
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<ExampleObj> {
|
||||
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<ExampleObj> {
|
||||
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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user