Skip to content

Instantly share code, notes, and snippets.

@codelahoma
Last active August 29, 2015 13:57
Show Gist options
  • Save codelahoma/64529a2ab24d8eb529d7 to your computer and use it in GitHub Desktop.
Save codelahoma/64529a2ab24d8eb529d7 to your computer and use it in GitHub Desktop.
A little utility to extend underscore with a couple of methods I wanted, `isCollection` and `complement`, as well as `isNot*` functions to match the `is*` functions.
module.exports = (function () {
var _ = require('underscore');
var isCollection = function(item) {
return (_.isObject(item) || _.isArray(item) || _.isArguments(item));
};
var complement = function(fn) {
return function() {
return !fn.apply(null, _.toArray(arguments));
}
};
_.mixin({
isCollection: isCollection,
complement: complement
});
var isaFunctions = _.pick(_, _.chain(_)
.functions()
.filter(function(fn){return fn.match(/^is[A-Z]/)})
.value());
var opposites = _.reduce(isaFunctions, function(memo, f, fname) {
var name = fname.replace(/^is/,"isNot");
var func = complement(f);
memo[name] = func;
return memo;
}, {});
_.mixin(opposites);
return _;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment