Skip to content

Commit

Permalink
detect like in ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Oct 25, 2010
1 parent 33577aa commit 441f3b4
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ function Hash (hash, xs) {
});
return Hash(acc);
},
detect : function (f) {
for (var key in hash) {
if (f.call(self, hash[key], key)) {
return hash[key];
}
}
return undefined;
},
reduce : function (f, acc) {
var keys = Object.keys(hash);
if (acc === undefined) acc = keys.shift();
Expand Down Expand Up @@ -145,6 +153,10 @@ Hash.filter = function (ref, f) {
return Hash(ref).filter(f).items;
};

Hash.detect = function (ref, f) {
return Hash(ref).detect(f);
};

Hash.reduce = function (ref, f, acc) {
return Hash(ref).reduce(f, acc);
};
Expand Down

0 comments on commit 441f3b4

Please sign in to comment.