1 | import {resolveDelimiterChar} from './helper/delimiter';
|
2 | import {createFieldStringifier} from '../lib/field-stringifier';
|
3 | import {strictEqual} from 'assert';
|
4 |
|
5 | describe('DefaultFieldStringifier', () => {
|
6 |
|
7 | describe('When field delimiter is comma', generateTestCases(','));
|
8 |
|
9 | describe('When field delimiter is semicolon', generateTestCases(';'));
|
10 |
|
11 | describe('When all fields needs to be quoted', () => {
|
12 | const stringifier = createFieldStringifier(',', true);
|
13 |
|
14 | it('quotes a field', () => {
|
15 | strictEqual(stringifier.stringify('VALUE'), '"VALUE"');
|
16 | });
|
17 |
|
18 | it('does not quote a field of value undefined', () => {
|
19 | strictEqual(stringifier.stringify(), '');
|
20 | });
|
21 |
|
22 | it('does not quote a field of value null', () => {
|
23 | strictEqual(stringifier.stringify(null), '');
|
24 | });
|
25 |
|
26 | it('does not quote a field of value empty string', () => {
|
27 | strictEqual(stringifier.stringify(''), '');
|
28 | });
|
29 | });
|
30 |
|
31 | function generateTestCases(fieldDelimiter: string) {
|
32 | const delim = resolveDelimiterChar(fieldDelimiter);
|
33 | return () => {
|
34 | const stringifier = createFieldStringifier(fieldDelimiter);
|
35 |
|
36 | it('returns the same string', () => {
|
37 | strictEqual(stringifier.stringify('VALUE'), 'VALUE');
|
38 | });
|
39 |
|
40 | it('preserves the whitespace characters', () => {
|
41 | strictEqual(stringifier.stringify(' VALUE\tA '), ' VALUE\tA ');
|
42 | });
|
43 |
|
44 | it(`wraps a field value with double quotes if the field contains "${delim}"`, () => {
|
45 | strictEqual(stringifier.stringify(`VALUE${delim}A`), `"VALUE${delim}A"`);
|
46 | });
|
47 |
|
48 | it('wraps a field value with double quotes if the field contains newline', () => {
|
49 | strictEqual(stringifier.stringify('VALUE\nA'), '"VALUE\nA"');
|
50 | });
|
51 |
|
52 | it('wraps a field value with double quotes and escape the double quotes if they are used in the field', () => {
|
53 | strictEqual(stringifier.stringify('VALUE"A'), '"VALUE""A"');
|
54 | });
|
55 |
|
56 | it('escapes double quotes even if double quotes are only on the both edges of the field', () => {
|
57 | strictEqual(stringifier.stringify('"VALUE"'), '"""VALUE"""');
|
58 | });
|
59 |
|
60 | it('converts a number into a string', () => {
|
61 | strictEqual(stringifier.stringify(1), '1');
|
62 | });
|
63 |
|
64 | it('converts undefined into an empty string', () => {
|
65 | strictEqual(stringifier.stringify(), '');
|
66 | });
|
67 |
|
68 | it('converts null into an empty string', () => {
|
69 | strictEqual(stringifier.stringify(null), '');
|
70 | });
|
71 |
|
72 | it('converts an object into toString-ed value', () => {
|
73 | const obj = {
|
74 | name: 'OBJECT_NAME',
|
75 | toString: function () { return `Name: ${this.name}`; }
|
76 | };
|
77 | strictEqual(stringifier.stringify(obj), 'Name: OBJECT_NAME');
|
78 | });
|
79 |
|
80 | it(`wraps a toString-ed field value with double quote if the value contains "${delim}"`, () => {
|
81 | const obj = {
|
82 | name: `OBJECT${delim}NAME`,
|
83 | toString: function () { return `Name: ${this.name}`; }
|
84 | };
|
85 | strictEqual(stringifier.stringify(obj), `"Name: OBJECT${delim}NAME"`);
|
86 | });
|
87 |
|
88 | it('escapes double quotes in a toString-ed field value if the value has double quotes', () => {
|
89 | const obj = {
|
90 | name: 'OBJECT_NAME"',
|
91 | toString: function () { return `Name: ${this.name}`; }
|
92 | };
|
93 | strictEqual(stringifier.stringify(obj), '"Name: OBJECT_NAME"""');
|
94 | });
|
95 | };
|
96 | }
|
97 | });
|