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

Commit

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

Minor enhancements:

* trim, ltrim, rtrim: validation refinements
* to: validation refinements
* from_line: validation refinements
* objname: validation refinements
Expand Down
60 changes: 50 additions & 10 deletions lib/es5/index.js
Expand Up @@ -50,8 +50,7 @@ var _require = require('stream'),
var ResizeableBuffer = require('./ResizeableBuffer');

var default_options = {
to_line: -1,
trim: false
to_line: -1
};
var cr = 13;
var nl = 10;
Expand Down Expand Up @@ -175,11 +174,21 @@ function (_Transform) {

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 (Number.isInteger(options.from)) {
if (options.from < 0) {
throw new Error("Invalid Option: from must be a positive integer, got ".concat(JSON.stringify(options.from)));
} // Great, nothing to do

} else if (typeof options.from === 'string' && /\d+/.test(options.from)) {
options.from = parseInt(options.from);
var from = parseInt(options.from);

if (from < 0) {
throw new Error("Invalid Option: from must be castable to a positive integer, got ".concat(JSON.stringify(options.from)));
}

options.from = from;
} else {
throw new Error("Invalid Option: from must be a positive integer, got ".concat(JSON.stringify(options.from)));
throw new Error("Invalid Option: from must be an integer, got ".concat(JSON.stringify(options.from)));
} // Normalize option `from_line`


Expand Down Expand Up @@ -305,16 +314,37 @@ function (_Transform) {
options.skip_lines_with_error = false;
} else {
throw new Error("Invalid Option: skip_lines_with_error must be a boolean, got ".concat(JSON.stringify(options.skip_lines_with_error)));
} // Normalize option `rtrim`


if (options.rtrim === undefined || options.rtrim === null || options.rtrim === false) {
options.rtrim = false;
} else if (options.rtrim !== true) {
throw new Error("Invalid Option: rtrim must be a boolean, got ".concat(JSON.stringify(options.rtrim)));
} // Normalize option `ltrim`


if (options.ltrim === undefined || options.ltrim === null || options.ltrim === false) {
options.ltrim = false;
} else if (options.ltrim !== true) {
throw new Error("Invalid Option: ltrim must be a boolean, got ".concat(JSON.stringify(options.ltrim)));
} // Normalize option `trim`


if (options.trim === undefined || options.trim === null || options.trim === false) {
options.trim = false;
} else if (options.trim !== true) {
throw new Error("Invalid Option: trim must be a boolean, got ".concat(JSON.stringify(options.trim)));
} // Normalize options `trim`, `ltrim` and `rtrim`


if (options.trim === true && options.ltrim !== false) {
if (options.trim === true && opts.ltrim !== false) {
options.ltrim = true;
} else if (options.ltrim !== true) {
options.ltrim = false;
}

if (options.trim === true && options.rtrim !== false) {
if (options.trim === true && opts.rtrim !== false) {
options.rtrim = true;
} else if (options.rtrim !== true) {
options.rtrim = false;
Expand All @@ -323,11 +353,21 @@ function (_Transform) {

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 (Number.isInteger(options.to)) {
if (options.to <= 0) {
throw new Error("Invalid Option: to must be a positive integer, got ".concat(JSON.stringify(options.to)));
} // Great, nothing to do

} else if (typeof options.to === 'string' && /\d+/.test(options.to)) {
options.to = parseInt(options.to);
var to = parseInt(options.to);

if (to <= 0) {
throw new Error("Invalid Option: to must be castable to a positive integer, got ".concat(JSON.stringify(options.to)));
}

options.to = to;
} else {
throw new Error("Invalid Option: to must be a positive integer, got ".concat(JSON.stringify(options.to)));
throw new Error("Invalid Option: to must be an integer, got ".concat(JSON.stringify(options.to)));
}

_this.info = {
Expand Down
23 changes: 20 additions & 3 deletions lib/index.js
Expand Up @@ -11,7 +11,6 @@ const ResizeableBuffer = require('./ResizeableBuffer')

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

const cr = 13
Expand Down Expand Up @@ -242,13 +241,31 @@ class Parser extends Transform {
}else{
throw new Error(`Invalid Option: skip_lines_with_error must be a boolean, got ${JSON.stringify(options.skip_lines_with_error)}`)
}
// Normalize option `rtrim`
if(options.rtrim === undefined || options.rtrim === null || options.rtrim === false){
options.rtrim = false
}else if(options.rtrim !== true){
throw new Error(`Invalid Option: rtrim must be a boolean, got ${JSON.stringify(options.rtrim)}`)
}
// Normalize option `ltrim`
if(options.ltrim === undefined || options.ltrim === null || options.ltrim === false){
options.ltrim = false
}else if(options.ltrim !== true){
throw new Error(`Invalid Option: ltrim must be a boolean, got ${JSON.stringify(options.ltrim)}`)
}
// Normalize option `trim`
if(options.trim === undefined || options.trim === null || options.trim === false){
options.trim = false
}else if(options.trim !== true){
throw new Error(`Invalid Option: trim must be a boolean, got ${JSON.stringify(options.trim)}`)
}
// Normalize options `trim`, `ltrim` and `rtrim`
if(options.trim === true && options.ltrim !== false){
if(options.trim === true && opts.ltrim !== false){
options.ltrim = true
}else if(options.ltrim !== true){
options.ltrim = false
}
if(options.trim === true && options.rtrim !== false){
if(options.trim === true && opts.rtrim !== false){
options.rtrim = true
}else if(options.rtrim !== true){
options.rtrim = false
Expand Down
40 changes: 40 additions & 0 deletions test/option.trim.coffee
@@ -1,8 +1,48 @@

parse = require '../lib'

describe 'Option `rtrim`', ->

it 'validation', ->
parse '', rtrim: true, (->)
parse '', rtrim: false, (->)
parse '', rtrim: null, (->)
parse '', rtrim: undefined, (->)
(->
parse '', rtrim: 1, (->)
).should.throw 'Invalid Option: rtrim must be a boolean, got 1'
(->
parse '', rtrim: "true", (->)
).should.throw 'Invalid Option: rtrim must be a boolean, got "true"'

describe 'Option `ltrim`', ->

it 'validation', ->
parse '', ltrim: true, (->)
parse '', ltrim: false, (->)
parse '', ltrim: null, (->)
parse '', ltrim: undefined, (->)
(->
parse '', ltrim: 1, (->)
).should.throw 'Invalid Option: ltrim must be a boolean, got 1'
(->
parse '', ltrim: "true", (->)
).should.throw 'Invalid Option: ltrim must be a boolean, got "true"'

describe 'Option `trim`', ->

it 'validation', ->
parse '', trim: true, (->)
parse '', trim: false, (->)
parse '', trim: null, (->)
parse '', trim: undefined, (->)
(->
parse '', trim: 1, (->)
).should.throw 'Invalid Option: trim must be a boolean, got 1'
(->
parse '', trim: "true", (->)
).should.throw 'Invalid Option: trim must be a boolean, got "true"'

it 'set ltrim', ->
parser = parse trim: true
parser.options.ltrim.should.be.true()
Expand Down

0 comments on commit b82e4c8

Please sign in to comment.