Skip to content

thisandagain/strainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Simple filtering of arrays and object streams.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published