Last active
August 29, 2015 13:57
-
-
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.
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
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