1 | import {assertFile, testFilePath} from './helper';
|
2 | import {CsvWriter} from '../lib/csv-writer';
|
3 | import {writeFileSync} from 'fs';
|
4 | import {createObjectCsvWriter} from '../index';
|
5 | import {ObjectMap} from '../lib/lang/object';
|
6 |
|
7 | describe('Write object records into CSV', () => {
|
8 |
|
9 | const makeFilePath = (id: string) => testFilePath(`object-${id}`);
|
10 | const records = [
|
11 | {name: 'Bob', lang: 'French', address: {country: 'France'}},
|
12 | {name: 'Mary', lang: 'English'}
|
13 | ];
|
14 |
|
15 | describe('When only path and header ids are given', () => {
|
16 | const filePath = makeFilePath('minimum');
|
17 | let writer: CsvWriter<ObjectMap<any>>;
|
18 |
|
19 | beforeEach(() => {
|
20 | writer = createObjectCsvWriter({
|
21 | path: filePath,
|
22 | header: ['name', 'lang']
|
23 | });
|
24 | });
|
25 |
|
26 | it('writes records to a new file', async () => {
|
27 | await writer.writeRecords(records);
|
28 | assertFile(filePath, 'Bob,French\nMary,English\n');
|
29 | });
|
30 |
|
31 | it('appends records when requested to write to the same file', async () => {
|
32 | await writer.writeRecords([records[0]]);
|
33 | await writer.writeRecords([records[1]]);
|
34 | assertFile(filePath, 'Bob,French\nMary,English\n');
|
35 | });
|
36 | });
|
37 |
|
38 | describe('When header ids are given with reverse order', () => {
|
39 | const filePath = makeFilePath('column-order');
|
40 | const writer = createObjectCsvWriter({
|
41 | path: filePath,
|
42 | header: ['lang', 'name']
|
43 | });
|
44 |
|
45 | it('also writes columns with reverse order', async () => {
|
46 | await writer.writeRecords(records);
|
47 | assertFile(filePath, 'French,Bob\nEnglish,Mary\n');
|
48 | });
|
49 | });
|
50 |
|
51 | describe('When field header is given with titles', () => {
|
52 | const filePath = makeFilePath('header');
|
53 | let writer: CsvWriter<ObjectMap<any>>;
|
54 |
|
55 | beforeEach(() => {
|
56 | writer = createObjectCsvWriter({
|
57 | path: filePath,
|
58 | header: [{id: 'name', title: 'NAME'}, {id: 'lang', title: 'LANGUAGE'}]
|
59 | });
|
60 | });
|
61 |
|
62 | it('writes a header', async () => {
|
63 | await writer.writeRecords(records);
|
64 | assertFile(filePath, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
|
65 | });
|
66 |
|
67 | it('appends records without headers', async () => {
|
68 | await writer.writeRecords([records[0]]);
|
69 | await writer.writeRecords([records[1]]);
|
70 | assertFile(filePath, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
|
71 | });
|
72 | });
|
73 |
|
74 | describe('When `append` flag is specified', () => {
|
75 | const filePath = makeFilePath('append');
|
76 | writeFileSync(filePath, 'Mike,German\n', 'utf8');
|
77 | const writer = createObjectCsvWriter({
|
78 | path: filePath,
|
79 | header: ['name', 'lang'],
|
80 | append: true
|
81 | });
|
82 |
|
83 | it('do not overwrite the existing contents and appends records to them', async () => {
|
84 | await writer.writeRecords([records[1]]);
|
85 | assertFile(filePath, 'Mike,German\nMary,English\n');
|
86 | });
|
87 | });
|
88 |
|
89 | describe('When encoding is specified', () => {
|
90 | const filePath = makeFilePath('encoding');
|
91 | const writer = createObjectCsvWriter({
|
92 | path: filePath,
|
93 | header: ['name', 'lang'],
|
94 | encoding: 'utf16le'
|
95 | });
|
96 |
|
97 | it('writes to a file with the specified encoding', async () => {
|
98 | await writer.writeRecords(records);
|
99 | assertFile(filePath, 'Bob,French\nMary,English\n', 'utf16le');
|
100 | });
|
101 | });
|
102 |
|
103 | describe('When semicolon is specified as a field delimiter', () => {
|
104 | const filePath = makeFilePath('field-delimiter');
|
105 | const writer = createObjectCsvWriter({
|
106 | path: filePath,
|
107 | header: [{id: 'name', title: 'NAME'}, {id: 'lang', title: 'LANGUAGE'}],
|
108 | fieldDelimiter: ';'
|
109 | });
|
110 |
|
111 | it('uses semicolon instead of comma to separate fields', async () => {
|
112 | await writer.writeRecords(records);
|
113 | assertFile(filePath, 'NAME;LANGUAGE\nBob;French\nMary;English\n');
|
114 | });
|
115 | });
|
116 |
|
117 | describe('When newline is specified', () => {
|
118 | const filePath = makeFilePath('newline');
|
119 | const writer = createObjectCsvWriter({
|
120 | path: filePath,
|
121 | header: ['name', 'lang'],
|
122 | recordDelimiter: '\r\n'
|
123 | });
|
124 |
|
125 | it('writes to a file with the specified newline character', async () => {
|
126 | await writer.writeRecords(records);
|
127 | assertFile(filePath, 'Bob,French\r\nMary,English\r\n');
|
128 | });
|
129 | });
|
130 |
|
131 | describe('When `alwaysQuote` flag is set', () => {
|
132 | const filePath = makeFilePath('always-quote');
|
133 | const writer = createObjectCsvWriter({
|
134 | path: filePath,
|
135 | header: [{id: 'name', title: 'NAME'}, {id: 'lang', title: 'LANGUAGE'}],
|
136 | alwaysQuote: true
|
137 | });
|
138 |
|
139 | it('quotes all fields', async () => {
|
140 | await writer.writeRecords(records);
|
141 | assertFile(filePath, '"NAME","LANGUAGE"\n"Bob","French"\n"Mary","English"\n');
|
142 | });
|
143 | });
|
144 |
|
145 | describe('When `headerIdDelimiter` flag is set', () => {
|
146 | const filePath = makeFilePath('nested');
|
147 | const writer = createObjectCsvWriter({
|
148 | path: filePath,
|
149 | header: [{id: 'name', title: 'NAME'}, {id: 'address.country', title: 'COUNTRY'}],
|
150 | headerIdDelimiter: '.'
|
151 | });
|
152 |
|
153 | it('breaks keys into key paths', async () => {
|
154 | await writer.writeRecords(records);
|
155 | assertFile(filePath, 'NAME,COUNTRY\nBob,France\nMary,\n');
|
156 | });
|
157 | });
|
158 | });
|