This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
/
option.cast.coffee
268 lines (242 loc) · 6.99 KB
/
option.cast.coffee
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
258
259
260
261
262
263
264
265
266
267
268
parse = require '../lib'
assert_error = require './api.assert_error'
describe 'Option `cast`', ->
it 'validate', ->
(->
parse cast: 'ohno', ( -> )
).should.throw
message: 'Invalid option cast: cast must be true or a function, got "ohno"'
code: 'CSV_INVALID_OPTION_CAST'
describe 'boolean true', ->
it 'all columns', (next) ->
parse '1,2,3', cast: true, (err, data) ->
data.should.eql [ [1, 2, 3] ]
next()
it 'convert numbers', (next) ->
data = []
parser = parse({ cast: true })
parser.write """
20322051544,1979,8.8017226E7,8e2,ABC,45,2000-01-01
28392898392,1974,8.8392926e7,8E2,DEF,23,2050-11-27
"""
parser.on 'readable', ->
while d = parser.read()
data.push d
parser.on 'error', (err) ->
next err
parser.on 'end', ->
data.should.eql [
[20322051544, 1979, 8.8017226e7, 800, 'ABC', 45, '2000-01-01']
[28392898392, 1974, 8.8392926e7, 800, 'DEF', 23, '2050-11-27']
]
next()
parser.end()
it 'ints', (next) ->
parse '123a,123,+123,-123,0123,+0123,-0123,', cast: true, (err, data) ->
data.should.eql [ ['123a', 123, 123, -123, 123, 123, -123, ''] ]
next()
it 'ints isnt exposed to DOS vulnerabilities, npm security issue 69742', (next) ->
data = Array.from( length: 3000000 ).map( (x) -> '1' ).join('') + '!'
parse data, cast: true, (err, data) ->
data[0][0].length.should.eql 3000001
next()
it 'float', (next) ->
parse '123a,1.23,0.123,01.23,.123,123.', cast: true, (err, data) ->
data.should.eql [ ['123a', 1.23, 0.123, 1.23, 0.123, 123] ]
next()
describe 'function', ->
it 'custom function', (next) ->
parse """
2000-01-01,date1
2050-11-27,date2
""",
cast: (value, context) ->
if context.index is 0
then "#{value}T05:00:00.000Z"
else {...context}
, (err, records) ->
records.should.eql [
[ '2000-01-01T05:00:00.000Z', {
column: 1, empty_lines: 0, error: undefined, header: false, index: 1,
invalid_field_length: 0, lines: 1, quoting: false, records: 0
} ]
[ '2050-11-27T05:00:00.000Z', {
column: 1, empty_lines: 0, error: undefined, header: false, index: 1,
invalid_field_length: 0, lines: 2, quoting: false, records: 1
} ]
] unless err
next err
it 'column is a string', (next) ->
parse """
1,2
3,4,5
6
""",
columns: ['a', 'b']
relax_column_count: true
cast: (value, {header, column}) ->
typeof column
, (err, records) ->
records.should.eql [
{a: 'string', b: 'string'}
{a: 'string', b: 'string'}
{a: 'string'}
] unless err
next err
it 'dont call cast on unreferenced columns', (next) ->
# It doesn't make sense to cast value which cannot later be assigned
# to a column name in the returned object
parse """
1,2
3,4,5,6
7
""",
columns: ['a', 'b']
relax_column_count: true
cast: (value, {column}) ->
throw Error 'Oh no' if value > 4 and value < 7
, (err, records) ->
next err
it 'custom function with quoting context', (next) ->
parse """
"2000-01-01",date1
2025-12-31,"date2"
2050-11-27,"date3"
""",
cast: (value, {quoting}) ->
quoting
, (err, records) ->
records.should.eql [
[ true, false ]
[ false, true ]
[ false, true ]
] unless err
next err
it 'return undefined', ->
it 'accept all values', (next) ->
parse """
1,2,3
4,5,6
""",
max_record_size: 10
cast: (value, {index}) ->
switch index
when 0
undefined
when 1
false
when 2
null
, (err, records) ->
records.shift().should.eql [undefined, false, null]
next err
describe 'columns', ->
it 'header is true on first line when columns is true', (next) ->
parse """
a,b,c
1,2,3
4,5,6
""",
columns: true
cast: (value, {header}) ->
if header then value else parseInt value
, (err, records) ->
records.should.eql [
{a: 1, b: 2, c: 3}
{a: 4, b: 5, c: 6}
] unless err
next err
it 'header is false when columns is an object', (next) ->
parse """
1,2,3
4,5,6
""",
columns: ['a', 'b', 'c']
cast: (value, {header}) ->
header.should.be.false()
parseInt value
, (err, records) ->
records.should.eql [
{a: 1, b: 2, c: 3}
{a: 4, b: 5, c: 6}
] unless err
next err
it 'dont count header line', (next) ->
parse """
a,b,c
1,2,3
4,5,6
""",
columns: true
cast: (value) ->
value
, (err, records) ->
next err
it 'filter columns if value is undefined', (next) ->
parse """
a,b,c,d
1,2,3,4
5,6,7,8
""",
columns: true,
cast: (value, context) ->
switch context.index
when 0 then if context.header then value else Number(value)
when 2 then value
else undefined
, (err, data) ->
data.should.eql [
'a': 1
'c': '3'
,
'a': 5
'c': '7'
] unless err
next err
it 'throw error if header is invalid', (next) ->
parse """
a,b,c,d
1,2,3,4
5,6,7,8
""",
columns: true,
cast: (value, context) ->
switch context.index
when 0 then if context.header then 'string' else value
when 1 then if context.header then undefined else value
when 2 then if context.header then null else value
when 3 then if context.header then 1234 else value
, (err, data) ->
assert_error err,
message: 'Invalid column definition: expect a string or a literal object, got 1234 at position 3'
code: 'CSV_INVALID_COLUMN_DEFINITION'
next()
describe 'columns_duplicates_to_array', ->
it 'leading zeros are maintained when columns_duplicates_to_array is true', (next) ->
parse """
FIELD_1,FIELD_1,FIELD_1
0,2,3
0,0,4
""",
cast: true
columns: true
columns_duplicates_to_array: true
, (err, data) ->
data.should.eql [
'FIELD_1': [0, 2, 3]
,
'FIELD_1': [0, 0, 4]
] unless err
next err
describe 'error', ->
it 'catch error', (next) ->
parse """
1,2,3
4,5,6
""",
cast: (value) ->
if value is '6' then throw Error 'Catchme'
value
, (err, records) ->
err.message.should.eql 'Catchme'
next()