UNPKG

7.83 kBJavaScriptView Raw
1"use strict";
2var __generator = (this && this.__generator) || function (thisArg, body) {
3 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
5 function verb(n) { return function (v) { return step([n, v]); }; }
6 function step(op) {
7 if (f) throw new TypeError("Generator is already executing.");
8 while (_) try {
9 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;
10 if (y = 0, t) op = [op[0] & 2, t.value];
11 switch (op[0]) {
12 case 0: case 1: t = op; break;
13 case 4: _.label++; return { value: op[1], done: false };
14 case 5: _.label++; y = op[1]; op = [0]; continue;
15 case 7: op = _.ops.pop(); _.trys.pop(); continue;
16 default:
17 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
18 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
19 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
20 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
21 if (t[2]) _.ops.pop();
22 _.trys.pop(); continue;
23 }
24 op = body.call(thisArg, _);
25 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
27 }
28};
29Object.defineProperty(exports, "__esModule", { value: true });
30var delimiter_1 = require("../helper/delimiter");
31var index_1 = require("../../index");
32var assert_1 = require("assert");
33describe('ObjectCsvStringifier', function () {
34 var records = [
35 { FIELD_A: 'VALUE_A1', FIELD_B: 'VALUE_B1' },
36 { FIELD_A: 'VALUE_A2', FIELD_B: 'VALUE_B2', OTHERS: { FIELD_C: 'VALUE_C2' } }
37 ];
38 describe('When field delimiter is comma', generateTestCases());
39 describe('When field delimiter is semicolon', generateTestCases(';'));
40 describe('When field delimiter is neither comma nor semicolon', function () {
41 it('throws an exception', function () {
42 assert_1.throws(function () {
43 index_1.createObjectCsvStringifier({
44 header: ['FIELD_A', 'FIELD_B'],
45 fieldDelimiter: '/'
46 });
47 });
48 });
49 });
50 describe('When record delimiter is neither LF nor CR+LF', function () {
51 it('throws an exception', function () {
52 assert_1.throws(function () {
53 index_1.createObjectCsvStringifier({
54 header: ['FIELD_A', 'FIELD_B'],
55 recordDelimiter: '\r'
56 });
57 });
58 });
59 });
60 describe('When records input is an iterable other than an array', function () {
61 var stringifier = index_1.createObjectCsvStringifier({
62 header: ['FIELD_A', 'FIELD_B']
63 });
64 function recordGenerator() {
65 return __generator(this, function (_a) {
66 switch (_a.label) {
67 case 0: return [4 /*yield*/, records[0]];
68 case 1:
69 _a.sent();
70 return [4 /*yield*/, records[1]];
71 case 2:
72 _a.sent();
73 return [2 /*return*/];
74 }
75 });
76 }
77 it('converts the records into CSV', function () {
78 assert_1.strictEqual(stringifier.stringifyRecords(recordGenerator()), 'VALUE_A1,VALUE_B1\nVALUE_A2,VALUE_B2\n');
79 });
80 });
81 describe('When `alwaysQuote` flag is set', function () {
82 var stringifier = index_1.createObjectCsvStringifier({
83 header: [
84 { id: 'FIELD_A', title: 'TITLE_A' },
85 { id: 'FIELD_B', title: 'TITLE_B' }
86 ],
87 alwaysQuote: true
88 });
89 it('quotes all header fields', function () {
90 assert_1.strictEqual(stringifier.getHeaderString(), '"TITLE_A","TITLE_B"\n');
91 });
92 it('quotes all data fields', function () {
93 assert_1.strictEqual(stringifier.stringifyRecords(records), '"VALUE_A1","VALUE_B1"\n"VALUE_A2","VALUE_B2"\n');
94 });
95 });
96 describe('When `headerIdDelimiter` is set', function () {
97 var stringifier = index_1.createObjectCsvStringifier({
98 header: [
99 { id: 'FIELD_A', title: 'TITLE_A' },
100 { id: 'OTHERS/FIELD_C', title: 'TITLE_C' }
101 ],
102 headerIdDelimiter: '/'
103 });
104 it('uses the title as is', function () {
105 assert_1.strictEqual(stringifier.getHeaderString(), 'TITLE_A,TITLE_C\n');
106 });
107 it('picks up a value in nested objects', function () {
108 assert_1.strictEqual(stringifier.stringifyRecords(records), 'VALUE_A1,\nVALUE_A2,VALUE_C2\n');
109 });
110 });
111 function generateTestCases(fieldDelimiter) {
112 var delim = delimiter_1.resolveDelimiterChar(fieldDelimiter);
113 return function () {
114 describe('header is specified with title', function () {
115 var stringifier = index_1.createObjectCsvStringifier({
116 header: [
117 { id: 'FIELD_A', title: 'TITLE_A' },
118 { id: 'FIELD_B', title: 'TITLE_B' }
119 ],
120 fieldDelimiter: fieldDelimiter
121 });
122 it("returns a header line with field separated by \"" + delim + "\"", function () {
123 assert_1.strictEqual(stringifier.getHeaderString(), "TITLE_A" + delim + "TITLE_B\n");
124 });
125 it("converts given data records into CSV lines with field separated by \"" + delim + "\"", function () {
126 assert_1.strictEqual(stringifier.stringifyRecords(records), "VALUE_A1" + delim + "VALUE_B1\nVALUE_A2" + delim + "VALUE_B2\n");
127 });
128 });
129 describe('header is specified without title', function () {
130 var stringifier = index_1.createObjectCsvStringifier({
131 header: ['FIELD_A', 'FIELD_B'],
132 fieldDelimiter: fieldDelimiter
133 });
134 it('returns null for header line', function () {
135 assert_1.strictEqual(stringifier.getHeaderString(), null);
136 });
137 it("converts given data records into CSV lines with field separated by \"" + delim + "\"", function () {
138 assert_1.strictEqual(stringifier.stringifyRecords(records), "VALUE_A1" + delim + "VALUE_B1\nVALUE_A2" + delim + "VALUE_B2\n");
139 });
140 });
141 describe('header columns are given with reverse order', function () {
142 var stringifier = index_1.createObjectCsvStringifier({
143 header: [
144 { id: 'FIELD_B', title: 'TITLE_B' },
145 { id: 'FIELD_A', title: 'TITLE_A' }
146 ],
147 fieldDelimiter: fieldDelimiter
148 });
149 it("layouts fields with the order of headers given with field separated by \"" + delim + "\"", function () {
150 assert_1.strictEqual(stringifier.stringifyRecords(records), "VALUE_B1" + delim + "VALUE_A1\nVALUE_B2" + delim + "VALUE_A2\n");
151 });
152 });
153 };
154 }
155});
156//# sourceMappingURL=object.test.js.map
\No newline at end of file