UNPKG

10.1 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 array records into CSV', function () {
43 var makeFilePath = function (id) { return helper_1.testFilePath("array-" + id); };
44 var records = [
45 ['Bob', 'French'],
46 ['Mary', 'English']
47 ];
48 describe('When only path is specified', function () {
49 var filePath = makeFilePath('minimum');
50 var writer;
51 beforeEach(function () {
52 writer = index_1.createArrayCsvWriter({ path: filePath });
53 });
54 it('writes records to a new file', function () { return __awaiter(void 0, void 0, void 0, function () {
55 return __generator(this, function (_a) {
56 switch (_a.label) {
57 case 0: return [4 /*yield*/, writer.writeRecords(records)];
58 case 1:
59 _a.sent();
60 helper_1.assertFile(filePath, 'Bob,French\nMary,English\n');
61 return [2 /*return*/];
62 }
63 });
64 }); });
65 it('appends records when requested to write to the same file', function () { return __awaiter(void 0, void 0, void 0, function () {
66 return __generator(this, function (_a) {
67 switch (_a.label) {
68 case 0: return [4 /*yield*/, writer.writeRecords([records[0]])];
69 case 1:
70 _a.sent();
71 return [4 /*yield*/, writer.writeRecords([records[1]])];
72 case 2:
73 _a.sent();
74 helper_1.assertFile(filePath, 'Bob,French\nMary,English\n');
75 return [2 /*return*/];
76 }
77 });
78 }); });
79 });
80 describe('When field header is given', function () {
81 var filePath = makeFilePath('header');
82 var writer;
83 beforeEach(function () {
84 writer = index_1.createArrayCsvWriter({
85 path: filePath,
86 header: ['NAME', 'LANGUAGE']
87 });
88 });
89 it('writes a header', 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, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
96 return [2 /*return*/];
97 }
98 });
99 }); });
100 it('appends records without headers', function () { return __awaiter(void 0, void 0, void 0, function () {
101 return __generator(this, function (_a) {
102 switch (_a.label) {
103 case 0: return [4 /*yield*/, writer.writeRecords([records[0]])];
104 case 1:
105 _a.sent();
106 return [4 /*yield*/, writer.writeRecords([records[1]])];
107 case 2:
108 _a.sent();
109 helper_1.assertFile(filePath, 'NAME,LANGUAGE\nBob,French\nMary,English\n');
110 return [2 /*return*/];
111 }
112 });
113 }); });
114 });
115 describe('When `append` flag is specified', function () {
116 var filePath = makeFilePath('append');
117 fs_1.writeFileSync(filePath, 'Mike,German\n', 'utf8');
118 var writer = index_1.createArrayCsvWriter({
119 path: filePath,
120 append: true
121 });
122 it('do not overwrite the existing contents and appends records to them', function () { return __awaiter(void 0, void 0, void 0, function () {
123 return __generator(this, function (_a) {
124 switch (_a.label) {
125 case 0: return [4 /*yield*/, writer.writeRecords([records[1]])];
126 case 1:
127 _a.sent();
128 helper_1.assertFile(filePath, 'Mike,German\nMary,English\n');
129 return [2 /*return*/];
130 }
131 });
132 }); });
133 });
134 describe('When encoding is specified', function () {
135 var filePath = makeFilePath('encoding');
136 var writer = index_1.createArrayCsvWriter({
137 path: filePath,
138 encoding: 'utf16le'
139 });
140 it('writes to a file with the specified encoding', function () { return __awaiter(void 0, void 0, void 0, function () {
141 return __generator(this, function (_a) {
142 switch (_a.label) {
143 case 0: return [4 /*yield*/, writer.writeRecords(records)];
144 case 1:
145 _a.sent();
146 helper_1.assertFile(filePath, 'Bob,French\nMary,English\n', 'utf16le');
147 return [2 /*return*/];
148 }
149 });
150 }); });
151 });
152 describe('When semicolon is specified as a field delimiter', function () {
153 var filePath = makeFilePath('field-delimiter');
154 var writer = index_1.createArrayCsvWriter({
155 path: filePath,
156 header: ['NAME', 'LANGUAGE'],
157 fieldDelimiter: ';'
158 });
159 it('uses semicolon instead of comma to separate fields', function () { return __awaiter(void 0, void 0, void 0, function () {
160 return __generator(this, function (_a) {
161 switch (_a.label) {
162 case 0: return [4 /*yield*/, writer.writeRecords(records)];
163 case 1:
164 _a.sent();
165 helper_1.assertFile(filePath, 'NAME;LANGUAGE\nBob;French\nMary;English\n');
166 return [2 /*return*/];
167 }
168 });
169 }); });
170 });
171 describe('When newline is specified', function () {
172 var filePath = makeFilePath('newline');
173 var writer = index_1.createArrayCsvWriter({
174 path: filePath,
175 recordDelimiter: '\r\n'
176 });
177 it('writes to a file with the specified newline character', function () { return __awaiter(void 0, void 0, void 0, function () {
178 return __generator(this, function (_a) {
179 switch (_a.label) {
180 case 0: return [4 /*yield*/, writer.writeRecords(records)];
181 case 1:
182 _a.sent();
183 helper_1.assertFile(filePath, 'Bob,French\r\nMary,English\r\n');
184 return [2 /*return*/];
185 }
186 });
187 }); });
188 });
189 describe('When `alwaysQuote` flag is set', function () {
190 var filePath = makeFilePath('always-quote');
191 var writer = index_1.createArrayCsvWriter({
192 path: filePath,
193 header: ['NAME', 'LANGUAGE'],
194 alwaysQuote: true
195 });
196 it('quotes all fields', function () { return __awaiter(void 0, void 0, void 0, function () {
197 return __generator(this, function (_a) {
198 switch (_a.label) {
199 case 0: return [4 /*yield*/, writer.writeRecords(records)];
200 case 1:
201 _a.sent();
202 helper_1.assertFile(filePath, '"NAME","LANGUAGE"\n"Bob","French"\n"Mary","English"\n');
203 return [2 /*return*/];
204 }
205 });
206 }); });
207 });
208});
209//# sourceMappingURL=write-array-records.test.js.map
\No newline at end of file