Last active
December 17, 2015 15:19
-
-
Save ericvoid/5630507 to your computer and use it in GitHub Desktop.
Any and All methods in arrays (lists and collections) are handy and expressive. This gist is an implementation of them for JavaScript. The tests explains their usage.
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
(function (Array) { | |
var isFunction = function (value) { | |
var getType = {}; | |
return value && getType.toString.call(value) === '[object Function]'; | |
}; | |
var anyItemMeetsPredicate = function (items, predicate) { | |
for (var i=0; i < items.length; i++) { | |
if (predicate(items[i]) === true) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
/* | |
* .any(value) | |
* - value is object | |
* Checks if any item in the array equals the value | |
* | |
* .any(predicate) | |
* - predicate is function (receiving a value, returning a boolean) | |
* The predicate is applied to all elements. If the application of predicate on any element returns true, the result | |
* is true, otherwise the result is false. | |
*/ | |
Array.prototype.any = function (value) { | |
if (isFunction(value) === true) { | |
return anyItemMeetsPredicate(this, value); | |
} else { | |
return this.indexOf(value) > -1; | |
} | |
}; | |
var makePredicateFrom = function (value) { | |
if (isFunction(value) === true) { | |
return value; | |
} else { | |
return function (item) { return item == value; }; | |
} | |
}; | |
var allItemsMeetsPredicate = function(items, predicate) { | |
for (var i=0; i < items.length; i++) { | |
if (predicate(items[i]) === false) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
/* | |
* .all(value) | |
* - value is object | |
* Checks if all items in the array equals the value | |
* | |
* .all(predicate) | |
* - predicate is function (receiving a value, returning a boolean) | |
* The predicate is applied to all elements. If the application of predicate on all elements returns true, the result | |
* is true, otherwise the result is false. | |
*/ | |
Array.prototype.all = function (value) { | |
var predicate = makePredicateFrom(value); | |
return allItemsMeetsPredicate(this, predicate); | |
}; | |
})(Array); |
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
(function () { | |
var log = function (message) { | |
$('.log').append(message + "\n"); // DID YOU GET JQUERY? | |
}; | |
var assert = function (result, message) { | |
log(message + " : " + (result ? "pass" : "ERROR")); | |
}; | |
var even = function (n) { | |
return n % 2 == 0; | |
}; | |
assert([1, 9, 8, 0, 5].any(0), "[1, 9, 8, 0, 5] has a 0"); | |
assert([3, 0, 5, 7, 4].any(1) == false, "[3, 0, 5, 7, 4] has no 1"); | |
assert([1, 2, 3, 4, 5].any(even), "[1, 2, 3, 4, 5] has an even number"); | |
assert([1, 3, 5, 7, 9].any(even) == false, "[1, 3, 5, 7, 9] has no even number"); | |
assert([0, 0, 0, 0, 0].all(0), "in [0, 0, 0, 0, 0] all items are 0"); | |
assert([0, 0, 0, 0, 1].all(0) == false, "in [0, 0, 0, 0, 1] not all items are 0"); | |
assert([2, 4, 6, 8, 6].all(even), "in [2, 4, 6, 8, 6] all items are even"); | |
assert([2, 4, 6, 8, 7].all(even) == false, "in [2, 4, 6, 8, 7] not all items are even"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment