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

Commit

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

Minor enhancements:

* to: validation refinements
* from_line: validation refinements
* objname: validation refinements
* from: validation refinements
Expand Down
34 changes: 29 additions & 5 deletions lib/es5/index.js
Expand Up @@ -50,11 +50,7 @@ var _require = require('stream'),
var ResizeableBuffer = require('./ResizeableBuffer');

var default_options = {
from_line: 1,
objname: undefined,
// TODO create a max_comment_size
to_line: -1,
to: -1,
trim: false
};
var cr = 13;
Expand Down Expand Up @@ -204,13 +200,31 @@ function (_Transform) {
} // 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
} else if (typeof options.max_record_size === 'string' && /\d+/.test(options.max_record_size)) {
options.max_record_size = parseInt(options.max_record_size);
} else {
throw new Error("Invalid Option: max_record_size must be a positive integer, got ".concat(JSON.stringify(options.max_record_size)));
} // Normalize option `objname`


if (options.objname === undefined || options.objname === null || options.objname === false) {
options.objname = undefined;
} else if (Buffer.isBuffer(options.objname)) {
if (options.objname.length === 0) {
throw new Error("Invalid Option: objname must be a non empty buffer");
}

options.objname = options.objname.toString();
} else if (typeof options.objname === 'string') {
if (options.objname.length === 0) {
throw new Error("Invalid Option: objname must be a non empty string");
} // Great, nothing to do

} else {
throw new Error("Invalid Option: objname must be a string or a buffer, got ".concat(options.objname));
} // Normalize option `quote`


Expand Down Expand Up @@ -304,6 +318,16 @@ function (_Transform) {
options.rtrim = true;
} else if (options.rtrim !== true) {
options.rtrim = false;
} // Normalize option `to`


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

_this.info = {
Expand Down
18 changes: 17 additions & 1 deletion lib/index.js
Expand Up @@ -11,7 +11,6 @@ const ResizeableBuffer = require('./ResizeableBuffer')

const default_options = {
to_line: -1,
to: -1,
trim: false
}

Expand Down Expand Up @@ -254,6 +253,23 @@ class Parser extends Transform {
}else if(options.rtrim !== true){
options.rtrim = false
}
// Normalize option `to`
if(options.to === undefined || options.to === null){
options.to = -1
}else if(Number.isInteger(options.to)){
if(options.to <= 0){
throw new Error(`Invalid Option: to must be a positive integer, got ${JSON.stringify(options.to)}`)
}
// Great, nothing to do
}else if(typeof options.to === 'string' && /\d+/.test(options.to)){
const to = parseInt(options.to)
if(to <= 0){
throw new Error(`Invalid Option: to must be castable to a positive integer, got ${JSON.stringify(options.to)}`)
}
options.to = to
}else{
throw new Error(`Invalid Option: to must be an integer, got ${JSON.stringify(options.to)}`)
}
this.info = {
comment_lines: 0,
empty_lines: 0,
Expand Down
22 changes: 22 additions & 0 deletions test/option.to.coffee
Expand Up @@ -2,6 +2,28 @@
parse = require '../lib'

describe 'Option `to`', ->

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

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

0 comments on commit b1b5c54

Please sign in to comment.