Permalink
Browse files

added either function, useful when dealing with filter on an array wi…

…th similar, but different objects
  • Loading branch information...
1 parent e4279fb commit b2bc657bc1c5f8ad673ca8030f44cd17a5a718c2 @boxxxie committed Feb 2, 2012
Showing with 33 additions and 3 deletions.
  1. +21 −1 test.js
  2. +12 −2 underscore_extended.js
View
22 test.js
@@ -46,7 +46,8 @@ join = _test("join"),
replace = _test("replace"),
matchTo = _test("matchTo"),
extend_r = _test("extend_r"),
-fill = _test("fill");
+fill = _test("fill"),
+either = _test("either");
pairs({a:'a',b:'b'})
([['a','a'],['b','b']])
@@ -435,4 +436,23 @@ fill(
upccode: "01123456"})
("complex real world object filling in a missing field");
+//--------------------------- either -------------------------
+
+either(undefined,undefined,null,0,1)
+(1)
+("simple list of false vals with the last one being the one returend");
+
+either()
+(undefined)
+("no args passed in");
+
+either(undefined, undefined)
+(undefined)
+("null args passed in");
+
+either(3, 1)
+(3)
+("non-false args passed in, return first");
+
+
console.log("tests finished");
View
@@ -376,8 +376,8 @@ _.mixin({
return _.chain(lists)
.groupBy(field)
.filter(function(val,key){
- return _.contains(fieldsToJoinOn,key);
- })
+ return _.contains(fieldsToJoinOn,key);
+ })
.mapMerge()
.flatten()
.value();
@@ -399,5 +399,15 @@ _.mixin({
return function(obj){
return _.has(obj,field);
};
+ },
+ filterHas:function(list,field){
+ return _.filter(list,_.has_F(field));
+ }
+ });
+
+_.mixin({
+ either:function(){
+ return _.chain(arguments).compact().first().value();
}
});
+

0 comments on commit b2bc657

Please sign in to comment.