Permalink
Browse files

added all(), first() and any() + map & grep aliases

  • Loading branch information...
1 parent acac678 commit db91b7863c457dfbb319d84d6acf32ac352e447d @mathieul committed Mar 24, 2009
Showing with 97 additions and 11 deletions.
  1. +45 −8 specs/lib/lib_spec.js
  2. +52 −3 src/jquery.lib.js
View
@@ -133,30 +133,67 @@
value_of($.lib.each).should_be($.each);
},
- "should inject an enumerable starting at 0 when called inject() without init": function () {
+ "should have $.lib.map() as an alias for $.map()": function () {
+ value_of($.lib.map).should_be($.map);
+ },
+
+ "should have $.lib.grep() as an alias for $.grep()": function () {
+ value_of($.lib.grep).should_be($.grep);
+ },
+
+ "should inject an enumerable starting at 0 when calling inject() without init": function () {
value_of($.lib.inject([1, 2, 3, 4, 5], function (mem, item) {
return mem + item;
})).should_be(15);
},
- "should inject an enumerable starting at init when called inject() with init": function () {
+ "should inject an enumerable starting at init when calling inject() with init": function () {
value_of($.lib.inject([10, 20, 30], 100, function (mem, item) {
return mem - item;
})).should_be(40);
- }
+ },
+
+ "should return all elements that match the condition when calling all()": function () {
+ value_of($.lib.all([5, 4, 3, 2, 1, 0],
+ function (i, item) { return item % 2 === 0; })).
+ should_be([4, 2, 0]);
+ },
- // TODO: all, any, map, first, select, grep, group_by, min, max, sort, compact
+ "should return the first element that matches the condition when calling first()": function () {
+ value_of($.lib.first([22, 33, 44],
+ function (i, item) { return item % 2 === 1; })).
+ should_be(33);
+
+ value_of($.lib.first([2, 4, 6, 8],
+ function (i, item) { return item % 2 === 1; })).
+ should_be(null);
+ },
+
+ "should return if any element matches the condition when calling any()": function () {
+ value_of($.lib.any([2, 4, 6, 8],
+ function (i, item) { return item % 2 === 0; })).
+ should_be(true);
+
+ value_of($.lib.any([2, 4, 6, 8],
+ function (i, item) { return item % 2 === 1; })).
+ should_be(false);
+ },
+
+ // TODO: select, group_by, min, max, sort, compact
+ "should ": function () {
+ value_of(true).should_be(true);
+ }
});
describe('Hash methods', {
- "should return an array of the object's keys when called keys()": function () {
+ "should return an array of the object's keys when calling keys()": function () {
value_of($.lib.keys({
hello: "Bonjour",
london: "Londres"})).
should_be(["hello", "london"]);
},
- "should return an array of the object's own keys when called keys() with flag set": function () {
+ "should return an array of the object's own keys when calling keys() with flag set": function () {
var ancestor = {
hello: "Bonjour",
london: "Londres"
@@ -168,14 +205,14 @@
value_of($.lib.keys(obj, true)).should_be(["one", "two", "three"]);
},
- "should return an array of the object's values when called values()": function () {
+ "should return an array of the object's values when calling values()": function () {
value_of($.lib.values({
hello: "Bonjour",
london: "Londres"})).
should_be(["Bonjour", "Londres"]);
},
- "should return an array of the object's own values when called values() with flag set": function () {
+ "should return an array of the object's own values when calling values() with flag set": function () {
var ancestor = {
hello: "Bonjour",
london: "Londres"
View
@@ -78,19 +78,19 @@
$.lib.is_undefined = function (val) { return typeof val === 'undefined' };
/*
- * inject: Combines the elements of enum by applying the block
+ * inject: Combines the elements of enumerable by applying the block
* to an accumulator value (memo) and each element in turn.
* At each step, memo is set to the value returned by the block.
* The first form lets you supply an initial value for memo.
* The second form uses the first element of the collection
* as a the initial value (and skips that element while iterating).
*/
- $.lib.inject = function (enum, memo, block) {
+ $.lib.inject = function (enumerable, memo, block) {
if (typeof block === 'undefined') {
block = memo;
memo = 0;
}
- $.each(enum, function (i, item) { memo = block(memo, item); });
+ $.each(enumerable, function (i, item) { memo = block(memo, item); });
return memo;
};
@@ -101,6 +101,17 @@
$.lib.each = $.each;
/*
+ * map: Calls block once for each element in self and return an array with
+ * the results
+ */
+ $.lib.map = $.map;
+
+ /*
+ * grep: TODO (same than all?)
+ */
+ $.lib.grep = $.grep;
+
+ /*
* keys: Returns an array with the keys of the object
*/
$.lib.keys = function (obj, own) {
@@ -129,4 +140,42 @@
}
return values;
};
+
+ /*
+ * all: Returns an array with the elements that match the condition
+ */
+ $.lib.all = function (enumerable, condition) {
+ var all = [],
+ key;
+
+ for (key in enumerable) {
+ if (enumerable.hasOwnProperty(key) && condition(key, enumerable[key])) {
+ all.push(enumerable[key]);
+ }
+ }
+ return all;
+ };
+
+ /*
+ * first: Returns the first enumerable element that matches the condition or null
+ * if none does
+ */
+ $.lib.first = function (enumerable, condition) {
+ var key;
+
+ for (key in enumerable) {
+ if (enumerable.hasOwnProperty(key) && condition(key, enumerable[key])) {
+ return enumerable[key];
+ }
+ }
+ return null;
+ };
+
+ /*
+ * any: Returns if any of the enumerable elements matches the condition
+ */
+ $.lib.any = function (enumerable, condition) {
+ return !!$.lib.first(enumerable, condition);
+ };
+
})(jQuery);

0 comments on commit db91b78

Please sign in to comment.