Last active
December 17, 2015 06:19
-
-
Save ryasmi/5564185 to your computer and use it in GitHub Desktop.
Determines if an object resembles another object (obj). If x resembles y, then x contains some of y's attributes. xRy => x.keys (subset of) y.keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using func. https://gist.github.com/ryansmith94/5564105 | |
func("resembles", function (obj) { | |
var self = this; | |
var keys = Object.keys(self); | |
var len = keys.length; | |
var key = 0; | |
return (function check() { | |
if (self[keys[key]] === obj[keys[key]]) { | |
key += 1; | |
return (key <= len) ? check() : true; | |
} | |
return false; | |
}()); | |
}, Object.prototype); | |
// Test code. | |
var testResembles = function () { | |
var x = {"a": 10, "b": 3}; | |
if (x.resembles({"a": 10}) !== false) {console.log("Test 1 failed."); } | |
if (x.resembles({"a": 10, "b": 4}) !== false) {console.log("Test 2 failed."); } | |
if (x.resembles({"a": 10, "b": 3}) !== true) {console.log("Test 3 failed."); } | |
if (x.resembles({"a": 10, "b": 3, "c": 1}) !== true) {console.log("Test 4 failed."); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment