Skip to content

Commit

Permalink
Implement numeric validation.
Browse files Browse the repository at this point in the history
Closes #130.
  • Loading branch information
flatheadmill committed Sep 15, 2015
1 parent b97fe29 commit 21dd91d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion invoke.js
Expand Up @@ -5,6 +5,8 @@ var util = require('util')
var slice = [].slice
var interrupt = require('./interrupt')

function isNumeric (n) { return !isNaN(parseFloat(n)) && isFinite(n) }

module.exports = cadence(function (async, source, env, argv, io, main) {
var options = {}

Expand Down Expand Up @@ -59,7 +61,11 @@ module.exports = cadence(function (async, source, env, argv, io, main) {
}
})
}
options.validate = function (test) {
options.numeric = function () {
this.validate.apply(this, [ '%s is not numeric' ].concat(slice.call(arguments))
.concat(isNumeric))
}
options.validate = function () {
var vargs = slice.call(arguments)
var format = vargs.shift()
var test = vargs.pop()
Expand Down
8 changes: 7 additions & 1 deletion t/arguable/run.t.js
Expand Up @@ -122,6 +122,12 @@ var prove = cadence(function (async, assert) {
}), function (error) {
assert(error.context.message, 'level is not an integer', 'validate')
})
run(path.join(__dirname, 'sub.js'), {}, [ 'run', '-p', 3, '-l', 'x' ], {
}, cadence(function (async, options) {
options.numeric('processes', 'level')
}), function (error) {
assert(error.context.message, 'level is not numeric', 'numeric')
})
run(__filename, {}, [], {
}, cadence(function (async, options) {
assert(options.format('ordered', 'this', 'that'), 'First that then this.', 'ordered format')
Expand All @@ -141,4 +147,4 @@ var prove = cadence(function (async, assert) {
io.events.emit('SIGINT')
})

require('proof')(24, prove)
require('proof')(25, prove)

0 comments on commit 21dd91d

Please sign in to comment.