Recursively filters collections (arrays and objects)
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test CS. Jan 11, 2015
.editorconfig Update .editorconfig (made with screpto) Jan 2, 2016
.gitignore Initial commit. Jan 11, 2015
.jshintrc Initial commit. Jan 11, 2015
.travis.yml Bump version. Jan 2, 2016
LICENSE Initial commit. Jan 11, 2015
README.md Update README.md Jun 6, 2017
bower.json Bump version. Jan 2, 2016
index.js Initial commit. Jan 11, 2015
index.umd.js Browserify. Jan 2, 2016
package.json Release 1.0.2 Apr 3, 2017

README.md

deep-filter

NPM version Downloads Build Status Dependency status Dev Dependency status Greenkeeper badge

Recursively filters collections (arrays and objects).

Installation

$ npm install deep-filter - NPM
$ bower install deep-filter - bower

The browser file is named index.umd.js which supports CommonJS, AMD and globals (deepFilter). If you want to run this module on old browsers, you must include es5-shim.

Usage

Examples bellow are based on nodejs.

// Example 1 - Remove all strings equal to 'foo'
var deepfilter = require('deep-filter');

deepfilter({
    prop1: 'foo',
    prop2: ['foo', 'bar'],
    prop3: ['foo', 'foo'],
    prop4: {
        prop5: 'foo',
        prop6: 'bar'
    }
}, function (value, prop, subject) {
    // prop is an array index or an object key
    // subject is either an array or an object
    return value !== 'foo';
});

/*
{
    prop2: [ 'bar' ],
    prop3: [],
    prop4: {
        prop6: 'bar'
    }
}
 */

// Example 2 - Filter empty values
var deepfilter = require('deep-filter');

function notEmpty(value, prop, subject) {
    var key;

    if (Array.isArray(value)) {
        return value.length > 0;
    } else if (!!value && typeof value === 'object' && value.constructor === Object) {
        for (key in value) {
            return true;
        }

        return false;
    } else if (typeof value === 'string') {
        return value.length > 0;
    } else {
        return value != null;
    }
}

deepfilter({
    something: [
        {
            colors: ['red', 'green', ''],
            cars: { audi: 'nice', vw: 'good', aston: '' }
        },
        undefined,
        ''
    ],
    foo: 'bar'
}, notEmpty);

/*
{
    something: [
        {
            colors: ['red', 'green'],
            cars: { audi: 'nice', vw: 'good' }
        }
    ],
    foo: 'bar'
});
*/

// Example 3 - Filter empty values + trim strings
// Just to demonstrate that subject properties can be manipulated while filtering
var deepfilter = require('deep-filter');

function notEmpty(value, prop, subject) {
    var key;

    if (Array.isArray(value)) {
        return value.length > 0;
    } else if (!!value && typeof value === 'object' && value.constructor === Object) {
        for (key in value) {
            return true;
        }

        return false;
    } else if (typeof value === 'string') {
        subject[prop] = value = value.trim();

        return value.length > 0;
    } else {
        return value != null;
    }
}

deepfilter({
    something: [
        {
            colors: ['red', ' green ', ''],
            cars: { audi: 'nice', vw: 'good', aston: '  ' }
        },
        undefined,
        ''
    ],
    foo: 'bar'
}, notEmpty);

/*
{
    something: [
        {
            colors: ['red', 'green'],
            cars: { audi: 'nice', vw: 'good' }
        }
    ],
    foo: 'bar'
});
*/

Tests

$ npm test

License

Released under the MIT License.