Created
October 29, 2011 01:22
-
-
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
This file contains 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({ | |
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