/
option.on_record.coffee
105 lines (92 loc) · 3.45 KB
/
option.on_record.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
import { parse } from '../lib/index.js'
import { assert_error } from './api.assert_error.coffee'
describe 'Option `on_record`', ->
it 'validate', ->
(->
parse on_record: true
).should.throw
message: 'Invalid option `on_record`: expect a function, got true'
code: 'CSV_INVALID_OPTION_ON_RECORD'
describe 'usage', ->
it 'alter records', (next) ->
parse "a,b", on_record: (record) ->
[record[1], record[0]]
, (err, records) ->
records.should.eql [ ['b', 'a'] ] unless err
next err
it 'filter records', (next) ->
parse "a,b\nc,d\ne,f", on_record: (record, {lines}) ->
if lines is 2 then null else record
, (err, records) ->
records.should.eql [ ['a', 'b'], ['e', 'f'] ] unless err
next err
it 'errors with callback', (next) ->
parse "a,b\nc,d\ne,f",
on_record: (record, {lines}) ->
if lines is 2 then throw Error 'Error thrown on line 2' else record
, (err, records) ->
err.message.should.eql 'Error thrown on line 2'
next()
it 'errors with events', (next) ->
parser = parse "a,a,a\nc,d\ne,f"
parser.on 'error', (err) ->
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
next()
parser.on 'end', () ->
next Error 'Should not be called'
it 'errors not handled by skip_records_with_error', (next) ->
parse "a,b\nc,d\ne,f",
on_record: (record, {lines}) ->
if lines is 2 then throw Error 'Error thrown on line 2' else record
skip_records_with_error: true
, (err, records) ->
err.message.should.eql 'Error thrown on line 2'
next()
describe 'context', ->
it 'properties', (next) ->
parse "a,b",
on_record: (record, context) ->
should(context.raw).be.undefined()
Object.keys(context).sort()
skip_records_with_error: true
, (err, records) ->
records.should.eql [[
'bytes',
'columns', 'comment_lines', 'empty_lines', 'error', 'header',
'index', 'invalid_field_length', 'lines', 'raw', 'records'
]]
next()
it 'properties with `columns: true` and `raw: true`', (next) ->
parse "a,b\n1,2\n3,4",
columns: true
raw: true
on_record: (record, context) ->
if context.lines is 2
context.raw.should.eql '1,2\n'
else if context.lines is 3
context.raw.should.eql '3,4'
Object.keys(context).sort()
skip_records_with_error: true
, (err, records) ->
records.shift().should.eql [
'bytes',
'columns', 'comment_lines', 'empty_lines', 'error', 'header',
'index', 'invalid_field_length', 'lines', 'raw', 'records'
]
next()
it 'values', (next) ->
parse "a,b\nc,d",
on_record: (record, context) ->
context
skip_records_with_error: true
, (err, records) ->
records.should.eql [
bytes: 4,
columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false
index: 2, invalid_field_length: 0, lines: 1, raw: undefined, records: 1
,
bytes: 7,
columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false
index: 2, invalid_field_length: 0, lines: 2, raw: undefined, records: 2
]
next()