Skip to content

Commit

Permalink
test(csv-parse): on_skip validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed May 13, 2024
1 parent 1326351 commit 53d62d4
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 12 deletions.
12 changes: 9 additions & 3 deletions packages/csv-parse/dist/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ const normalize_options = function(opts){
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down Expand Up @@ -1316,10 +1323,9 @@ const transform = function(original_options = {}) {
class Parser extends stream.Transform {
constructor(opts = {}){
super({...{readableObjectMode: true}, ...opts, encoding: null});
this.api = transform(opts);
this.api.options.on_skip = (err, chunk) => {
this.api = transform({on_skip: (err, chunk) => {
this.emit('skip', err, chunk);
};
}, ...opts});
// Backward compatibility
this.state = this.api.state;
this.options = this.api.options;
Expand Down
7 changes: 7 additions & 0 deletions packages/csv-parse/dist/cjs/sync.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,13 @@ const normalize_options = function(opts){
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down
12 changes: 9 additions & 3 deletions packages/csv-parse/dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5553,6 +5553,13 @@ const normalize_options = function(opts){
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down Expand Up @@ -6438,10 +6445,9 @@ const transform = function(original_options = {}) {
class Parser extends Transform {
constructor(opts = {}){
super({...{readableObjectMode: true}, ...opts, encoding: null});
this.api = transform(opts);
this.api.options.on_skip = (err, chunk) => {
this.api = transform({on_skip: (err, chunk) => {
this.emit('skip', err, chunk);
};
}, ...opts});
// Backward compatibility
this.state = this.api.state;
this.options = this.api.options;
Expand Down
7 changes: 7 additions & 0 deletions packages/csv-parse/dist/esm/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,13 @@ const normalize_options = function(opts){
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down
12 changes: 9 additions & 3 deletions packages/csv-parse/dist/iife/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5556,6 +5556,13 @@ var csv_parse = (function (exports) {
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down Expand Up @@ -6441,10 +6448,9 @@ var csv_parse = (function (exports) {
class Parser extends Transform {
constructor(opts = {}){
super({...{readableObjectMode: true}, ...opts, encoding: null});
this.api = transform(opts);
this.api.options.on_skip = (err, chunk) => {
this.api = transform({on_skip: (err, chunk) => {
this.emit('skip', err, chunk);
};
}, ...opts});
// Backward compatibility
this.state = this.api.state;
this.options = this.api.options;
Expand Down
7 changes: 7 additions & 0 deletions packages/csv-parse/dist/iife/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,13 @@ var csv_parse_sync = (function (exports) {
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down
12 changes: 9 additions & 3 deletions packages/csv-parse/dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5559,6 +5559,13 @@
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down Expand Up @@ -6444,10 +6451,9 @@
class Parser extends Transform {
constructor(opts = {}){
super({...{readableObjectMode: true}, ...opts, encoding: null});
this.api = transform(opts);
this.api.options.on_skip = (err, chunk) => {
this.api = transform({on_skip: (err, chunk) => {
this.emit('skip', err, chunk);
};
}, ...opts});
// Backward compatibility
this.state = this.api.state;
this.options = this.api.options;
Expand Down
7 changes: 7 additions & 0 deletions packages/csv-parse/dist/umd/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,13 @@
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down
7 changes: 7 additions & 0 deletions packages/csv-parse/lib/api/normalize_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ const normalize_options = function(opts){
`got ${JSON.stringify(options.on_record)}`
], options);
}
// Normalize option `on_skip`
// options.on_skip ??= (err, chunk) => {
// this.emit('skip', err, chunk);
// };
if(options.on_skip !== undefined && options.on_skip !== null && typeof options.on_skip !== 'function'){
throw new Error(`Invalid Option: on_skip must be a function, got ${JSON.stringify(options.on_skip)}`);
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null;
Expand Down
36 changes: 36 additions & 0 deletions packages/csv-parse/test/option.on_skip.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

import { parse } from '../lib/index.js'
import { assert_error } from './api.assert_error.coffee'

describe 'Option `on_skip`', ->

it 'validation', ->
parse '', on_skip: (->), (->)
(->
parse '', on_skip: 1, (->)
).should.throw 'Invalid Option: on_skip must be a function, got 1'

it 'handle "CSV_RECORD_INCONSISTENT_FIELDS_LENGTH" with bom (fix #411)', (next) ->
errors = 0
parser = parse
bom: true
skip_records_with_error: true
on_skip: (err) ->
assert_error err,
message: 'Invalid Record Length: expect 4, got 3 on line 2'
code: 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH'
record: ['1', '2', '3']
errors++
, (err, records) ->
records.should.eql [
['a', 'b', 'c', 'd']
['e', 'f', 'g', 'h']
] unless err
errors.should.eql 1
next err
parser.write '''
a,b,c,d
1,2,3
e,f,g,h
'''
parser.end()

0 comments on commit 53d62d4

Please sign in to comment.