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

Commit

Permalink
from: validation refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 29, 2018
1 parent f3d86b2 commit 83337c8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@

Enhancements:

* from: validation refinements
* escape: validation refinements
* skip_empty_lines: validation refinements
* skip_lines_with_empty_values: validation refinements
Expand Down
11 changes: 10 additions & 1 deletion lib/es5/index.js
Expand Up @@ -50,7 +50,6 @@ var _require = require('stream'),
var ResizeableBuffer = require('./ResizeableBuffer');

var default_options = {
from: 1,
from_line: 1,
objname: undefined,
// TODO create a max_comment_size
Expand Down Expand Up @@ -175,6 +174,16 @@ function (_Transform) {
throw new Error("Invalid Option Length: escape must be one character, got ".concat(options.escape.length));
} else {
options.escape = options.escape[0];
} // Normalize option `from`


if (options.from === undefined || options.raw === null || options.raw === false) {
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 `info`


Expand Down
11 changes: 10 additions & 1 deletion lib/index.js
Expand Up @@ -10,7 +10,6 @@ const { Transform } = require('stream')
const ResizeableBuffer = require('./ResizeableBuffer')

const default_options = {
from: 1,
from_line: 1,
objname: undefined,
// TODO create a max_comment_size
Expand Down Expand Up @@ -113,6 +112,16 @@ class Parser extends Transform {
}else{
options.escape = options.escape[0]
}
// Normalize option `from`
if(options.from === undefined || options.raw === null || options.raw === false){
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 ${JSON.stringify(options.from)}`)
}
// Normalize option `info`
if(options.info === undefined || options.info === null || options.info === false){
options.info = false
Expand Down
13 changes: 13 additions & 0 deletions test/option.from.coffee
Expand Up @@ -2,6 +2,19 @@
parse = require '../lib'

describe 'Option `from`', ->

it 'validation', ->
parse '', from: 10, (->)
parse '', from: "10", (->)
(->
parse '', from: -1, (->)
).should.throw 'Invalid Option: from must be a positive integer, got -1'
(->
parse '', from: true, (->)
).should.throw 'Invalid Option: from must be a positive integer, got true'
(->
parse '', from: 'oh no', (->)
).should.throw 'Invalid Option: from must be a positive integer, got "oh no"'

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

0 comments on commit 83337c8

Please sign in to comment.