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
/
options.quote.coffee
144 lines (121 loc) · 3.81 KB
/
options.quote.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
fs = require 'fs'
parse = require '../src'
describe 'options quote', ->
it 'with default', (next) ->
parse """
abc,"123",def,"456"
hij,klm,"789",nop
""", (err, data) ->
return next err if err
data.should.eql [
[ 'abc','123','def','456' ]
[ 'hij','klm','789','nop' ]
]
next()
it 'with fields containing delimiters', (next) ->
parse """
20322051544,",1979.0,8.8017226E7,ABC,45,2000-01-01",1,2,3,4
28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
"28392898392,1974.0","8.8392926E7","DEF,23,2050-11-27,",4,5,6
""", (err, data) ->
return next err if err
data.should.eql [
[ '20322051544',',1979.0,8.8017226E7,ABC,45,2000-01-01','1','2','3','4' ]
[ '28392898392','1974.0','8.8392926E7','DEF','23','2050-11-27' ]
[ '28392898392,1974.0','8.8392926E7','DEF,23,2050-11-27,','4','5','6' ]
]
next()
it 'empty value', (next) ->
parse """
20322051544,"",8.8017226E7,45,""
"",1974,8.8392926E7,"",""
""", (err, data) ->
return next err if err
data.should.eql [
[ '20322051544','','8.8017226E7','45','' ]
[ '','1974','8.8392926E7','','' ]
]
next()
it 'values containing quotes and double quotes escape', (next) ->
parse '''
AB,"""",CD,"""hi"""
"",JK,"",""
''', (err, data) ->
return next err if err
data.should.eql [
[ 'AB','"','CD','"hi"' ]
[ '','JK','','' ]
]
next()
it 'line breaks inside quotes', (next) ->
parse """
20322051544,"
",8.8017226E7,45,"
ok
"
"
",1974,8.8392926E7,"","
"
""", (err, data) ->
data.should.eql [
[ '20322051544','\n','8.8017226E7','45','\nok\n' ]
[ '\n','1974','8.8392926E7','','\n' ]
] unless err
next err
describe 'disabled', ->
it 'if empty', (next) ->
parse """
a,b,c
1,r"2"d"2",3
""", quote: '', (err, data) ->
data.should.eql [['a','b','c'],['1','r"2"d"2"','3']] unless err
next err
it 'if null', (next) ->
parse """
a,b,c
1,r"2"d"2",3
""", quote: null, (err, data) ->
data.should.eql [['a','b','c'],['1','r"2"d"2"','3']] unless err
next err
it 'if false', (next) ->
parse """
a,b,c
1,r"2"d"2",3
""", quote: null, (err, data) ->
data.should.eql [['a','b','c'],['1','r"2"d"2"','3']] unless err
next err
describe 'error "Quoted field not terminated"', ->
it 'when unclosed', (next) ->
parse """
"",1974,8.8392926E7,"","
""", (err, data) ->
err.message.should.eql 'Quoted field not terminated at line 1'
next()
describe 'error "Invalid closing quote"', ->
it 'when followed by a character', (next) ->
parse '""!', quote: '"', escape: '"', (err) ->
err.message.should.eql 'Invalid closing quote at line 1; found "!" instead of delimiter ","'
next()
it 'no throw followed by a comment', (next) ->
parse '""# A comment', quote: '"', escape: '"', comment: '#', (err) ->
next err
it 'no throw followed by a delimiter', (next) ->
parse '""|BB', quote: '"', escape: '"', delimiter: '|', (err) ->
next err
it 'no throw followed by a row delimiter', (next) ->
parse '""|BB', quote: '"', escape: '"', rowDelimiter: '|', (err) ->
next err
it 'no throw followed by a trimable character', (next) ->
parse '"" ', quote: '"', escape: '"', rtrim: true, (err) ->
next err
describe 'error "Invalid opening quotes"', ->
it 'count empty lines', (next) ->
parse """
"this","line","is",valid
"this","line",is,"also,valid"
this,"line",is,invalid h"ere"
"and",valid,line,follows...
""", (err, data) ->
err.message.should.eql 'Invalid opening quote at line 4'
(data == undefined).should.be.true
next()