Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make equal take multiple arguments

  • Loading branch information...
commit 227e4902916a2194bc3f5115c498c72e3728d3a5 1 parent 1aef8ba
@bronson authored
Showing with 19 additions and 2 deletions.
  1. +14 −1 lib/valid.js
  2. +5 −1 test/valid.test.js
View
15 lib/valid.js
@@ -29,9 +29,22 @@ Valid.verify = function assert(value) {
Valid.nop = Valid.SimpleTest(function Nop(val) { });
Valid.fail = Valid.SimpleTest(function Fail(val,msg) { return msg || "failed"; });
-Valid.equal = Valid.SimpleTest(function Equal(val,want) { if(val !== want) return "is not equal to "+Valid.Escape(want); });
Valid.mod = Valid.SimpleTest(function mod(val,by,rem) { if(val%by !== (rem||0)) return "mod "+by+" is "+(val%by)+" not "+rem; });
+Valid.equal = Valid.SimpleTest(function Equal(value) {
+ // Here is the old equal, not sure supporting multiple values is really worth it:
+ // Valid.equal = Valid.SimpleTest(function Equal(val,want) { if(val !== want) return "is not equal to "+Valid.Escape(want); });
+ if(arguments.length == 1) return "equal needs at least one argument";
+ var opts = [];
+ for(i=1; i<arguments.length; i++) {
+ if(value === arguments[i]) return;
+ opts.push(Valid.Escape(arguments[i]));
+ }
+ if(arguments.length == 2) return "is not equal to " + opts[0];
+ var lastopt = opts.pop();
+ return "is not " + opts.join(", ") + " or " + lastopt;
+});
+
Valid.type = Valid.SimpleTest(function Type(value,type) {
if(typeof type !== 'string') return "type requires a string argument, not "+(typeof type);
if(typeof value !== type) return "is of type " + (typeof value) + " not " + type;
View
6 test/valid.test.js
@@ -1,5 +1,4 @@
var Valid = require('../lib/valid');
-var DeepCompare = require('./deepcompare');
// Like check() but throws if the result doesn't match the expectation
Valid.assert = function assert(value, expected) {
@@ -47,6 +46,7 @@ Valid.exists().assert(undefined, "does not exist");
// don't test Valid.noexisty() until the name doesn't suck
// equality
+Valid.equal().assert(undefined, "equal needs at least one argument");
Valid.equal(null).assert(null);
Valid.equal(null).assert(undefined, "is not equal to null");
Valid.equal("a").assert("a");
@@ -59,6 +59,10 @@ Valid.notEqual("a").assert(" ");
Valid.notEqual("a").assert("a", "is equal to 'a'");
Valid.notEqual(12).assert(13);
Valid.notEqual(12).assert(12, "is equal to 12");
+Valid.equal(undefined, null).assert(null);
+Valid.equal(1,2,3,4,5).assert(4);
+Valid.equal(1,2,3,4,5).assert(6, "is not 1, 2, 3, 4 or 5");
+Valid.equal("able","baker").assert(undefined, "is not 'able' or 'baker'");
// typeof
Valid.type('undefined').assert(undefined);
Please sign in to comment.
Something went wrong with that request. Please try again.