🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@riddler/predicator

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@riddler/predicator

Safe predicate engine

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

Build Status Maintainability Test Coverage

Predicator

Predicator is a safe (does not eval code), admin or business user facing predicate engine. It turns a string predicate like "score > 600 or (score > 580 and monthly_income > 9000)" along with a supplied context into a true or false. This predicate can be stored as an attribute of a model (ex: an Offer model could store a predicate indicating if it is available to a customer).

Usage

Simple usage:

const Predicator = require('predicator');

// Compile a list of instructions which a Predicator Evaluator can execute

Predicator.compile('score >= 600 or (score > 580 and income > 9000)');

//  [
//    [ 'load', 'score' ],
//    [ 'to_int' ],
//    [ 'lit', 600 ],
//    [ 'compare', 'GTE' ],
//    [ 'jtrue', 11 ],
//    [ 'load', 'score' ],
//    [ 'to_int' ],
//    [ 'lit', 580 ],
//    [ 'compare', 'GT' ],
//    [ 'jfalse', 5 ],
//    [ 'load', 'income' ],
//    [ 'to_int' ],
//    [ 'lit', 9000 ],
//    [ 'compare', 'GT' ]
//  ]


// Evaluation

Predicator.evaluate('score >= 600 or (score > 580 and income > 9000)', {score: 600}); // true

Predicator.evaluate('score >= 600 or (score > 580 and income > 9000)', {score: 590}); // false

Predicator.evaluate('score >= 600 or (score > 580 and income > 9000)', {score: 590, income: 10000}); // true

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/riddler/predicator-js. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The library is available as open source under the terms of the MIT License.

FAQs

Package last updated on 08 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts