UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var delimiter_1 = require("./helper/delimiter");
4var field_stringifier_1 = require("../lib/field-stringifier");
5var assert_1 = require("assert");
6describe('DefaultFieldStringifier', function () {
7 describe('When field delimiter is comma', generateTestCases(','));
8 describe('When field delimiter is semicolon', generateTestCases(';'));
9 describe('When all fields needs to be quoted', function () {
10 var stringifier = field_stringifier_1.createFieldStringifier(',', true);
11 it('quotes a field', function () {
12 assert_1.strictEqual(stringifier.stringify('VALUE'), '"VALUE"');
13 });
14 it('does not quote a field of value undefined', function () {
15 assert_1.strictEqual(stringifier.stringify(), '');
16 });
17 it('does not quote a field of value null', function () {
18 assert_1.strictEqual(stringifier.stringify(null), '');
19 });
20 it('does not quote a field of value empty string', function () {
21 assert_1.strictEqual(stringifier.stringify(''), '');
22 });
23 });
24 function generateTestCases(fieldDelimiter) {
25 var delim = delimiter_1.resolveDelimiterChar(fieldDelimiter);
26 return function () {
27 var stringifier = field_stringifier_1.createFieldStringifier(fieldDelimiter);
28 it('returns the same string', function () {
29 assert_1.strictEqual(stringifier.stringify('VALUE'), 'VALUE');
30 });
31 it('preserves the whitespace characters', function () {
32 assert_1.strictEqual(stringifier.stringify(' VALUE\tA '), ' VALUE\tA ');
33 });
34 it("wraps a field value with double quotes if the field contains \"" + delim + "\"", function () {
35 assert_1.strictEqual(stringifier.stringify("VALUE" + delim + "A"), "\"VALUE" + delim + "A\"");
36 });
37 it('wraps a field value with double quotes if the field contains newline', function () {
38 assert_1.strictEqual(stringifier.stringify('VALUE\nA'), '"VALUE\nA"');
39 });
40 it('wraps a field value with double quotes and escape the double quotes if they are used in the field', function () {
41 assert_1.strictEqual(stringifier.stringify('VALUE"A'), '"VALUE""A"');
42 });
43 it('escapes double quotes even if double quotes are only on the both edges of the field', function () {
44 assert_1.strictEqual(stringifier.stringify('"VALUE"'), '"""VALUE"""');
45 });
46 it('converts a number into a string', function () {
47 assert_1.strictEqual(stringifier.stringify(1), '1');
48 });
49 it('converts undefined into an empty string', function () {
50 assert_1.strictEqual(stringifier.stringify(), '');
51 });
52 it('converts null into an empty string', function () {
53 assert_1.strictEqual(stringifier.stringify(null), '');
54 });
55 it('converts an object into toString-ed value', function () {
56 var obj = {
57 name: 'OBJECT_NAME',
58 toString: function () { return "Name: " + this.name; }
59 };
60 assert_1.strictEqual(stringifier.stringify(obj), 'Name: OBJECT_NAME');
61 });
62 it("wraps a toString-ed field value with double quote if the value contains \"" + delim + "\"", function () {
63 var obj = {
64 name: "OBJECT" + delim + "NAME",
65 toString: function () { return "Name: " + this.name; }
66 };
67 assert_1.strictEqual(stringifier.stringify(obj), "\"Name: OBJECT" + delim + "NAME\"");
68 });
69 it('escapes double quotes in a toString-ed field value if the value has double quotes', function () {
70 var obj = {
71 name: 'OBJECT_NAME"',
72 toString: function () { return "Name: " + this.name; }
73 };
74 assert_1.strictEqual(stringifier.stringify(obj), '"Name: OBJECT_NAME"""');
75 });
76 };
77 }
78});
79//# sourceMappingURL=field-stringifier.test.js.map
\No newline at end of file