Gathering detailed insights and metrics for json-rules-engine
Gathering detailed insights and metrics for json-rules-engine
Gathering detailed insights and metrics for json-rules-engine
Gathering detailed insights and metrics for json-rules-engine
npm install json-rules-engine
v6.5.0
Published on 10 Nov 2023
Support Facts in Events
Published on 23 Aug 2023
6.3.0
Published on 13 Jul 2023
6.2.0
Published on 03 Jul 2023
v2.3.6
Published on 08 May 2019
Use Array.isArray instead of instanceof to test Array parameters to address edge cases
Published on 26 Apr 2019
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,664 Stars
518 Commits
477 Forks
72 Watching
5 Branches
27 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
JavaScript (97.52%)
TypeScript (2.24%)
Shell (0.24%)
Cumulative downloads
Total Downloads
Last day
-6.4%
24,387
Compared to previous day
Last week
-0.5%
140,660
Compared to previous week
Last month
16.2%
591,033
Compared to previous month
Last year
41.2%
6,076,158
Compared to previous year
A rules engine expressed in JSON
json-rules-engine
is a powerful, lightweight rules engine. Rules are composed of simple json structures, making them human readable and easy to persist.
ALL
and ANY
boolean operators, including recursive nesting1$ npm install json-rules-engine
See the Examples, which demonstrate the major features and capabilities.
This example demonstrates an engine for detecting whether a basketball player has fouled out (a player who commits five personal fouls over the course of a 40-minute game, or six in a 48-minute game, fouls out).
1const { Engine } = require('json-rules-engine') 2 3 4/** 5 * Setup a new engine 6 */ 7let engine = new Engine() 8 9// define a rule for detecting the player has exceeded foul limits. Foul out any player who: 10// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes) 11engine.addRule({ 12 conditions: { 13 any: [{ 14 all: [{ 15 fact: 'gameDuration', 16 operator: 'equal', 17 value: 40 18 }, { 19 fact: 'personalFoulCount', 20 operator: 'greaterThanInclusive', 21 value: 5 22 }] 23 }, { 24 all: [{ 25 fact: 'gameDuration', 26 operator: 'equal', 27 value: 48 28 }, { 29 fact: 'personalFoulCount', 30 operator: 'greaterThanInclusive', 31 value: 6 32 }] 33 }] 34 }, 35 event: { // define the event to fire when the conditions evaluate truthy 36 type: 'fouledOut', 37 params: { 38 message: 'Player has fouled out!' 39 } 40 } 41}) 42 43/** 44 * Define facts the engine will use to evaluate the conditions above. 45 * Facts may also be loaded asynchronously at runtime; see the advanced example below 46 */ 47let facts = { 48 personalFoulCount: 6, 49 gameDuration: 40 50} 51 52// Run the engine to evaluate 53engine 54 .run(facts) 55 .then(({ events }) => { 56 events.map(event => console.log(event.params.message)) 57 }) 58 59/* 60 * Output: 61 * 62 * Player has fouled out! 63 */
This is available in the examples
This example demonstates an engine for identifying employees who work for Microsoft and are taking Christmas day off.
This demonstrates an engine which uses asynchronous fact data. Fact information is loaded via API call during runtime, and the results are cached and recycled for all 3 conditions. It also demonstates use of the condition path feature to reference properties of objects returned by facts.
1const { Engine } = require('json-rules-engine') 2 3// example client for making asynchronous requests to an api, database, etc 4import apiClient from './account-api-client' 5 6/** 7 * Setup a new engine 8 */ 9let engine = new Engine() 10 11/** 12 * Rule for identifying microsoft employees taking pto on christmas 13 * 14 * the account-information fact returns: 15 * { company: 'XYZ', status: 'ABC', ptoDaysTaken: ['YYYY-MM-DD', 'YYYY-MM-DD'] } 16 */ 17let microsoftRule = { 18 conditions: { 19 all: [{ 20 fact: 'account-information', 21 operator: 'equal', 22 value: 'microsoft', 23 path: '$.company' // access the 'company' property of "account-information" 24 }, { 25 fact: 'account-information', 26 operator: 'in', 27 value: ['active', 'paid-leave'], // 'status' can be active or paid-leave 28 path: '$.status' // access the 'status' property of "account-information" 29 }, { 30 fact: 'account-information', 31 operator: 'contains', // the 'ptoDaysTaken' property (an array) must contain '2016-12-25' 32 value: '2016-12-25', 33 path: '$.ptoDaysTaken' // access the 'ptoDaysTaken' property of "account-information" 34 }] 35 }, 36 event: { 37 type: 'microsoft-christmas-pto', 38 params: { 39 message: 'current microsoft employee taking christmas day off' 40 } 41 } 42} 43engine.addRule(microsoftRule) 44 45/** 46 * 'account-information' fact executes an api call and retrieves account data, feeding the results 47 * into the engine. The major advantage of this technique is that although there are THREE conditions 48 * requiring this data, only ONE api call is made. This results in much more efficient runtime performance 49 * and fewer network requests. 50 */ 51engine.addFact('account-information', function (params, almanac) { 52 console.log('loading account information...') 53 return almanac.factValue('accountId') 54 .then((accountId) => { 55 return apiClient.getAccountInformation(accountId) 56 }) 57}) 58 59// define fact(s) known at runtime 60let facts = { accountId: 'lincoln' } 61engine 62 .run(facts) 63 .then(({ events }) => { 64 console.log(facts.accountId + ' is a ' + events.map(event => event.params.message)) 65 }) 66 67/* 68 * OUTPUT: 69 * 70 * loading account information... // <-- API call is made ONCE and results recycled for all 3 conditions 71 * lincoln is a current microsoft employee taking christmas day off 72 */
This is available in the examples
To see what the engine is doing under the hood, debug output can be turned on via:
1DEBUG=json-rules-engine
1// set debug flag in local storage & refresh page to see console output 2localStorage.debug = 'json-rules-engine'
https://github.com/vinzdeveloper/json-rule-editor - configuration ui for json-rules-engine:
No vulnerabilities found.
Reason
no vulnerabilities detected
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
dependency not pinned by hash detected -- score normalized to 9
Details
Reason
GitHub code reviews found for 9 commits out of the last 30 -- score normalized to 3
Details
Reason
0 commit(s) out of 30 and 0 issue activity out of 30 found in the last 90 days -- score normalized to 0
Reason
no badge detected
Reason
non read-only tokens detected in GitHub workflows
Details
Reason
security policy file not detected
Reason
no update tool detected
Details
Reason
project is not fuzzed
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2022-08-15
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More