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

Commit

Permalink
from_line: validation refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 30, 2018
1 parent fae2e78 commit 3602cee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ New features:

Minor enhancements:

* from_line: validation refinements
* objname: validation refinements
* from: validation refinements
* escape: validation refinements
Expand Down
14 changes: 11 additions & 3 deletions lib/index.js
Expand Up @@ -10,8 +10,6 @@ const { Transform } = require('stream')
const ResizeableBuffer = require('./ResizeableBuffer')

const default_options = {
from_line: 1,
// TODO create a max_comment_size
to_line: -1,
to: -1,
trim: false
Expand Down Expand Up @@ -121,14 +119,24 @@ class Parser extends Transform {
}else{
throw new Error(`Invalid Option: from must be a positive integer, got ${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 ${JSON.stringify(options.from_line)}`)
}
// Normalize option `info`
if(options.info === undefined || options.info === null || options.info === false){
options.info = false
}else if(options.info !== true){
throw new Error(`Invalid Option: info must be true, got ${JSON.stringify(options.info)}`)
}
// Normalize option `max_record_size`
if(options.max_record_size === undefined || options.raw === null || options.raw === false){
if(options.max_record_size === undefined || options.max_record_size === null || options.max_record_size === false){
options.max_record_size = 0
}else if(Number.isInteger(options.max_record_size) && options.max_record_size >= 0){
// Great, nothing to do
Expand Down
21 changes: 21 additions & 0 deletions test/option.from_line.coffee
Expand Up @@ -2,6 +2,27 @@
parse = require '../lib'

describe 'Option `from_line`', ->

it 'validation', ->
parse '', from_line: 10, (->)
parse '', from_line: "10", (->)
parse '', from_line: null, (->)
parse '', from_line: undefined, (->)
(->
parse '', from_line: -1, (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got -1'
(->
parse '', from_line: 0, (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got 0'
(->
parse '', from_line: true, (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got true'
(->
parse '', from_line: false, (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got false'
(->
parse '', from_line: 'oh no', (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got "oh no"'

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

0 comments on commit 3602cee

Please sign in to comment.