/
trim.coffee
106 lines (95 loc) · 3.31 KB
/
trim.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
###
Node CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
Testing the read options `trim`, `ltrim` and `rtrim`.
###
fs = require 'fs'
should = require 'should'
csv = if process.env.CSV_COV then require '../lib-cov' else require '../src'
describe 'ltrim', ->
it 'should ignore the whitespaces immediately following the delimiter', (next) ->
csv()
.from.string("""
FIELD_1, FIELD_2, FIELD_3, FIELD_4, FIELD_5, FIELD_6
20322051544," 1979",8.8017226E7, ABC,45,2000-01-01
28392898392, " 1974", 8.8392926E7,DEF, 23, 2050-11-27
""", ltrim: true )
.transform( (record, index) -> record )
.on 'close', (count) ->
count.should.eql 3
.to.string (data) ->
data.should.eql """
FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_6
20322051544, 1979,8.8017226E7,ABC,45,2000-01-01
28392898392, 1974,8.8392926E7,DEF,23,2050-11-27
"""
next()
it 'should work on last field', (next) ->
csv()
.from.string("""
FIELD_1, FIELD_2
20322051544, a
28392898392, " "
""", ltrim: true )
.transform( (record, index) -> record )
.on 'close', (count) ->
count.should.eql 3
.to.string (data) ->
data.should.eql """
FIELD_1,FIELD_2
20322051544,a
28392898392,
"""
next()
describe 'rtrim', ->
it 'should ignore the whitespaces immediately preceding the delimiter', (next) ->
csv()
.from.string("""
FIELD_1 ,FIELD_2 ,FIELD_3 ,FIELD_4 ,FIELD_5 ,FIELD_6
20322051544 ,1979,8.8017226E7 ,ABC,45 ,2000-01-01
28392898392,1974 ,8.8392926E7,DEF,23 ,2050-11-27
""", rtrim: true )
.transform( (record, index) -> record )
.on 'close', (count) ->
count.should.eql 3
.to.string (data) ->
data.should.eql """
FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_6
20322051544,1979,8.8017226E7,ABC,45,2000-01-01
28392898392,1974,8.8392926E7,DEF,23,2050-11-27
"""
next()
describe 'trim', ->
it 'should ignore the whitespaces immediately preceding and following the delimiter', (next) ->
csv()
.from.string("""
FIELD_1 , FIELD_2 , FIELD_3,FIELD_4 , FIELD_5,FIELD_6
20322051544,1979 ,8.8017226E7,ABC , 45 , 2000-01-01
28392898392, 1974,8.8392926E7,DEF , 23 , 2050-11-27
""", trim: true )
.transform( (record, index) -> record )
.on 'close', (count) ->
count.should.eql 3
.to.string (data) ->
data.should.eql """
FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_6
20322051544,1979,8.8017226E7,ABC,45,2000-01-01
28392898392,1974,8.8392926E7,DEF,23,2050-11-27
"""
next()
it 'should preserve surrounding whitespaces', (next) ->
csv()
.from.string("""
FIELD_1 , FIELD_2 , FIELD_3,FIELD_4 , FIELD_5,FIELD_6
20322051544,1979 ,8.8017226E7,ABC , 45 , 2000-01-01
28392898392, 1974,8.8392926E7,DEF , 23 , 2050-11-27
""")
.transform( (record, index) -> record )
.on 'close', (count) ->
count.should.eql 3
.to.string (data) ->
data.should.eql """
FIELD_1 , FIELD_2 , FIELD_3,FIELD_4 , FIELD_5,FIELD_6
20322051544,1979 ,8.8017226E7,ABC , 45 , 2000-01-01
28392898392, 1974,8.8392926E7,DEF , 23 , 2050-11-27
"""
next()