-
Notifications
You must be signed in to change notification settings - Fork 266
/
option.to_line.coffee
101 lines (91 loc) · 2.43 KB
/
option.to_line.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
import { Readable } from 'node:stream'
import { parse } from '../lib/index.js'
import { generate } from 'csv-generate'
describe 'Option `to_line`', ->
it 'validation', ->
parse '', to_line: 10, (->)
parse '', to_line: "10", (->)
parse '', to_line: null, (->)
parse '', to_line: undefined, (->)
(->
parse '', to_line: -1, (->)
).should.throw 'Invalid Option: to_line must be a positive integer greater than 0, got -1'
(->
parse '', to_line: 0, (->)
).should.throw 'Invalid Option: to_line must be a positive integer greater than 0, got 0'
(->
parse '', to_line: "0", (->)
).should.throw 'Invalid Option: to_line must be a positive integer greater than 0, got "0"'
(->
parse '', to_line: true, (->)
).should.throw 'Invalid Option: to_line must be an integer, got true'
(->
parse '', to_line: false, (->)
).should.throw 'Invalid Option: to_line must be an integer, got false'
(->
parse '', to_line: 'oh no', (->)
).should.throw 'Invalid Option: to_line must be an integer, got "oh no"'
it 'start at defined position', (next) ->
parse """
1,2,3
4,5,6
7,8,9
""", to_line: 2, (err, records) ->
records.should.eql [
[ '1','2','3' ]
[ '4','5','6' ]
] unless err
next err
it 'count headers', (next) ->
parse """
a,b,c
1,2,3
4,5,6
7,8,9
""", columns: true, to_line: 3, (err, records) ->
records.should.eql [
{a: '1',b: '2',c: '3'}
{a: '4',b: '5',c: '6'}
] unless err
next err
it 'records with quoted line at the end of line', (next) ->
parse """
1,2,"
3"
4,5,"
6"
7,8,"
9"
""", to_line: 5, (err, records) ->
records.should.eql [
[ '1','2','\n\n3' ]
[ '4','5','\n6' ]
] unless err
next err
it 'records with quoted line in the middle of line', (next) ->
parse """
1,2,"
3"
4,5,"
6"
7,8,"
9"
""", to_line: 6, (err, records) ->
records.should.eql [
[ '1','2','\n\n3' ]
[ '4','5','\n6' ]
] unless err
next err
it 'not influenced by record delimiter', (next) ->
parse """
a,b,c:1,2,
3:d,e,f:4,5,
6:g,h,i:7,8,
9
""", to_line: 2, record_delimiter: ':', (err, records) ->
records.should.eql [
[ 'a','b','c' ]
[ '1','2','\n3' ]
[ 'd','e','f' ]
] unless err
next err