bucket-filter

1.0.1 • Public • Published

Bucket Filter

Build Status Dependency Status devDependency Status

Array filtering using buckets. Allows infinite buckets, bucket size limit, keeps input order and discards unmatched elements.

Get Started

npm install bucket-filter

API

filter(input, buckets[, this])

Returns new Array containing values filtered by buckets.

  • input: Array input. (Array)
  • buckets: Array of buckets: (Array)
    • condition: Evaluates bucket selection. Return boolean. (Function)
    • limit: Optional. Bucket size limit. (Number)
  • this: Optional. Value of this when executing each bucket condition. (Mixed)

Usage

// Note: This example uses ES6 syntax
var filter = require('bucket-filter')

var data = [
  { type: 1, title: 'foo-1-1' },
  // ...
  { type: 2, title: 'foo-2-1' },
  // ...
]

var buckets = [
  // Include max 2 values where type is 1
  { condition: (i) => i.type === 1, limit: 2 },
  // Include all values where type is 2
  { condition: (i) => i.type === 2 }
  // All values not matching any condition will be dismissed
]

filter(data, buckets)
// → []

Benchmark

node benchmark.js

Results

10x array / 1x bucket x 787,188 ops/sec ±7.22% (75 runs sampled)
10x array / 2x buckets x 586,677 ops/sec ±0.67% (87 runs sampled)
10x array / 5x buckets x 255,597 ops/sec ±1.06% (89 runs sampled)

License

See the License file.

Package Sidebar

Install

npm i bucket-filter

Weekly Downloads

38

Version

1.0.1

License

MIT

Unpacked Size

8.44 kB

Total Files

8

Last publish

Collaborators

  • grindcode