-
Notifications
You must be signed in to change notification settings - Fork 206
/
FormatterOptions.ts
74 lines (54 loc) · 2.13 KB
/
FormatterOptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Row, RowTransformFunction } from './types';
interface QuoteColumnMap {
[s: string]: boolean;
}
type QuoteColumns = boolean | boolean[] | QuoteColumnMap;
export interface FormatterOptionsArgs<I extends Row, O extends Row> {
objectMode?: boolean;
delimiter?: string;
rowDelimiter?: string;
quote?: string | boolean;
escape?: string;
quoteColumns?: QuoteColumns;
quoteHeaders?: QuoteColumns;
headers?: null | boolean | string[];
writeHeaders?: boolean;
includeEndRowDelimiter?: boolean;
writeBOM?: boolean;
transform?: RowTransformFunction<I, O>;
alwaysWriteHeaders?: boolean;
}
export class FormatterOptions<I extends Row, O extends Row> {
public readonly objectMode: boolean = true;
public readonly delimiter: string = ',';
public readonly rowDelimiter: string = '\n';
public readonly quote: string = '"';
public readonly escape: string = this.quote;
public readonly quoteColumns: QuoteColumns = false;
public readonly quoteHeaders: QuoteColumns = this.quoteColumns;
public readonly headers: null | string[] = null;
public readonly includeEndRowDelimiter: boolean = false;
public readonly transform?: RowTransformFunction<I, O>;
public readonly shouldWriteHeaders: boolean;
public readonly writeBOM: boolean = false;
public readonly escapedQuote: string;
public readonly BOM: string = '\ufeff';
public readonly alwaysWriteHeaders: boolean = false;
public constructor(opts: FormatterOptionsArgs<I, O> = {}) {
Object.assign(this, opts || {});
if (typeof opts?.quoteHeaders === 'undefined') {
this.quoteHeaders = this.quoteColumns;
}
if (opts?.quote === true) {
this.quote = '"';
} else if (opts?.quote === false) {
this.quote = '';
}
if (typeof opts?.escape !== 'string') {
this.escape = this.quote;
}
this.shouldWriteHeaders = !!this.headers && (opts.writeHeaders ?? true);
this.headers = Array.isArray(this.headers) ? this.headers : null;
this.escapedQuote = `${this.escape}${this.quote}`;
}
}