UNPKG

1.68 kBPlain TextView Raw
1import {CsvWriter} from './csv-writer';
2import {CsvStringifierFactory} from './csv-stringifier-factory';
3import {ObjectStringifierHeader} from './record';
4
5export interface ArrayCsvWriterParams {
6 path: string;
7 header?: string[];
8 fieldDelimiter?: string;
9 recordDelimiter?: string;
10 alwaysQuote?: boolean;
11 encoding?: string;
12 append?: boolean;
13}
14
15export interface ObjectCsvWriterParams {
16 path: string;
17 header: ObjectStringifierHeader;
18 fieldDelimiter?: string;
19 recordDelimiter?: string;
20 headerIdDelimiter?: string;
21 alwaysQuote?: boolean;
22 encoding?: string;
23 append?: boolean;
24}
25
26export class CsvWriterFactory {
27 constructor(private readonly csvStringifierFactory: CsvStringifierFactory) {}
28
29 createArrayCsvWriter(params: ArrayCsvWriterParams) {
30 const csvStringifier = this.csvStringifierFactory.createArrayCsvStringifier({
31 header: params.header,
32 fieldDelimiter: params.fieldDelimiter,
33 recordDelimiter: params.recordDelimiter,
34 alwaysQuote: params.alwaysQuote
35 });
36 return new CsvWriter(csvStringifier, params.path, params.encoding, params.append);
37 }
38
39 createObjectCsvWriter(params: ObjectCsvWriterParams) {
40 const csvStringifier = this.csvStringifierFactory.createObjectCsvStringifier({
41 header: params.header,
42 fieldDelimiter: params.fieldDelimiter,
43 recordDelimiter: params.recordDelimiter,
44 headerIdDelimiter: params.headerIdDelimiter,
45 alwaysQuote: params.alwaysQuote
46 });
47 return new CsvWriter(csvStringifier, params.path, params.encoding, params.append);
48 }
49}