Skip to content
Simple filtering of arrays and object streams.
JavaScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
LICENSE.md
README.md
makefile
package.json

README.md

strainer

Simple filtering of arrays and object streams.

Build Status

Installation

npm install strainer

Basic Use (Array / Callback)

var strainer    = require('strainer');
var input       = [{'bar':'foo'}, {'foo':'bar'}];

strainer({
    input:  someArray,
    key:    'foo',
    value:  'bar'
}, function (err, result) {
    // [{foo: "bar"}]
});

Basic Use (Stream)

[
    {"bar": "foo"},
    {"foo": "bar"}
]
var strainer    = require('strainer');
var input       = require('fs').createReadStream('/some/array.json');

input.pipe(strainer({
    key:    'foo',
    value:  'bar'
})).pipe(process.stdout);

Using Functions as Values

If a function is provided as the "value" attribute, it will be used as a subroutine to evaluate each object. For example:

var strainer    = require('strainer');
var someArray   = [{foo: 1}, {foo: 2}, {foo: 3}, {foo: 4}];

strainer({
    input:  someArray,
    key:    'foo',
    value:  function (val) {
        return val > 2;
    }
}, function (err, result) {
    // [{foo: 3}, {foo: 4}]
});

Selectors

Strainer supports the use of JS-style object selectors. For example, let's say you had a complex object:

[
    {
        "level1": {
            "id": "something",
            "stuff": []
        },
        "level2": {
            "id": "somethingElse",
            "stuff": [
                {
                    "prop": true
                },
                {
                    "prop": false
                }
            ]
        }
    }
]

Here, you could use a selector to filter out a property that is buried deep within each object:

input.pipe(strainer({
    key:    'level2.stuff[0].prop',
    value:  true
})).pipe(process.stdout);

Testing

npm test

Naïve Benchmarks

make benchmark
Something went wrong with that request. Please try again.