Permalink
Browse files

handle regexps in deepcompare

  • Loading branch information...
1 parent 0ed2a63 commit 8e989b493daa5376311591900010afecc8f55e43 @bronson committed Oct 6, 2011
Showing with 12 additions and 4 deletions.
  1. +12 −4 test/deepcompare.js
View
@@ -1,8 +1,10 @@
// deepcompare.js
-// Quickly compare two JavaScript values.
+//
+// Quickly compare any two JavaScript values.
+// Returns a message describing where they differ.
//
// usage:
-// DeepCompare({a:1}, {a:1}) returns undefined meaning no differences
+// DeepCompare({a:1}, {a:1}) returns undefined, no differences
// DeepCompare({a:1}, {a:2}) returns a string describing the difference
//
// can't use JSON.stringify for comparison because of key order problems
@@ -18,9 +20,15 @@ module.exports = function DeepCompare(a, b, path) {
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(a instanceof RegExp) {
+ if(!(b instanceof RegExp)) return path + ": should be a RegExp";
+ if(actual.source !== expected.source || actual.global !== expected.global ||
+ actual.ignoreCase !== expected.ignoreCase || actual.multiline !== expected.multiline) {
+ return path + ": regexps differ";
+ }
+ } else 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;
+ 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(a[i], b[i], path+"["+i+"]");
if(iresult) return iresult;

0 comments on commit 8e989b4

Please sign in to comment.