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

Commit

Permalink
from: dont accept false
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 30, 2018
1 parent 35283c5 commit 80cff34
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
12 changes: 11 additions & 1 deletion lib/es5/index.js
Expand Up @@ -177,13 +177,23 @@ function (_Transform) {
} // Normalize option `from`


if (options.from === undefined || options.raw === null || options.raw === false) {
if (options.from === undefined || options.from === null) {
options.from = 1;
} else if (Number.isInteger(options.from) && options.from >= 0) {// Great, nothing to do
} else if (typeof options.from === 'string' && /\d+/.test(options.from)) {
options.from = parseInt(options.from);
} else {
throw new Error("Invalid Option: from must be a positive integer, got ".concat(JSON.stringify(options.from)));
} // Normalize option `from_line`


if (options.from_line === undefined || options.from_line === null) {
options.from_line = 1;
} else if (Number.isInteger(options.from_line) && options.from_line > 0) {// Great, nothing to do
} else if (typeof options.from_line === 'string' && /\d+/.test(options.from_line)) {
options.from_line = parseInt(options.from_line);
} else {
throw new Error("Invalid Option: from_line must be a positive integer greater than 0, got ".concat(JSON.stringify(options.from_line)));
} // Normalize option `info`


Expand Down
15 changes: 11 additions & 4 deletions lib/index.js
Expand Up @@ -110,14 +110,21 @@ class Parser extends Transform {
options.escape = options.escape[0]
}
// Normalize option `from`
if(options.from === undefined || options.raw === null || options.raw === false){
if(options.from === undefined || options.from === null){
options.from = 1
}else if(Number.isInteger(options.from) && options.from >= 0){
}else if(Number.isInteger(options.from)){
if(options.from < 0){
throw new Error(`Invalid Option: from must be a positive integer, got ${JSON.stringify(options.from)}`)
}
// Great, nothing to do
}else if(typeof options.from === 'string' && /\d+/.test(options.from)){
options.from = parseInt(options.from)
const from = parseInt(options.from)
if(from < 0){
throw new Error(`Invalid Option: from must be castable to a positive integer, got ${JSON.stringify(options.from)}`)
}
options.from = from
}else{
throw new Error(`Invalid Option: from must be a positive integer, got ${JSON.stringify(options.from)}`)
throw new Error(`Invalid Option: from must be an integer, got ${JSON.stringify(options.from)}`)
}
// Normalize option `from_line`
if(options.from_line === undefined || options.from_line === null){
Expand Down
10 changes: 8 additions & 2 deletions test/option.from.coffee
Expand Up @@ -9,12 +9,18 @@ describe 'Option `from`', ->
(->
parse '', from: -1, (->)
).should.throw 'Invalid Option: from must be a positive integer, got -1'
(->
parse '', from: '-1', (->)
).should.throw 'Invalid Option: from must be castable to a positive integer, got "-1"'
(->
parse '', from: true, (->)
).should.throw 'Invalid Option: from must be a positive integer, got true'
).should.throw 'Invalid Option: from must be an integer, got true'
(->
parse '', from: false, (->)
).should.throw 'Invalid Option: from must be an integer, got false'
(->
parse '', from: 'oh no', (->)
).should.throw 'Invalid Option: from must be a positive integer, got "oh no"'
).should.throw 'Invalid Option: from must be an integer, got "oh no"'

it 'start at defined position', (next) ->
parse """
Expand Down

0 comments on commit 80cff34

Please sign in to comment.