mol-proto
Advanced tools
Comparing version 0.1.7 to 0.1.8
{ | ||
"name": "proto", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"homepage": "https://github.com/MailOnline/proto", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -29,2 +29,3 @@ 'use strict'; | ||
* - [omitKeys](#omitKeys) | ||
* - [isEqual](#isEqual) | ||
* | ||
@@ -52,3 +53,4 @@ * All these methods can be [chained](proto.js.html#Proto) | ||
pickKeys: pickKeys, | ||
omitKeys: omitKeys | ||
omitKeys: omitKeys, | ||
isEqual: isEqual | ||
}; | ||
@@ -580,1 +582,40 @@ | ||
} | ||
/** | ||
* Performs deep equality test of the object. Does not work with recursive objects | ||
* @param {Any} self object to compare | ||
* @param {Any} obj object to compare | ||
* @return {Boolean} | ||
*/ | ||
function isEqual(obj) { | ||
if (this === obj) return this !== 0 || 1/this == 1/obj; // 0 and -0 are considered not equal, although 0 === -0 is true | ||
if (this == null || obj == null) return false; | ||
var className = this.constructor.name; | ||
if (className != obj.constructor.name) return false; | ||
switch (className) { | ||
case 'String': | ||
return this == String(obj); | ||
case 'Number': | ||
return this != +this ? obj != +obj : (this == 0 ? 1/this == 1/obj : this == +obj); | ||
case 'Date': | ||
case 'Boolean': | ||
return +this == +obj; | ||
case 'RegExp': | ||
return this.source == obj.source | ||
&& this.global == obj.global | ||
&& this.multiline == obj.multiline | ||
&& this.ignoreCase == obj.ignoreCase; | ||
} | ||
if (typeof this != 'object' || typeof obj != 'object') return false; | ||
if (Array.isArray(this)) | ||
return this.length == obj.length | ||
&& this.every(function(item, index) { | ||
return isEqual.call(item, obj[index]); | ||
}); | ||
else | ||
return everyKey.call(this, function(value, key) { | ||
return isEqual.call(value, obj[key]); | ||
}); | ||
} |
@@ -41,2 +41,3 @@ 'use strict'; | ||
* - [omitKeys](proto_object.js.html#omitKeys) | ||
* - [isEqual](proto_object.js.html#isEqual) | ||
*/ | ||
@@ -43,0 +44,0 @@ var objectMethods = require('./proto_object'); |
{ | ||
"name": "mol-proto", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "ES5 object manipulation library for node and modern browsers", | ||
@@ -5,0 +5,0 @@ "main": "lib/proto.js", |
@@ -82,2 +82,3 @@ proto | ||
* [omitKeys](http://mailonline.github.io/proto/proto_object.js.html#omitKeys) | ||
* [isEqual](http://mailonline.github.io/proto/proto_object.js.html#isEqual) | ||
@@ -84,0 +85,0 @@ * [__Array functions__](http://mailonline.github.io/proto/proto_array.js.html) |
@@ -678,2 +678,3 @@ 'use strict'; | ||
it('should define pickKeys function', function() { | ||
@@ -685,2 +686,3 @@ var obj = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }; | ||
it('should define omitKeys function', function() { | ||
@@ -691,2 +693,23 @@ var obj = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }; | ||
}); | ||
it('should define isEqual function', function() { | ||
var obj1 = { name: 'milo', info: { test: 1 } } | ||
, obj2 = { name: 'milo', info: { test: 1 } } | ||
, obj3 = { name: 'milo', info: { test: 2 } } | ||
, arr1 = [ 1, 2, [ 3, 4, { test: 5 } ] ] | ||
, arr2 = [ 1, 2, [ 3, 4, { test: 5 } ] ] | ||
, arr3 = [ 1, 2, [ 3, 4, { test: 6 } ] ]; | ||
assert(_.isEqual(null, null)); | ||
assert(_.isEqual(NaN, NaN)); | ||
assert(_.isEqual(undefined, undefined)); | ||
assert(! _.isEqual(0, -0)); | ||
assert(_.isEqual(obj1, obj2)); | ||
assert(! _.isEqual(obj1, obj3)); | ||
assert(_.isEqual(arr1, arr2)); | ||
assert(! _.isEqual(arr1, arr3)); | ||
assert(_.isEqual(/[a-c]/, /[a-c]/)); | ||
assert(! _.isEqual(/[a-c]/, /[a-c]/i)); | ||
}) | ||
}); |
174470
4227
136