Skip to content

Instantly share code, notes, and snippets.

@jstrimpel
Created April 23, 2012 19:02
Show Gist options
  • Save jstrimpel/2473110 to your computer and use it in GitHub Desktop.
Save jstrimpel/2473110 to your computer and use it in GitHub Desktop.
underscore valid object chain and truth test
_.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