Permalink
Browse files

copy in official underscore version. use mixin to add functions

  • Loading branch information...
1 parent c81e985 commit 0bd7c6196dab17f7aebfc14d2b03a13af1d6687a @ratbeard committed Mar 5, 2010
Showing with 251 additions and 374 deletions.
  1. +1 −1 test/flexargs.js
  2. +31 −33 underscore.flexargs.js
  3. +219 −340 underscore.js
View
@@ -146,7 +146,7 @@ $(document).ready(function() {
it('accepts a regex as a value in an object',
_.all(dogs, {name: /e/, age: 5}),
_.all(dogs, function (dog) { return /e/.test(dog.name) && dog.age === 5})
- );
+ );
});
test('collections: any', function() {
View
@@ -1,16 +1,18 @@
-flexargs = function (iterator, context) {
- return (typeof iterator === 'object') ? multicheck(iterator)
- : (typeof iterator === 'string') ? singlecheck(iterator, context)
- : iterator;
+(function () {
+
+var flexargs = function (iterator, context) {
+ return (typeof iterator === 'object') ? multicheck(iterator) // {keys:props}
+ : (typeof iterator === 'string') ? singlecheck(iterator, context) // key, prop
+ : iterator; // standard iterator fn
};
-singlecheck = function (key, value) {
+var singlecheck = function (key, value) {
return _.isRegExp(value) ?
function(item) { return value.test(item[key]);}
: function(item) { return item[key] === value; };
};
-multicheck = function (props) {
+var multicheck = function (props) {
var checks = _.map(props, function (value, key) {
return singlecheck(key, value);
});
@@ -21,36 +23,32 @@ multicheck = function (props) {
};
};
-
-var origfilter = _.filter;
-_.filter = function(obj, iterator, context) {
- return origfilter(obj, flexargs(iterator, context), context);
-};
-
-var origdetect = _.detect;
-_.detect = function(obj, iterator, context) {
- return origdetect(obj, flexargs(iterator, context), context);
+var makeflexy = function (name) {
+ var orig = _[name];
+ return function (obj, iterator, context) {
+ return orig(obj, flexargs(iterator, context), context);
+ };
};
-var origall = _.all;
-_.all = function (obj, iterator, context) {
- return origall(obj, flexargs(iterator, context), context);
-};
+_.mixin(
+ _.reduce(['filter','detect','any','all'], {}, function (memo, name) {
+ memo[name] = makeflexy(name);
+ return memo;
+ })
+);
-var origany = _.any;
-_.any = function (obj, iterator, context) {
- return origany(obj, flexargs(iterator, context), context);
-};
- var origmap = _.map;
-_.map = function(obj, iterator, context) {
- if (typeof iterator === 'string') {
- var match, key = iterator;
- iterator = (match=/(.*)\(\)$/.exec(key)) ?
- function (item) { return item[match[1]](); }
- : function (item) { return item[key]; };
+var origmap = _.map;
+_.mixin({
+ map: function(obj, iterator, context) {
+ if (typeof iterator === 'string') {
+ var match, key = iterator;
+ iterator = (match=/(.*)\(\)$/.exec(key)) ?
+ function (item) { return item[match[1]](); }
+ : function (item) { return item[key]; };
+ }
+ return origmap(obj, iterator, context);
}
- return origmap(obj, iterator, context);
-};
-
+})
+})();
Oops, something went wrong.

0 comments on commit 0bd7c61

Please sign in to comment.