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

Commit

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

Minor enhancements:

* to_line: validation refinements
* trim, ltrim, rtrim: validation refinements
* to: validation refinements
* from_line: validation refinements
Expand Down
86 changes: 47 additions & 39 deletions lib/es5/index.js
Expand Up @@ -49,9 +49,6 @@ var _require = require('stream'),

var ResizeableBuffer = require('./ResizeableBuffer');

var default_options = {
to_line: -1
};
var cr = 13;
var nl = 10;
var space = 32;
Expand All @@ -72,14 +69,7 @@ function (_Transform) {
_this = _possibleConstructorReturn(this, _getPrototypeOf(Parser).call(this, _objectSpread({}, {
readableObjectMode: true
}, opts)));
var options = {}; // Import default options

for (var k in default_options) {
if (options[k] === undefined) {
options[k] = default_options[k];
}
} // Merge with user options

var options = {}; // Merge with user options

for (var opt in opts) {
options[underscore(opt)] = opts[opt];
Expand Down Expand Up @@ -174,31 +164,35 @@ function (_Transform) {

if (options.from === undefined || options.from === null) {
options.from = 1;
} 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)) {
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)));
} else {
if (typeof options.from === 'string' && /\d+/.test(options.from)) {
options.from = parseInt(options.from);
}

options.from = from;
} else {
throw new Error("Invalid Option: from must be an integer, got ".concat(JSON.stringify(options.from)));
if (Number.isInteger(options.from)) {
if (options.from < 0) {
throw new Error("Invalid Option: from must be a positive integer, got ".concat(JSON.stringify(opts.from)));
}
} else {
throw new Error("Invalid Option: from must be an 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)));
if (typeof options.from_line === 'string' && /\d+/.test(options.from_line)) {
options.from_line = parseInt(options.from_line);
}

if (Number.isInteger(options.from_line)) {
if (options.from_line <= 0) {
throw new Error("Invalid Option: from_line must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.from_line)));
}
} else {
throw new Error("Invalid Option: from_line must be an integer, got ".concat(JSON.stringify(opts.from_line)));
}
} // Normalize option `info`


Expand Down Expand Up @@ -353,21 +347,35 @@ function (_Transform) {

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 ".concat(JSON.stringify(options.to)));
} // Great, nothing to do

} else if (typeof options.to === 'string' && /\d+/.test(options.to)) {
var to = parseInt(options.to);
} else {
if (typeof options.to === 'string' && /\d+/.test(options.to)) {
options.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)));
if (Number.isInteger(options.to)) {
if (options.to <= 0) {
throw new Error("Invalid Option: to must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.to)));
}
} else {
throw new Error("Invalid Option: to must be an integer, got ".concat(JSON.stringify(opts.to)));
}
} // Normalize option `to_line`


options.to = to;
if (options.to_line === undefined || options.to_line === null) {
options.to_line = -1;
} else {
throw new Error("Invalid Option: to must be an integer, got ".concat(JSON.stringify(options.to)));
if (typeof options.to_line === 'string' && /\d+/.test(options.to_line)) {
options.to_line = parseInt(options.to_line);
}

if (Number.isInteger(options.to_line)) {
if (options.to_line <= 0) {
throw new Error("Invalid Option: to_line must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.to_line)));
}
} else {
throw new Error("Invalid Option: to_line must be an integer, got ".concat(JSON.stringify(opts.to_line)));
}
}

_this.info = {
Expand Down
78 changes: 42 additions & 36 deletions lib/index.js
Expand Up @@ -9,10 +9,6 @@ information.
const { Transform } = require('stream')
const ResizeableBuffer = require('./ResizeableBuffer')

const default_options = {
to_line: -1,
}

const cr = 13
const nl = 10
const space = 32
Expand All @@ -22,12 +18,6 @@ class Parser extends Transform {
constructor(opts = {}){
super({...{readableObjectMode: true}, ...opts})
const options = {}
// Import default options
for(let k in default_options){
if(options[k] === undefined){
options[k] = default_options[k]
}
}
// Merge with user options
for(let opt in opts){
options[underscore(opt)] = opts[opt]
Expand Down Expand Up @@ -110,29 +100,32 @@ class Parser extends Transform {
// Normalize option `from`
if(options.from === undefined || options.from === null){
options.from = 1
}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)}`)
}else{
if(typeof options.from === 'string' && /\d+/.test(options.from)){
options.from = parseInt(options.from)
}
// Great, nothing to do
}else if(typeof options.from === 'string' && /\d+/.test(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)}`)
if(Number.isInteger(options.from)){
if(options.from < 0){
throw new Error(`Invalid Option: from must be a positive integer, got ${JSON.stringify(opts.from)}`)
}
}else{
throw new Error(`Invalid Option: from must be an integer, got ${JSON.stringify(options.from)}`)
}
options.from = from
}else{
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){
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)}`)
if(typeof options.from_line === 'string' && /\d+/.test(options.from_line)){
options.from_line = parseInt(options.from_line)
}
if(Number.isInteger(options.from_line)){
if(options.from_line <= 0){
throw new Error(`Invalid Option: from_line must be a positive integer greater than 0, got ${JSON.stringify(opts.from_line)}`)
}
}else{
throw new Error(`Invalid Option: from_line must be an integer, got ${JSON.stringify(opts.from_line)}`)
}
}
// Normalize option `info`
if(options.info === undefined || options.info === null || options.info === false){
Expand Down Expand Up @@ -273,19 +266,32 @@ class Parser extends Transform {
// 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)}`)
}else{
if(typeof options.to === 'string' && /\d+/.test(options.to)){
options.to = parseInt(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)}`)
if(Number.isInteger(options.to)){
if(options.to <= 0){
throw new Error(`Invalid Option: to must be a positive integer greater than 0, got ${JSON.stringify(opts.to)}`)
}
}else{
throw new Error(`Invalid Option: to must be an integer, got ${JSON.stringify(opts.to)}`)
}
options.to = to
}
// Normalize option `to_line`
if(options.to_line === undefined || options.to_line === null){
options.to_line = -1
}else{
throw new Error(`Invalid Option: to must be an integer, got ${JSON.stringify(options.to)}`)
if(typeof options.to_line === 'string' && /\d+/.test(options.to_line)){
options.to_line = parseInt(options.to_line)
}
if(Number.isInteger(options.to_line)){
if(options.to_line <= 0){
throw new Error(`Invalid Option: to_line must be a positive integer greater than 0, got ${JSON.stringify(opts.to_line)}`)
}
}else{
throw new Error(`Invalid Option: to_line must be an integer, got ${JSON.stringify(opts.to_line)}`)
}
}
this.info = {
comment_lines: 0,
Expand Down
2 changes: 1 addition & 1 deletion test/option.from.coffee
Expand Up @@ -11,7 +11,7 @@ describe 'Option `from`', ->
).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"'
).should.throw 'Invalid Option: from must be a positive integer, got "-1"'
(->
parse '', from: true, (->)
).should.throw 'Invalid Option: from must be an integer, got true'
Expand Down
9 changes: 6 additions & 3 deletions test/option.from_line.coffee
Expand Up @@ -14,15 +14,18 @@ describe 'Option `from_line`', ->
(->
parse '', from_line: 0, (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got 0'
(->
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'
).should.throw 'Invalid Option: from_line must be an integer, got true'
(->
parse '', from_line: false, (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got false'
).should.throw 'Invalid Option: from_line must be an integer, got false'
(->
parse '', from_line: 'oh no', (->)
).should.throw 'Invalid Option: from_line must be a positive integer greater than 0, got "oh no"'
).should.throw 'Invalid Option: from_line must be an integer, got "oh no"'

it 'start at defined position', (next) ->
parse """
Expand Down
6 changes: 3 additions & 3 deletions test/option.to.coffee
Expand Up @@ -8,13 +8,13 @@ describe 'Option `to`', ->
parse '', to: "10", (->)
(->
parse '', to: -1, (->)
).should.throw 'Invalid Option: to must be a positive integer, got -1'
).should.throw 'Invalid Option: to must be a positive integer greater than 0, got -1'
(->
parse '', to: 0, (->)
).should.throw 'Invalid Option: to must be a positive integer, got 0'
).should.throw 'Invalid Option: to must be a positive integer greater than 0, got 0'
(->
parse '', to: '0', (->)
).should.throw 'Invalid Option: to must be castable to a positive integer, got "0"'
).should.throw 'Invalid Option: to must be a positive integer greater than 0, got "0"'
(->
parse '', to: true, (->)
).should.throw 'Invalid Option: to must be an integer, got true'
Expand Down
24 changes: 24 additions & 0 deletions test/option.to_line.coffee
Expand Up @@ -2,6 +2,30 @@
parse = require '../lib'

describe 'Option `to_line`', ->

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

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

0 comments on commit 8e085b7

Please sign in to comment.