Permalink
Browse files

move deepcompare into its own module

  • Loading branch information...
1 parent e2398d8 commit 1aef8ba058ace13323046ee424a316b03f05c167 @bronson committed Sep 21, 2011
Showing with 50 additions and 45 deletions.
  1. +42 −0 test/deepcompare.js
  2. +1 −0 test/valid.test.js
  3. +7 −45 test/validjson.test.js
View
@@ -0,0 +1,42 @@
+// deepcompare.js
+// Quickly compare two JavaScript values.
+//
+// can't use JSON.stringify for comparison because of key order problems
+// returns undefined if equal or a string describing the location of the difference.
+
+module.exports = function DeepCompare(path, a, b) {
+ if(typeof a !== typeof b) return path + ": " + (typeof a) + " vs " + (typeof b);
+ switch(typeof a) {
+ case 'string': case 'number': case 'boolean': case 'undefined':
+ if(a !== b) return path + ": " + a + " != " + b;
+ break;
+
+ case 'null': case 'function': case 'object':
+ if(a === null && b !== null) return path + ": should be null";
+ if(a !== null && b === null) return path + ": should not be null";
+ if(a instanceof Array) {
+ if(!(b instanceof Array)) return path + ": should be an Array";
+ if(a.length !== b.length) return path + " should be length " + a.length + " not " + b.length;
+ for(var i=0; i < a.length; i++) {
+ var iresult = DeepCompare(path+"["+i+"]", a[i], b[i]);
+ if(iresult) return iresult;
+ }
+ } else {
+ if(b instanceof Array) return path + ": should not be an Array";
+ for(var akey in a) {
+ if(!a.hasOwnProperty(akey)) continue;
+ if(!(akey in b)) return path + ": " + akey + " is missing";
+ var aresult = DeepCompare(path+"."+akey, a[akey], b[akey]);
+ if(aresult) return aresult;
+ }
+ for(var bkey in b) {
+ if(!b.hasOwnProperty(bkey)) continue;
+ if(!(bkey in a)) return path + ": " + bkey + " shouldn't exist";
+ }
+ }
+ break;
+
+ default: return path + ": what is a " + (typeof a) + "?";
+ }
+};
+
View
@@ -1,4 +1,5 @@
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) {
View
@@ -1,43 +1,5 @@
var Valid = require('../lib/validjson');
-
-// can't use JSON.stringify for comparison because of key order problems
-// returns undefined if equal or a string describing the location of the difference.
-function DeepCompare(path, a, b) {
- if(typeof a !== typeof b) return path + ": " + (typeof a) + " vs " + (typeof b);
- switch(typeof a) {
- case 'string': case 'number': case 'boolean': case 'undefined':
- if(a !== b) return path + ": " + a + " != " + b;
- break;
-
- case 'null': case 'function': case 'object':
- if(a === null && b !== null) return path + ": should be null";
- if(a !== null && b === null) return path + ": should not be null";
- if(a instanceof Array) {
- if(!(b instanceof Array)) return path + ": should be an Array";
- if(a.length !== b.length) return path + " should be length " + a.length + " not " + b.length;
- for(var i=0; i < a.length; i++) {
- var iresult = DeepCompare(path+"["+i+"]", a[i], b[i]);
- if(iresult) return iresult;
- }
- } else {
- if(b instanceof Array) return path + ": should not be an Array";
- for(var akey in a) {
- if(!a.hasOwnProperty(akey)) continue;
- if(!(akey in b)) return path + ": " + akey + " is missing";
- var aresult = DeepCompare(path+"."+akey, a[akey], b[akey]);
- if(aresult) return aresult;
- }
- for(var bkey in b) {
- if(!b.hasOwnProperty(bkey)) continue;
- if(!(bkey in a)) return path + ": " + bkey + " shouldn't exist";
- }
- }
- break;
-
- default: return path + ": what is a " + (typeof a) + "?";
- }
-}
-
+var DeepCompare = require('./deepcompare');
Valid.assert = function assert(value, expected) {
var actual = this.test(value);
@@ -53,25 +15,25 @@ Valid.assert = function assert(value, expected) {
// quick real-world scenario...
-var Address = { // schema for an address
+var AddressSchema = { // schema for an address
Address : Valid.array(Valid.string()).len(1,2),
CityName : Valid.notBlank(),
StateName : /^[A-Z][A-Z]$/,
PostalCode : /^[0-9]{5}(-[0-9]{4})?/,
CountryCode : "US"
};
-var Person = { // schema for a person
+var PersonSchema = { // schema for a person
// NamePrefixText : Valid.oneOf("", "Mr.", "Mrs.", "Ms.", "Dr."),
FirstName : Valid.notBlank(),
MiddleName : Valid.optional(Valid.string()),
LastName : Valid.notBlank(),
- HomeAddress : Address,
- WorkAddress : Address
-}
+ HomeAddress : AddressSchema,
+ WorkAddress : AddressSchema
+};
-Valid.json(Person).assert({ // and here is some data that matches Person
+Valid.json(PersonSchema).assert({ // and here is some data that matches Person
// NamePrefixText : '',
FirstName : 'Scott',
MiddleName : undefined,

0 comments on commit 1aef8ba

Please sign in to comment.