Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
callback: pass undefined instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 16, 2018
1 parent 8a1fd16 commit 77fa2ce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Expand Up @@ -5,7 +5,8 @@

This is a complete rewrite based with a Buffer implementation. There are no major breaking changes but it introduces multiple minor breaking changes:

* options `rowDelimiter` is now `record_delimiter`
* option `rowDelimiter` is now `record_delimiter`
* option `max_limit_on_data_read` is now `max_record_size`
* drop the record event
* normalise error message as `{error type}: {error description}`
* state values are now isolated into the `info` object
Expand All @@ -17,20 +18,20 @@ This is a complete rewrite based with a Buffer implementation. There are no majo
* drop support for deprecated options `auto_parse` and `auto_parse_date`
* drop emission of the `record` event
* in `raw` option, the `row` property is renamed `record`
* option `max_limit_on_data_read` is now `max_record_size`
* default value of `max_record_size` is now `0` (unlimited)
* remove the `record` event, use the `readable` event and `this.read()` instead

New features:

* new options info, from_line and to_line
* new options `info`, `from_line` and `to_line`
* trim: respect `ltrim` and `rtrim` when defined
* delimiter: may be a Buffer
* delimiter: handle multiple bytes/characters
* callback: export info object as third argument
* cast: catch error in user functions
* ts: mark info as readonly with required properties
* comment_lines: count the number of commented lines with no records
* callback: pass undefined instead of null

API management

Expand Down
5 changes: 2 additions & 3 deletions lib/es5/index.js
Expand Up @@ -331,7 +331,6 @@ function (_Transform) {

if (this.state.wasRowDelimiter === true) {
this.info.lines++;
console.log(this.info.records, this.state.info.records);

if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) {
this.state.info = Object.assign({}, this.info);
Expand Down Expand Up @@ -946,10 +945,10 @@ var parse = function parse() {
}
});
parser.on('error', function (err) {
callback(err, null, parser.info);
callback(err, undefined, parser.info);
});
parser.on('end', function () {
callback(null, records, parser.info);
callback(undefined, records, parser.info);
});
}

Expand Down
7 changes: 4 additions & 3 deletions lib/index.d.ts
Expand Up @@ -11,17 +11,18 @@ declare function parse(input: string, callback?: parse.Callback): parse.Parser;
declare function parse(options?: parse.Options, callback?: parse.Callback): parse.Parser;
declare function parse(callback?: parse.Callback): parse.Parser;
declare namespace parse {
type Callback = (err: any | Error, output: any, info: Info) => void;

type Callback = (err: Error | undefined, records: any | undefined, info?: Info) => void;

type MatcherFunc = (value: any) => boolean;

interface Parser extends stream.Transform {}

class Parser {
constructor(options: Options);

__push(line: any): any;

__write(chars: any, end: any, callback: any): any;

readonly options: Options;
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -695,10 +695,10 @@ const parse = function(){
}
})
parser.on('error', function(err){
callback(err, null, parser.info)
callback(err, undefined, parser.info)
})
parser.on('end', function(){
callback(null, records, parser.info)
callback(undefined, records, parser.info)
})
}
if(data !== undefined){
Expand Down
10 changes: 10 additions & 0 deletions test/api.types.ts
Expand Up @@ -31,6 +31,16 @@ describe('API Types', () => {
])
})

it('Receive Callback', (next) => {
parse('a\nb', function(err: Error, data: object, info: Info){
if(err !== undefined){
data.should.eql([['a'], ['b']])
info.records.should.eql(2)
}
next(err)
})
})

})

describe('Info', () => {
Expand Down

0 comments on commit 77fa2ce

Please sign in to comment.