Skip to content

Commit 53d62d4

Browse files
committed
test(csv-parse): on_skip validation
1 parent 1326351 commit 53d62d4

File tree

10 files changed

+107
-12
lines changed

10 files changed

+107
-12
lines changed

packages/csv-parse/dist/cjs/index.cjs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ const normalize_options = function(opts){
431431
`got ${JSON.stringify(options.on_record)}`
432432
], options);
433433
}
434+
// Normalize option `on_skip`
435+
// options.on_skip ??= (err, chunk) => {
436+
// this.emit('skip', err, chunk);
437+
// };
438+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
439+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
440+
}
434441
// Normalize option `quote`
435442
if(options.quote === null || options.quote === false || options.quote === ''){
436443
options.quote = null;
@@ -1316,10 +1323,9 @@ const transform = function(original_options = {}) {
13161323
class Parser extends stream.Transform {
13171324
constructor(opts = {}){
13181325
super({...{readableObjectMode: true}, ...opts, encoding: null});
1319-
this.api = transform(opts);
1320-
this.api.options.on_skip = (err, chunk) => {
1326+
this.api = transform({on_skip: (err, chunk) => {
13211327
this.emit('skip', err, chunk);
1322-
};
1328+
}, ...opts});
13231329
// Backward compatibility
13241330
this.state = this.api.state;
13251331
this.options = this.api.options;

packages/csv-parse/dist/cjs/sync.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ const normalize_options = function(opts){
429429
`got ${JSON.stringify(options.on_record)}`
430430
], options);
431431
}
432+
// Normalize option `on_skip`
433+
// options.on_skip ??= (err, chunk) => {
434+
// this.emit('skip', err, chunk);
435+
// };
436+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
437+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
438+
}
432439
// Normalize option `quote`
433440
if(options.quote === null || options.quote === false || options.quote === ''){
434441
options.quote = null;

packages/csv-parse/dist/esm/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5553,6 +5553,13 @@ const normalize_options = function(opts){
55535553
`got ${JSON.stringify(options.on_record)}`
55545554
], options);
55555555
}
5556+
// Normalize option `on_skip`
5557+
// options.on_skip ??= (err, chunk) => {
5558+
// this.emit('skip', err, chunk);
5559+
// };
5560+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
5561+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
5562+
}
55565563
// Normalize option `quote`
55575564
if(options.quote === null || options.quote === false || options.quote === ''){
55585565
options.quote = null;
@@ -6438,10 +6445,9 @@ const transform = function(original_options = {}) {
64386445
class Parser extends Transform {
64396446
constructor(opts = {}){
64406447
super({...{readableObjectMode: true}, ...opts, encoding: null});
6441-
this.api = transform(opts);
6442-
this.api.options.on_skip = (err, chunk) => {
6448+
this.api = transform({on_skip: (err, chunk) => {
64436449
this.emit('skip', err, chunk);
6444-
};
6450+
}, ...opts});
64456451
// Backward compatibility
64466452
this.state = this.api.state;
64476453
this.options = this.api.options;

packages/csv-parse/dist/esm/sync.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,6 +2399,13 @@ const normalize_options = function(opts){
23992399
`got ${JSON.stringify(options.on_record)}`
24002400
], options);
24012401
}
2402+
// Normalize option `on_skip`
2403+
// options.on_skip ??= (err, chunk) => {
2404+
// this.emit('skip', err, chunk);
2405+
// };
2406+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
2407+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
2408+
}
24022409
// Normalize option `quote`
24032410
if(options.quote === null || options.quote === false || options.quote === ''){
24042411
options.quote = null;

packages/csv-parse/dist/iife/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5556,6 +5556,13 @@ var csv_parse = (function (exports) {
55565556
`got ${JSON.stringify(options.on_record)}`
55575557
], options);
55585558
}
5559+
// Normalize option `on_skip`
5560+
// options.on_skip ??= (err, chunk) => {
5561+
// this.emit('skip', err, chunk);
5562+
// };
5563+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
5564+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
5565+
}
55595566
// Normalize option `quote`
55605567
if(options.quote === null || options.quote === false || options.quote === ''){
55615568
options.quote = null;
@@ -6441,10 +6448,9 @@ var csv_parse = (function (exports) {
64416448
class Parser extends Transform {
64426449
constructor(opts = {}){
64436450
super({...{readableObjectMode: true}, ...opts, encoding: null});
6444-
this.api = transform(opts);
6445-
this.api.options.on_skip = (err, chunk) => {
6451+
this.api = transform({on_skip: (err, chunk) => {
64466452
this.emit('skip', err, chunk);
6447-
};
6453+
}, ...opts});
64486454
// Backward compatibility
64496455
this.state = this.api.state;
64506456
this.options = this.api.options;

packages/csv-parse/dist/iife/sync.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,13 @@ var csv_parse_sync = (function (exports) {
24022402
`got ${JSON.stringify(options.on_record)}`
24032403
], options);
24042404
}
2405+
// Normalize option `on_skip`
2406+
// options.on_skip ??= (err, chunk) => {
2407+
// this.emit('skip', err, chunk);
2408+
// };
2409+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
2410+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
2411+
}
24052412
// Normalize option `quote`
24062413
if(options.quote === null || options.quote === false || options.quote === ''){
24072414
options.quote = null;

packages/csv-parse/dist/umd/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5559,6 +5559,13 @@
55595559
`got ${JSON.stringify(options.on_record)}`
55605560
], options);
55615561
}
5562+
// Normalize option `on_skip`
5563+
// options.on_skip ??= (err, chunk) => {
5564+
// this.emit('skip', err, chunk);
5565+
// };
5566+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
5567+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
5568+
}
55625569
// Normalize option `quote`
55635570
if(options.quote === null || options.quote === false || options.quote === ''){
55645571
options.quote = null;
@@ -6444,10 +6451,9 @@
64446451
class Parser extends Transform {
64456452
constructor(opts = {}){
64466453
super({...{readableObjectMode: true}, ...opts, encoding: null});
6447-
this.api = transform(opts);
6448-
this.api.options.on_skip = (err, chunk) => {
6454+
this.api = transform({on_skip: (err, chunk) => {
64496455
this.emit('skip', err, chunk);
6450-
};
6456+
}, ...opts});
64516457
// Backward compatibility
64526458
this.state = this.api.state;
64536459
this.options = this.api.options;

packages/csv-parse/dist/umd/sync.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,13 @@
24052405
`got ${JSON.stringify(options.on_record)}`
24062406
], options);
24072407
}
2408+
// Normalize option `on_skip`
2409+
// options.on_skip ??= (err, chunk) => {
2410+
// this.emit('skip', err, chunk);
2411+
// };
2412+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
2413+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
2414+
}
24082415
// Normalize option `quote`
24092416
if(options.quote === null || options.quote === false || options.quote === ''){
24102417
options.quote = null;

packages/csv-parse/lib/api/normalize_options.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ const normalize_options = function(opts){
270270
`got ${JSON.stringify(options.on_record)}`
271271
], options);
272272
}
273+
// Normalize option `on_skip`
274+
// options.on_skip ??= (err, chunk) => {
275+
// this.emit('skip', err, chunk);
276+
// };
277+
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
278+
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
279+
}
273280
// Normalize option `quote`
274281
if(options.quote === null || options.quote === false || options.quote === ''){
275282
options.quote = null;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
import { parse } from '../lib/index.js'
3+
import { assert_error } from './api.assert_error.coffee'
4+
5+
describe 'Option `on_skip`', ->
6+
7+
it 'validation', ->
8+
parse '', on_skip: (->), (->)
9+
(->
10+
parse '', on_skip: 1, (->)
11+
).should.throw 'Invalid Option: on_skip must be a function, got 1'
12+
13+
it 'handle "CSV_RECORD_INCONSISTENT_FIELDS_LENGTH" with bom (fix #411)', (next) ->
14+
errors = 0
15+
parser = parse
16+
bom: true
17+
skip_records_with_error: true
18+
on_skip: (err) ->
19+
assert_error err,
20+
message: 'Invalid Record Length: expect 4, got 3 on line 2'
21+
code: 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH'
22+
record: ['1', '2', '3']
23+
errors++
24+
, (err, records) ->
25+
records.should.eql [
26+
['a', 'b', 'c', 'd']
27+
['e', 'f', 'g', 'h']
28+
] unless err
29+
errors.should.eql 1
30+
next err
31+
parser.write '''
32+
a,b,c,d
33+
1,2,3
34+
e,f,g,h
35+
'''
36+
parser.end()

0 commit comments

Comments
 (0)