Created
April 23, 2012 19:02
-
-
Save jstrimpel/2473110 to your computer and use it in GitHub Desktop.
underscore valid object chain and truth test
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
_.mixin({ | |
validChain: function (chain, test) { | |
var parts = chain.split('.'), valid = false, struct = window; | |
if (parts[0] === 'window') parts.splice(0, 1); | |
valid = _.all(parts, function (part) { return struct = struct[part]; }); | |
if (test && valid) { | |
if (typeof test === 'function') | |
return test(struct); | |
else | |
return struct === test; | |
} else { | |
return valid; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment