Skip to content

Array filtering for Loopback inspired by MongoDB.

Notifications You must be signed in to change notification settings

BlueEastCode/loopback-advance-filters

 
 

Repository files navigation

Loopback Advance Filters Build Status

Filter javascript arrays using a MongoDB style syntax and is available for node.js and the browser. It was originally written as an internal component for Seed, but it had no other dependancies and seemed fit for use in the browser.

Installation

Node.js

Filtr is available on npm.

npm install loopback-advance-filters

Features

loopback-advance-filters is still in early development so expect this list to grow.

Expansive Query Language
  • Comparators: gt, gte, lt, lte, all, exists, mod, eq, ne, in, nin, size
  • Traversables: or, nor, and
Data Helpers
  • filtr.getPathValue returns the nested value in an object given a string path
  • filtr.setPathValue sets the nested value in an object given a string path
  • filtr.comparators are available directly for quick value testing

Usage

var query = filtr({ gt: 15, lt: 25 })
  , results = query.test([ 5, 10, 17, 19, 25 ]);
// results == [ 17, 19 ];

Test options provide different output

Testing also supports a number of options passed in as the second argument.

  • spec: output modifer
    • subset: (default) return an array containing a subset of matched items
    • boolean: return an array of the original length with each item being a boolean when object passed or failed.
    • index: return an array of numbers matching the index of passed object in the original array
  • type: input modifier
    • set: (default) assert that the data provided is an array. test each item.
    • single: assert that the data provided is a single item. return boolean.

Using the spec output modifier is an easy way to handle post processing of result sets without having to match up a subset.

var query = filtr({ gt: 15, lt: 25 })
  , results = query.test([ 5, 10, 17, 19, 25 ], { spec: 'boolean' });
// results == [ false, false, true, true, false ];

Using paths for deep matching

Filtr also supports using paths for deep matching within a javascript object. Given the following items, and sample queries.

var dataComplex = [
    { a: { b: 100 }
    , c: 'testC'
    , d: 
      [ { e: 'world' } ] 
    }
  , { a: { b: 50 }
    , c: 'testC'
    , d: 
      [ { e: 'universe' }
      , { e: 'galaxy' } ]
    }
];

var query1 = filtr({ 'a.b': { gt: 75, lt: 125 } });
  , query2 = filtr({ 'a.b': { gt: 25, lt: 75 }, 'd[0].e': { $eq: 'universe' } });

var res1 = query1.test(dataComplex)  // result would have the first item
  , res2 = query1.test(dataComplex); // result would have the second item 

A helper is also available that returns the value in a nested object given a string path.

var hello = filtr.getPathValue('d[0].e', dataComplex[1]);
// hello == 'universe'

Where to Get Help

Please post issues to GitHub Issues.

Tests

Tests are written in the BDD styles for the Mocha test runner using the should assertion interface from Chai. Running tests is simple:

make test

A browser suite is also available at test/browser/index.js. The same test definitions are used in both contexts.

Contributing

Interested in contributing? Fork to get started. Contact @BlueEastCode if you are interested in being regular contributor.

Forked From

License

(The MIT License)

About

Array filtering for Loopback inspired by MongoDB.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 95.7%
  • HTML 2.8%
  • Makefile 1.5%