Skip to content

Instantly share code, notes, and snippets.

@aaronjorbin
Created October 29, 2011 01:22
Show Gist options
  • Save aaronjorbin/1323961 to your computer and use it in GitHub Desktop.
Save aaronjorbin/1323961 to your computer and use it in GitHub Desktop.
underscore.js mixin for checking if an array has a list of values or an object has a list of keys
_.mixin({
has_values: function( ){
var args = _.values(arguments),
list = args.shift(),
length = args.length;
if (_.isObject(list))
list = _.values(list);
return (_.intersection(list,args).length === length);
},
has_keys: function(){
var args = _.values(arguments),
list = args.shift(),
length = args.length;
if (_.isObject(list))
list = _.keys(list);
return (_.intersection(list,args).length === length);
}
});
// Tests and usage
// True
console.log(_.has_values([1,2,3,4], 2, 3));
console.log(_.has_values([1,2,3,4], 2));
console.log(_.has_values([1,2,3,4]));
console.log(_.has_values({a:1,b:2,c:3,d:4 }, 2,3));
// False
console.log(_.has_values([1,2,3,4], 2, 3, 5));
console.log(_.has_values({a:1,b:2,c:3,d:4 }, 2,3,5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment