# reservoir

> Fast random sampling using reservoir sampling

Latest version **0.1.2** (published 2015-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install reservoir
pnpm add reservoir
yarn add reservoir
bun add reservoir
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2015-07-23 |
| First published | 2012-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/reservoir) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Jon-Carlos Rivera |
| Maintainers | imbcmdth |
| Keywords | reservoir, sampling, iterator |

## Links

- npm: https://www.npmjs.com/package/reservoir
- Repository: https://github.com/imbcmdth/reservoir
- Homepage: https://github.com/imbcmdth/reservoir#readme
- Issues: https://github.com/imbcmdth/reservoir/issues
- npm.io page: https://npm.io/package/reservoir

## Recent versions

- 0.1.2 (latest) — 2015-07-23
- 0.1.1 — 2012-08-21
- 0.1.0 — 2012-08-21

## README

# Reservoir
A simple JavaScript library for quickly generating a random subset of arrays, iterators, or streams using [reservoir sampling](http://en.wikipedia.org/wiki/Reservoir_sampling).

## Installation

### Node.js

For *Node.js*, use `npm`:

````console
npm install reservoir
````

..then `require` Reservoir:

````javascript
var Reservoir = require('reservoir');
````

### In the browser, traditional

For the *browser*, add the following to your pages:

````html
<script src="reservoir.js"></script>
````

### In the browser, using AMD (require.js)

...Or using AMD in the browser:

````javascript
require(["reservoir"], function(Reservoir) {
	// ...
});
````

## Usage

A reservoir is just an array with one very special function added - `pushSome`.

### Basic

To create an empty reservoir that will contain a *maximum* of 3 randomly-chosen items:

````javascript
var myReservoir = Reservoir(3); 
````

Now, using that new reservoir we begin to add items into it:

````javascript
var myData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

myData.forEach(function(e) {
	myReservoir.pushSome(e);
});
````

At this point, `myReservoir` will contain example 3 randomly-chosen values from the array `myData`:

````javascript
myReservoir => [2, 4, 7] // This can be any random subset of myData
````

As we can see, one important property of the Reservoir is that the order of the input is preserved.

### Intermediate

Since, `Reservoir.pushSome` operates much like the real `Array.push` we can pass `pushSome` more than one parameter and each one will be pushed in order. That means we can make the previous example more succinct by using `Function.apply`:

````javascript
myReservoir.pushSome.apply(myReservoir, myData);
````

Since the object returned from Reservoir() is just an array *decorated* with a `pushSome` function, all normal array functions are available. Particularly of value are the iterator functions: `forEach`, `filter`, `map`, `every`, `some`, `reduce`, and `reduceRight`.

### Advanced

TODO: Advanced usage (ie. see `Some`).

## API

### `Reservoir( reservoirSize = 1 [, randomNumberGenerator = Math.random] )`

#### Parameters

`reservoirSize` is the maximum size of the reservoir. This is the number of elements to be randomly chosen from the input provided to it using `pushSome`.

`randomNumberGenerator` is an optional random number generating function to use in place of the default `Math.random`. 

#### Returns

An empty Reservoir (an Array with the function `pushSome` added to it).

### `reservoir.pushSome( datum[, datum...])`

#### Parameters

`datum` one or more elements to consider for inclusion into the reservoir.

#### Returns 

The current length of the reservoir.

## License

> Copyright (C) 2012 Jon-Carlos Rivera
> 
> 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.

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