pull-window
Aggregate a pull-stream into windows.
Several helpers are provided for particular types of windows, sliding, tumbling, etc.
And also, a low level
Example: "tumbling" window
sum every 10 items.
var pull =var window ={var i = 0//window calls init with each data item,//and a callback to close that window.return window {//if you don't want to start a window here,//return undefinedifi != 0 returnvar sum = 0//else return a function.//this will be called all data//until you callback.return {ifend returnsum += dataif++i >= 10i = 0}}}
Example: variable sized window
Each window doesn't have to be the same size...
var pull =var window ={var sum = nullreturn window {ifsum != null return//sum stuff together until you have 100 or morereturn {ifend returnsum += dataifsum >= 100//copy sum like this, incase the next item//comes through syncvar _sum = sum; sum = null}}}
Example: sliding window
to make more over lapping windows just return the window function more often.
var pull =var window ={return window {var sum = 0 i = 0//sum stuff together until you have 100 or morereturn {ifend returnsum += dataif++i >= 10//in this example, each window gets it's own sum,//so we don't need to copy it.}}}
API
window (start, map)
window {//called on each chunk//including the first onereturn {//cb(null, aggregate) when done.}} {//(optional)//map the window to something that tracks start, also}
By default, windows are mapped to {start: firstData, data: aggregate}
.
unless you pass in an different mapWindow
function.
window.sliding(reduce, size)
reduce every size
items into a single value, in a sliding window
window.recent(size, time)
tumbling window that groups items onto an array,
either every size
items, or within time
ms,
which ever occurs earliest.
License
MIT