Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
LICENSE | Sat Dec 27 17:53:17 -0800 2008 | |
| |
README.textile | Fri Jan 02 17:17:59 -0800 2009 | |
| |
jquery.enumerable.js | Sun Dec 28 19:27:20 -0800 2008 | |
| |
spec/ | Sun Dec 28 19:27:20 -0800 2008 | |
| |
vendor/ | Sat Dec 27 16:51:05 -0800 2008 |
jquery-enumerable
The only fully tested, API consistent enumerable plugin for jQuery
Quick start
<script src="jquery-1.2.6.js"></script>
<script src="jquery.enumerable.js"></script>
Usage
squares = $([1,2,3]).collect(function () {
return this * this;
} // => [1, 4, 9]
sum = $([1,2,3]).inject(0, function (accumulator) {
return accumulator + this;
} // => 6
// Sum is actually provided as a convenience function
$([1,2,3]).sum() // => 6
Every function is passed an index parameter
indexed_squares = $([1,2,3]).collect(function (index) {
return [index + 1, this * this];
} // => [[1, 1], [2, 4], [3, 9]]
The following functions are currently implemented
collect
inject
reject
select
all
any
sum
Architecture
These notes are for if you plan to extend the library.
Functions are defined in an object hash, which is subsequently used to extend both jQuery’s static functions and iterator functions. The function that you define must take the enumerable object as it’s first argument, and the callback as its last (for functions that don’t need a callback, such as sum, this is not required). This function is decorated with some basic error checking (such as ensuring the callback is valid). In code:
var methods = {
new_function: function(enumerable, callback) { }
}
// new_function is automatically wrapped in another function to provide error checking of the arguments
$.extend(methods) // Creates $.new_function
$.fn.extend(methods) // Creates $([]).new_function
Some test helpers are provided: expect_result and it_protects_from_invalid_callback. The ensures an error is raised if an invalid callback function is provided, the former runs the given spec against both the static ($.new_function) and iteraton ($([]).new_function) version of the function. See the existing suite for examples.








