Overview
Simple util to provide the ability to evaluate targets based on simple evaluation conditions expressed as arrays. Primarily created so that external JSON files can express conditional expressions.
Example
The below example shows the simple filter expressions on a collection.
// Import evaluate from the library; // JSON definition of filterconst json = "filter": "all" "==" "type" "document" "in" "tags" "example" "other" ; // Example objectsconst objects = type: 'document' tags: 'example' src: 'https://alpacamaps/document1.json' type: 'document' tags: 'something' src: 'https://alpacamaps/document2.json' type: 'document' tags: 'other' src: 'https://alpacamaps/document3.json' type: 'image' tags: 'example' src: 'https://alpacamaps/image.png' ; // Match the objectsconst matched = objects;// Returns [// { type: "document", src: "https://alpacamaps/document1.json", tags: ["example"] },// { type: "document", src: "https://alpacamaps/document3.json", tags: ["other"] }// ]
Installation
You can add this using NPM
$ npm install filter-expressions
Supported Comparison Operators
- Existence: exists / exist / !exists / !exist / empty / !empty
- Comparison: == / != / > / >= / < / <=
- Membership: in / !in
- String Comparision: match
- Combining: all / any / none
- Geospatial comparisons: geo-within / geo-contains / geo-disjoint / geo-crosses / geo-overlap
Extensions
Custom Comparison Operators
You can provide your own comparison operator implementations.
; const options = comparisons: a === b ; // evaluate(expression, object, [options])const result = ; console; // Prints true
Custom Value Modifiers
You can implement your own value modifiers.
; const options = modifiers: !val ; // evaluate(expression, object, [options])const result = ; console; // Prints true
Custom Operators
You can implement custom operators to perform transformations
; const options = operators: array && arrayindex Numberval ; console; // Prints 4console; // Prints 4
Conversion of query
// Import asMongoDbQueryDocument from the library; // JSON definition of an expression that can run both on a local collectionconst json = "all" "==" "type" "document" "in" "tags" "example" "other"; // Convert the evaluation into a mongodb query document that can run on// a mongo db collectionconst query = ;
Notes
- Comparisons are done as using lodash "isEqual".
- Run npm t to run tests
- Inspired based on Mapbox style filters