Array filtering inspired by MongoDB's query language.
gaia-filter is available on npm.
$ npm install gaia-filter
gaia-filter is available as a component.
$ component install qualiancy/gaia-filter
The primary export is a function that will memoize a query so that it can be used to filter many arrays. Furthermore, there are several methods to adjust the output of the result set.
The filter query language is similiar MongoDB's query language
in that it allows for nested statements, deep object property
detection, and conditionals such as
var filter = ;var dataComplex =a: b: 100c: 'testC'd:e: 'world'a: b: 50c: 'testC'd:e: 'universe'e: 'galaxy';var query1 = ;query2 = ;var res1 = query1 // result array will have the first itemres2 = query1; // result array will have the second item
Filter allows for some flexibility when composing queries by making assumptions based on sane defaults.
When a comparator is omitted when comparing a value, it assumes
// the following are the same.;;
When multiple statements are listed, it assumes
// the following are the same.;;;
Test a single data point against the query. Will return a boolean indicating whether data passes query criteria.
var query =pass = query;passshouldbetrue;
Test an array of data points against the query. Will return an array of all data points that pass the query criteria.
var query = $or : 'hello': true 'universe': trueq =result = q;resultshouldbeandhavelength2anddeep;
Test an array of data points against the query. Will return an array of boolean in the order of the original array that indicate if the data at that index passed the query criteria.
var query = $or : 'hello': true 'universe': trueq =result = q;resultshouldbeandhavelength4anddeep;
Test an array of data points against the query. Will return an array of numbers indicating the indexes of the original array that passed the query criteria.
var query = $or : 'hello': true 'universe': trueq =result = qindexhello: trueuniverse: falsehello: false universe: truehello: false universe: false;resultshouldbeandhavelength2anddeep;
The following are the comparators implemented
for use as part of a query. They can also be used
for simple comparation, such as
which would return
a is greater than
a is greater than or equal to
a is less than
a is less than or equal to
a contains at least all items in
Assert truthiness of
a mod (
a equals (
a does not equal (
a is in
a is not in
a has length of
a does not have a length.
a has at least one truthy value.
a has zero truthy values.
a has all truthy values.
(The MIT License)
Copyright (c) 2012 Jake Luer firstname.lastname@example.org (http://qualiancy.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.