UNPKG

12.2 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __generator = (this && this.__generator) || function (thisArg, body) {
12 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14 function verb(n) { return function (v) { return step([n, v]); }; }
15 function step(op) {
16 if (f) throw new TypeError("Generator is already executing.");
17 while (_) try {
18 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19 if (y = 0, t) op = [op[0] & 2, t.value];
20 switch (op[0]) {
21 case 0: case 1: t = op; break;
22 case 4: _.label++; return { value: op[1], done: false };
23 case 5: _.label++; y = op[1]; op = [0]; continue;
24 case 7: op = _.ops.pop(); _.trys.pop(); continue;
25 default:
26 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30 if (t[2]) _.ops.pop();
31 _.trys.pop(); continue;
32 }
33 op = body.call(thisArg, _);
34 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36 }
37};
38Object.defineProperty(exports, "__esModule", { value: true });
39var helper_1 = require("./helper");
40var fs_1 = require("fs");
41var index_1 = require("../index");
42describe('Write object records into CSV', function () {
43 var makeFilePath = function (id) { return helper_1.testFilePath("object-" + id); };
44 var records = [
45 { name: 'Bob', lang: 'French', address: { country: 'France' } },
46 { name: 'Mary', lang: 'English' }
47 ];
48 describe('When only path and header ids are given', function () {
49 var filePath = makeFilePath('minimum');
50 var writer;
51 beforeEach(function () {
52 writer = index_1.createObjectCsvWriter({
53 path: filePath,
54 header: ['name', 'lang']
55 });
56 });
57 it('writes records to a new file', function () { return __awaiter(void 0, void 0, void 0, function () {
58 return __generator(this, function (_a) {
59 switch (_a.label) {
60 case 0: return [4 /*yield*/, writer.writeRecords(records)];
61 case 1:
62 _a.sent();
63 helper_1.assertFile(filePath, 'Bob,French\nMary,English\n');
64 return [2 /*return*/];
65 }
66 });
67 }); });
68 it('appends records when requested to write to the same file', function () { return __awaiter(void 0, void 0, void 0, function () {
69 return __generator(this, function (_a) {
70 switch (_a.label) {
71 case 0: return [4 /*yield*/, writer.writeRecords([records[0]])];
72 case 1:
73 _a.sent();
74 return [4 /*yield*/, writer.writeRecords([records[1]])];
75 case 2:
76 _a.sent();
77 helper_1.assertFile(filePath, 'Bob,French\nMary,English\n');
78 return [2 /*return*/];
79 }
80 });
81 }); });
82 });
83 describe('When header ids are given with reverse order', function () {
84 var filePath = makeFilePath('column-order');
85 var writer = index_1.createObjectCsvWriter({
86 path: filePath,
87 header: ['lang', 'name']
88 });
89 it('also writes columns with reverse order', function () { return __awaiter(void 0, void 0, void 0, function () {
90 return __generator(this, function (_a) {
91 switch (_a.label) {
92 case 0: return [4 /*yield*/, writer.writeRecords(records)];
93 case 1:
94 _a.sent();
95 helper_1.assertFile(filePath, 'French,Bob\nEnglish,Mary\n');
96 return [2 /*return*/];
97 }
98 });
99 }); });
100 });
101 describe('When field header is given with titles', function () {
102 var filePath = makeFilePath('header');
103 var writer;
104 beforeEach(function () {
105 writer = index_1.createObjectCsvWriter({
106 path: filePath,
107 header: [{ id: 'name', title: 'NAME' }, { id: 'lang', title: 'LANGUAGE' }]
108 });
109 });
110 it('writes a header', function () { return __awaiter(void 0, void 0, void 0, function () {
111 return __generator(this, function (_a) {
112 switch (_a.label) {
113 case 0: return [4 /*yield*/, writer.writeRecords(records)];
114 case 1:
115 _a.sent();
116 helper_1.assertFile(filePath, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
117 return [2 /*return*/];
118 }
119 });
120 }); });
121 it('appends records without headers', function () { return __awaiter(void 0, void 0, void 0, function () {
122 return __generator(this, function (_a) {
123 switch (_a.label) {
124 case 0: return [4 /*yield*/, writer.writeRecords([records[0]])];
125 case 1:
126 _a.sent();
127 return [4 /*yield*/, writer.writeRecords([records[1]])];
128 case 2:
129 _a.sent();
130 helper_1.assertFile(filePath, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
131 return [2 /*return*/];
132 }
133 });
134 }); });
135 });
136 describe('When `append` flag is specified', function () {
137 var filePath = makeFilePath('append');
138 fs_1.writeFileSync(filePath, 'Mike,German\n', 'utf8');
139 var writer = index_1.createObjectCsvWriter({
140 path: filePath,
141 header: ['name', 'lang'],
142 append: true
143 });
144 it('do not overwrite the existing contents and appends records to them', function () { return __awaiter(void 0, void 0, void 0, function () {
145 return __generator(this, function (_a) {
146 switch (_a.label) {
147 case 0: return [4 /*yield*/, writer.writeRecords([records[1]])];
148 case 1:
149 _a.sent();
150 helper_1.assertFile(filePath, 'Mike,German\nMary,English\n');
151 return [2 /*return*/];
152 }
153 });
154 }); });
155 });
156 describe('When encoding is specified', function () {
157 var filePath = makeFilePath('encoding');
158 var writer = index_1.createObjectCsvWriter({
159 path: filePath,
160 header: ['name', 'lang'],
161 encoding: 'utf16le'
162 });
163 it('writes to a file with the specified encoding', function () { return __awaiter(void 0, void 0, void 0, function () {
164 return __generator(this, function (_a) {
165 switch (_a.label) {
166 case 0: return [4 /*yield*/, writer.writeRecords(records)];
167 case 1:
168 _a.sent();
169 helper_1.assertFile(filePath, 'Bob,French\nMary,English\n', 'utf16le');
170 return [2 /*return*/];
171 }
172 });
173 }); });
174 });
175 describe('When semicolon is specified as a field delimiter', function () {
176 var filePath = makeFilePath('field-delimiter');
177 var writer = index_1.createObjectCsvWriter({
178 path: filePath,
179 header: [{ id: 'name', title: 'NAME' }, { id: 'lang', title: 'LANGUAGE' }],
180 fieldDelimiter: ';'
181 });
182 it('uses semicolon instead of comma to separate fields', function () { return __awaiter(void 0, void 0, void 0, function () {
183 return __generator(this, function (_a) {
184 switch (_a.label) {
185 case 0: return [4 /*yield*/, writer.writeRecords(records)];
186 case 1:
187 _a.sent();
188 helper_1.assertFile(filePath, 'NAME;LANGUAGE\nBob;French\nMary;English\n');
189 return [2 /*return*/];
190 }
191 });
192 }); });
193 });
194 describe('When newline is specified', function () {
195 var filePath = makeFilePath('newline');
196 var writer = index_1.createObjectCsvWriter({
197 path: filePath,
198 header: ['name', 'lang'],
199 recordDelimiter: '\r\n'
200 });
201 it('writes to a file with the specified newline character', function () { return __awaiter(void 0, void 0, void 0, function () {
202 return __generator(this, function (_a) {
203 switch (_a.label) {
204 case 0: return [4 /*yield*/, writer.writeRecords(records)];
205 case 1:
206 _a.sent();
207 helper_1.assertFile(filePath, 'Bob,French\r\nMary,English\r\n');
208 return [2 /*return*/];
209 }
210 });
211 }); });
212 });
213 describe('When `alwaysQuote` flag is set', function () {
214 var filePath = makeFilePath('always-quote');
215 var writer = index_1.createObjectCsvWriter({
216 path: filePath,
217 header: [{ id: 'name', title: 'NAME' }, { id: 'lang', title: 'LANGUAGE' }],
218 alwaysQuote: true
219 });
220 it('quotes all fields', function () { return __awaiter(void 0, void 0, void 0, function () {
221 return __generator(this, function (_a) {
222 switch (_a.label) {
223 case 0: return [4 /*yield*/, writer.writeRecords(records)];
224 case 1:
225 _a.sent();
226 helper_1.assertFile(filePath, '"NAME","LANGUAGE"\n"Bob","French"\n"Mary","English"\n');
227 return [2 /*return*/];
228 }
229 });
230 }); });
231 });
232 describe('When `headerIdDelimiter` flag is set', function () {
233 var filePath = makeFilePath('nested');
234 var writer = index_1.createObjectCsvWriter({
235 path: filePath,
236 header: [{ id: 'name', title: 'NAME' }, { id: 'address.country', title: 'COUNTRY' }],
237 headerIdDelimiter: '.'
238 });
239 it('breaks keys into key paths', function () { return __awaiter(void 0, void 0, void 0, function () {
240 return __generator(this, function (_a) {
241 switch (_a.label) {
242 case 0: return [4 /*yield*/, writer.writeRecords(records)];
243 case 1:
244 _a.sent();
245 helper_1.assertFile(filePath, 'NAME,COUNTRY\nBob,France\nMary,\n');
246 return [2 /*return*/];
247 }
248 });
249 }); });
250 });
251});
252//# sourceMappingURL=write-object-records.test.js.map
\No newline at end of file