I’ve found myself needing to apply an aggregate function over a jQuery object several times. I decided to wrap the functionality in a plugin. I attempted to make
$.aggregate and its little brother
Let’s take a look with an example:
1 2 3 4 5 6 7 8
Here, we’re building up a message from an array of numbers that correspond to letters of the alphabet. We supplied the aggregate function with a source and a function describing how we wanted to build the aggregate, based on the current value we’re iterating over and the “working” aggregate (what we’ve built up so far).
The conditional operator inside the aggregate function is kind of awkward, so lets supply an initial “seed” value to the aggregate:
1 2 3 4 5 6 7 8
Finally, we may want to supply some sort of final transformation function to our aggregated value. With the aggregate plugin, you can supply a function that will transform the aggregated value before returning it:
1 2 3 4 5 6 7 8 9 10
That’s pretty much it for
$.aggregate. You should be able to get pretty creative with it.
$.sum is just a convenient way to call
$.aggregate. After implementing
$.sum was pretty easy.
$.sum sheds some parameters, but is much more readable if all you’re doing is adding some values up:
1 2 3
Much neater right? You can also supply a transformation function that should return the item to be “summed”:
1 2 3 4 5 6 7 8 9 10 11 12 13
That’s pretty much it for the “static” functions. Both
$.aggregate can take an object or an array of values to aggregate.
There are also “instance” methods on jQuery objects. These methods operate on jQuery objects:
1 2 3 4 5 6 7 8 9 10 11 12
(Aggregate follows the same pattern).
I’m also toying with the idea of adding $.min and $.max.
If you want to download, view the source, or run the unit tests associated with the plugin, I have it up on Github. Minified version coming soon.