-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
format.test.js
144 lines (132 loc) · 3.87 KB
/
format.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import test from 'node:test'
import { equal } from 'node:assert'
import { format, formatArray, formatObject, formatField } from './format.js'
const defaultOptions = {
header: true,
escapeChar: '"',
quoteChar: '"',
delimiterChar: ',',
newlineChar: '/n',
enableReturn: true,
enqueue: () => {}
}
// *** format() *** //
test('Should format array of objects w/ header == true', async (t) => {
const field = format([{ a: '1', b: '2' }], {
...defaultOptions,
header: true
})
equal(field, 'a,b/n1,2/n')
})
test('Should format array of objects w/ header == [...]', async (t) => {
const field = format([{ a: '1', b: '2', c: '3' }], {
...defaultOptions,
header: ['b', 'a']
})
equal(field, 'b,a/n2,1/n')
})
test('Should format array of objects w/ header === false', async (t) => {
const field = format([{ a: '1', b: '2' }], {
...defaultOptions,
header: false
})
equal(field, '1,2/n')
})
test('Should format array of arrays w/ header == [...]', async (t) => {
const field = format([['1', '2']], {
...defaultOptions,
header: ['a', 'b']
})
equal(field, 'a,b/n1,2/n')
})
test('Should format array of arrays w/ header === false', async (t) => {
const field = format([['1', '2']], {
...defaultOptions,
header: false
})
equal(field, '1,2/n')
})
// *** formatHeader() *** //
test('Should format header', async (t) => {
const field = formatArray(['b', 'a'], {
...defaultOptions,
header: ['b', 'a']
})
equal(field, 'b,a/n')
})
// *** formatArray() *** //
test('Should format row array', async (t) => {
const field = formatArray(['1', '2'], { ...defaultOptions, header: false })
equal(field, '1,2/n')
})
// *** formatObject() *** //
test('Should format row object', async (t) => {
const field = formatObject(
{ a: '1', b: '2' },
{ ...defaultOptions, header: ['b', 'a'] }
)
equal(field, '2,1/n')
})
test('Should format row object w/ quotes', async (t) => {
const field = formatObject(
{ a: '1', b: '2' },
{ ...defaultOptions, header: ['b', 'a'], quoteColumn: [true, true] }
)
equal(field, '"2","1"/n')
})
test('Should format row object w/o quotes', async (t) => {
const field = formatObject(
{ a: '1', b: '2' },
{ ...defaultOptions, header: ['b', 'a'], quoteColumn: [false, false] }
)
equal(field, '2,1/n')
})
// *** formatField() *** //
test('Should format undefined', async (t) => {
const field = formatField(undefined, undefined, defaultOptions)
equal(field, '')
})
test('Should format null', async (t) => {
const field = formatField(null, undefined, defaultOptions)
equal(field, '')
})
test('Should format empty string', async (t) => {
const field = formatField('', undefined, defaultOptions)
equal(field, '')
})
test('Should format date', async (t) => {
const field = formatField(
new Date('2000-01-01T00:00:00.000Z'),
undefined,
defaultOptions
)
equal(field, '2000-01-01T00:00:00.000Z')
})
test('Should format number', async (t) => {
const field = formatField(0, undefined, defaultOptions)
equal(field, '0')
})
test('Should format string', async (t) => {
const field = formatField('column', undefined, defaultOptions)
equal(field, 'column')
})
test('Should format string with delimiter', async (t) => {
const field = formatField('_"_', undefined, defaultOptions)
equal(field, '"_""_"')
})
test('Should format string with leading space', async (t) => {
const field = formatField(' space', undefined, defaultOptions)
equal(field, '" space"')
})
test('Should format string with trailing space', async (t) => {
const field = formatField('space ', undefined, defaultOptions)
equal(field, '"space "')
})
test('Should format w/ quotes', async (t) => {
const field = formatField('column', true, defaultOptions)
equal(field, '"column"')
})
test('Should format w/o quotes', async (t) => {
const field = formatField('column', false, defaultOptions)
equal(field, 'column')
})