Aggr
Object aggregation for node! This lib will essentially combine objects so long as they are overlapping within a single process tick.
Usage
Node.js
var aggr =
Browser
aggr(namespace, data, [options], callback)
namespace
- String namespace to prevent collisionsdata
- Data to aggregate, default type is object unless customiterator
is usedoptions
- Change the default behavior of aggr (optional)callback
- Invoked when aggregation is completed
options
extra
- custom callback to use in place of all callbacks after the first (default,false
)noop
- ignore all callbacks after the first (default,false
)iterator
- custom function to deal with each data aggregation (default,extend
)duration
- set a time duration for when to stop aggregating (default, 0)debounce
- if using duration, set to reset the timer each call (default,false
)
iterator
If using a custom iterator
, note that the very first argument will be undefined
.
The default iterator
is an object extend function, and it is assumed that objects
are being passed in unless this option is set.
var ns = 'adding' { // First execution, `a` === `undefined` return a || 0 + b} // This will be called 3 times with the final `total` { // total === `10`} var opt = iterator: add
extra and noop (no operation)
Sometimes only a single callback is needed, you can enable this behavior by passing
in noop
in the options, or an extra
function to handle this.
var ns = 'no-operations' opt = noop: true // This function will only be called once { // all === {one: 1, two: 2, three: 3}} // Uses the callback // No operation callback // No operation callback
Example
var ns = 'test' // This will be called 4 times, one for each aggr call { // combined data sent to callback // obj === {a: 'a', b: 'b', c: 10}}
Shortcut
You can create a shortcut method to use for aggregation by using aggr.wrap
,
this will return a function that takes a single data
argument. By default
it has noop
set to true, which seems to make more sense since you only pass
a callback once.
aggr.wrap(namespace, [options], callback)
// By default this will only be called once { // obj === {dark: 'coffee', is: 'good'}} var wrapped = aggr
Install
With npm
npm install aggr
License
(The MIT License)
Copyright (c) 2013 Beau Sorensen mail@beausorensen.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.