UNPKG

2.15 kBTypeScriptView Raw
1export interface ISharedOptions {
2 /**
3 * Specifies the different types of delimiters
4 */
5 delimiter?: {
6 /** @default ',' */
7 field?: string;
8 /** @default '"' */
9 wrap?: string;
10 /** @default '\n' */
11 eol?: string;
12 };
13
14 /**
15 * Should a unicode character be prepended to allow Excel to open
16 * a UTF-8 encoded file with non-ASCII characters present
17 * @default false
18 */
19 excelBOM?: boolean;
20 /**
21 * Specify the keys (as strings) that should be converted
22 *
23 * * If you have a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']
24 * * If you want all keys to be converted, then specify null or don't specify the option to utilize the default.
25 */
26 keys?: string[];
27 /**
28 * Should the header fields be trimmed
29 * @default false
30 */
31 trimHeaderFields?: boolean;
32 /**
33 * Should the field values be trimmed? (in development)
34 * @default false
35 */
36 trimFieldValues?: boolean;
37}
38
39export interface IFullOptions extends ISharedOptions {
40 /**
41 * Should all documents have the same schema?
42 * @default false
43 */
44 checkSchemaDifferences?: boolean;
45
46 /**
47 * Value that, if specified, will be substituted in for field values
48 * that are undefined, null, or an empty string
49 */
50 emptyFieldValue?: any;
51
52 /**
53 * Should objects in array values be deep-converted to CSV
54 * @default false
55 */
56 expandArrayObjects?: boolean;
57
58 /**
59 * Should the auto-generated header be prepended as the first line in the CSV
60 * @default true
61 */
62 prependHeader?: boolean;
63 /**
64 * Should the header keys be sorted in alphabetical order
65 * @default false
66 */
67 sortHeader?: boolean;
68
69}
70
71export function json2csv(data: object[],
72 callback: (err?: Error, csv?: string) => void, options?: IFullOptions): void;
73
74export function json2csvAsync(data: object[], options?: IFullOptions): Promise<string>;
75
76export function csv2json(csv: string,
77 callback: (err?: Error, data?: any[]) => void, options?: ISharedOptions): void;
78
79export function csv2jsonAsync(csv: string, options?: ISharedOptions): Promise<any[]>;