This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 167
/
option.cast.coffee
125 lines (112 loc) · 3.31 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
parse = require '../lib'
describe '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,0123,', cast: true, (err, data) ->
data.should.eql [ ['123a', 123, 123, ''] ]
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', {
quoting: false, index: 1, column: 1, empty_lines: 0, lines: 1,
header: false, invalid_field_length: 0, records: 0
} ]
[ '2050-11-27T05:00:00.000Z', {
quoting: false, index: 1, column: 1, empty_lines: 0, lines: 2,
header: false, invalid_field_length: 0, records: 1
} ]
] unless err
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 '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, context) ->
if context.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, context) ->
context.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 'catch error', (next) ->
parse """
1,2,3
4,5,6
""",
cast: (value, context) ->
if value is '6' then throw Error 'Catchme'
value
, (err, records) ->
err.message.should.eql 'Catchme'
next()