/
csv2json.test.ts
257 lines (219 loc) · 8.58 KB
/
csv2json.test.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { describe, expect, test } from 'vitest'
import { csv2json } from './csv2json'
describe('csv2json', () => {
test('should convert all data types', () => {
expect(
csv2json(
'string,empty,number,true,false,object,array,null,undefined\r\n' +
'hi,"",42,true,false,"{""key"":""value""}","[""item1""]",,\r\n'
)
).toEqual([
{
string: 'hi',
empty: '',
number: 42,
true: true,
false: false,
object: { key: 'value' },
array: ['item1'],
null: null,
undefined: undefined
}
])
})
test('should parse a CSV with header', () => {
const csv = 'id,name\r\n1,Joe\r\n2,Sarah\r\n'
const expected = [
{ id: 1, name: 'Joe' },
{ id: 2, name: 'Sarah' }
]
expect(csv2json(csv)).toEqual(expected)
expect(csv2json(csv, { header: true })).toEqual(expected)
})
test('should parse a CSV without header', () => {
expect(csv2json('1,Joe\r\n2,Sarah\r\n', { header: false })).toEqual([
{ 'Field 0': 1, 'Field 1': 'Joe' },
{ 'Field 0': 2, 'Field 1': 'Sarah' }
])
})
test('should multiple subsequent escaped quotes', () => {
expect(csv2json('text\r\n"""""hello""""""""world"\r\n')).toEqual([{ text: '""hello""""world' }])
})
test('should convert a list with a number in the name', () => {
expect(csv2json('1a\r\nhello\r\n')).toEqual([{ '1a': 'hello' }])
})
test('should parse numbers and strings', () => {
expect(csv2json('value\r\n123\r\n')).toEqual([{ value: 123 }])
expect(csv2json('value\r\n123.45e6\r\n')).toEqual([{ value: 123.45e6 }])
expect(csv2json('value\r\n-123\r\n')).toEqual([{ value: -123 }])
expect(csv2json('value\r\n123a\r\n')).toEqual([{ value: '123a' }])
expect(csv2json('value\r\n 123\r\n')).toEqual([{ value: ' 123' }])
expect(csv2json('value\r\n123 \r\n')).toEqual([{ value: 123 }])
expect(csv2json('value\r\na 123\r\n')).toEqual([{ value: 'a 123' }])
expect(csv2json('value\r\n123 a\r\n')).toEqual([{ value: '123 a' }])
expect(csv2json('value\r\n-\r\n')).toEqual([{ value: '-' }])
expect(csv2json('value\r\n/\r\n')).toEqual([{ value: '/' }])
expect(csv2json('value\r\n.\r\n')).toEqual([{ value: '.' }])
expect(csv2json('value\r\n.2\r\n')).toEqual([{ value: 0.2 }])
})
test("should handle strings that look like a number or array or object but aren't", () => {
expect(csv2json('value\r\n2abc\r\n')).toEqual([{ value: '2abc' }])
expect(csv2json('value\r\n[abc\r\n')).toEqual([{ value: '[abc' }])
expect(csv2json('value\r\n{abc\r\n')).toEqual([{ value: '{abc' }])
})
test('should convert a list with an empty field name', () => {
// note: this does not result in the same output as the original,
// but I think it is ok to parse everything into an object instead of catering for these edge cases
expect(csv2json('"[""""]"\r\n1\r\n2\r\n3\r\n')).toEqual([{ '': 1 }, { '': 2 }, { '': 3 }])
})
test('should convert a list with values instead of objects', () => {
expect(csv2json('""\r\n1\r\n2\r\n3\r\n')).toEqual([{ '': 1 }, { '': 2 }, { '': 3 }])
})
test('should convert an array with arrays', () => {
// note: this does not result in the same output as the original,
// but I think it is ok to parse everything into an object instead of catering for these edge cases
expect(csv2json('0,1\r\n1,2\r\n3,4\r\n')).toEqual([
{ 0: 1, 1: 2 },
{ 0: 3, 1: 4 }
])
})
test('should parse a custom delimiter', () => {
expect(csv2json('a;b\r\n"containing;delimiter";text\r\n', { delimiter: ';' })).toEqual([
{ a: 'containing;delimiter', b: 'text' }
])
})
test('should parse a custom eol', () => {
expect(csv2json('id,name\n1,Joe\n2,Sarah\n')).toEqual([
{ id: 1, name: 'Joe' },
{ id: 2, name: 'Sarah' }
])
})
test('should parse custom fields', () => {
expect(
csv2json('id,name\r\n1,Joe\r\n2,Sarah\r\n', {
fields: [{ name: 'name', setValue: (item, value) => (item.name = value) }]
})
).toEqual([{ name: 'Joe' }, { name: 'Sarah' }])
})
test('should parse custom fields, fields being a function', () => {
expect(
csv2json('id,name\r\n1,Joe\r\n2,Sarah\r\n', {
fields: () => [{ name: 'name', setValue: (item, value) => (item.name = value) }]
})
).toEqual([{ name: 'Joe' }, { name: 'Sarah' }])
})
test('should parse custom fields by index', () => {
expect(
csv2json('id,name\r\n1,Joe\r\n2,Sarah\r\n', {
fields: [{ index: 1, setValue: (item, value) => (item.name = value) }]
})
).toEqual([{ name: 'Joe' }, { name: 'Sarah' }])
})
test('should throw an error when defining the same field twice', () => {
expect(() =>
csv2json('id,name\r\n1,Joe\r\n2,Sarah\r\n', {
fields: [
{ index: 1, setValue: (item, value) => (item.name = value) },
{ name: 'name', setValue: (item, value) => (item.name = value) }
]
})
).toThrow('Duplicate field for index 1')
})
test('should throw an error when a field is not found', () => {
expect(() => {
csv2json('id,name\n42,Joe', {
fields: [{ name: 'foo', setValue: (record, value) => (record.foo = value) }]
})
}).toThrow('Field "foo" not found in the csv data')
})
test('should unescape control character "', () => {
expect(csv2json('name\r\n"""Big"" Joe"\r\n')).toEqual([{ name: '"Big" Joe' }])
})
test('should unescape control character ,', () => {
expect(csv2json('name\r\n"Joe,Jones"\r\n')).toEqual([{ name: 'Joe,Jones' }])
})
test('should unescape control character \\n', () => {
expect(csv2json('name\r\n"Joe\nJones"\r\n')).toEqual([{ name: 'Joe\nJones' }])
})
test('should unescape control character \\r', () => {
expect(csv2json('name\r\n"Joe\rJones"\r\n')).toEqual([{ name: 'Joe\rJones' }])
})
test('should unescape control character " in the header', () => {
expect(csv2json('name\r\n"""Big"" Joe"\r\n')).toEqual([{ name: '"Big" Joe' }])
})
test('should unescape control character , in the header', () => {
expect(csv2json('name\r\n"Joe,Jones"\r\n')).toEqual([{ name: 'Joe,Jones' }])
})
test('should unescape control character \\n in the header', () => {
expect(csv2json('name\r\n"Joe\nJones"\r\n')).toEqual([{ name: 'Joe\nJones' }])
})
test('should unescape control character \\r in the header', () => {
expect(csv2json('name\r\n"Joe\rJones"\r\n')).toEqual([{ name: 'Joe\rJones' }])
})
test('should parse nested fields', () => {
const csv =
'name,details.address.city,details.location[0],details.location[1]\r\n' +
'Joe,Rotterdam,51.9280712,4.4207888\r\n'
const jsonNested = [
{
name: 'Joe',
details: {
address: { city: 'Rotterdam' },
location: [51.9280712, 4.4207888]
}
}
]
const jsonFlat = [
{
name: 'Joe',
'details.address.city': 'Rotterdam',
'details.location[0]': 51.9280712,
'details.location[1]': 4.4207888
}
]
expect(csv2json(csv)).toEqual(jsonNested)
expect(csv2json(csv, { nested: true })).toEqual(jsonNested)
expect(csv2json(csv, { nested: false })).toEqual(jsonFlat)
})
test('should parse stringified JSON', () => {
const csv =
'name,details\r\n' +
'Joe,"{""address"":{""city"":""Rotterdam""},""location"":[51.9280712,4.4207888]}"\r\n'
expect(csv2json(csv)).toEqual([
{
name: 'Joe',
details: {
address: { city: 'Rotterdam' },
location: [51.9280712, 4.4207888]
}
}
])
})
test('should parser nested fields containing the key separator', () => {
expect(csv2json('"nested[""field.name""]"\r\n42\r\n')).toEqual([
{ nested: { 'field.name': 42 } }
])
})
test('should parser nested fields with dot notation for indexes', () => {
expect(csv2json('"nested.geo.0"\r\n42\r\n')).toEqual([{ nested: { geo: [42] } }])
})
test('should parser nested fields containing the key separator and a control character', () => {
expect(csv2json('"nested[""field.,name""]"\r\n42\r\n')).toEqual([
{ nested: { 'field.,name': 42 } }
])
})
test('should parse empty data', () => {
expect(csv2json('')).toEqual([])
expect(csv2json('\r\n')).toEqual([])
})
test('should throw an error when an end quote is missing', () => {
expect(() => {
csv2json('"text')
}).toThrow('Unexpected end: end quote " missing')
})
test('should throw an error when passing an invalid delimiter', () => {
expect(() => {
csv2json('"text', { delimiter: 'foo' })
}).toThrow('Invalid delimiter: must be a single character but is "foo"')
})
})