# pull-window

> Aggregate a pull-stream into windows.

Latest version **2.1.4** (published 2016-05-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install pull-window
pnpm add pull-window
yarn add pull-window
bun add pull-window
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.4 |
| Published | 2016-05-22 |
| First published | 2013-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Dominic Tarr |
| Maintainers | dominictarr |

## Links

- npm: https://www.npmjs.com/package/pull-window
- Repository: https://github.com/dominictarr/pull-window
- Issues: https://github.com/dominictarr/pull-window/issues
- npm.io page: https://npm.io/package/pull-window

## Dependencies (1)

- [looper](https://npm.io/package/looper.md) ^2.0.0

## Recent versions

- 2.1.4 (latest) — 2016-05-22
- 2.1.3 — 2015-09-27
- 2.1.2 — 2013-07-09
- 2.1.1 — 2013-07-09
- 2.1.0 — 2013-06-10
- 2.0.0 — 2013-05-31
- 1.0.3 — 2013-05-30
- 1.0.2 — 2013-04-05
- 1.0.1 — 2013-04-04
- 1.0.0 — 2013-04-03

## README

# 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.

``` js
var pull   = require('pull-stream')
var window = require('pull-window')

function everyTen () {
  var i = 0
  //window calls init with each data item,
  //and a callback to close that window.
  return window(function (data, cb) {
    //if you don't want to start a window here,
    //return undefined
    if(i != 0) return
    var sum = 0

    //else return a function.
    //this will be called all data
    //until you callback.
    return function (end, data) {
      if(end) return cb(null, sum)
      sum += data
      if(++i >= 10) {
        i = 0
        cb(null, sum)
      }
    }
  }
}

pull(
  pull.count(1000),
  everyTen(),
  pull.log()
)
```

## Example: variable sized window

Each window doesn't have to be the same size...

``` js
var pull   = require('pull-stream')
var window = require('pull-window')

function groupTo100 () {
  var sum = null
  return window(function (_, cb) {
    if(sum != null) return

    //sum stuff together until you have 100 or more
    return function (end, data) {
      if(end) return cb(null, sum)
      sum += data
      if(sum >= 100) {
        //copy sum like this, incase the next item
        //comes through sync
        var _sum = sum; sum = null
        cb(null, _sum)
      }
    }
  })
}

pull(
  pull.count(1000)
  groupTo100(),
  pull.log()
)
```

## Example: sliding window

to make more over lapping windows
just return the window function more often.

``` js
var pull   = require('pull-stream')
var window = require('pull-window')

function sliding () {
  return window(function (_, cb) {
    var sum = 0, i = 0

    //sum stuff together until you have 100 or more
    return function (end, data) {
      if(end) return cb(null, sum)
      sum += data
      if(++i >= 10) {
        //in this example, each window gets it's own sum,
        //so we don't need to copy it.
        cb(null, sum)
      }
    }
  })
}

pull(
  pull.count(100)
  sliding(),
  pull.log()
)
```


## API


### window (start, map)
``` js

window(function startWindow (data, cb) {

  //called on each chunk
  //including the first one
  return function addToWindow (end, data) {
    //cb(null, aggregate) when done.
  }
}, function mapWindow (start, data) {
  //(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

---
_Source: https://npm.io/package/pull-window · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
