Gathering detailed insights and metrics for @tiledesk/tiledesk-json-rules-engine
Gathering detailed insights and metrics for @tiledesk/tiledesk-json-rules-engine
Gathering detailed insights and metrics for @tiledesk/tiledesk-json-rules-engine
Gathering detailed insights and metrics for @tiledesk/tiledesk-json-rules-engine
npm install @tiledesk/tiledesk-json-rules-engine
Typescript
Module System
Node Version
NPM Version
v6.5.0
Updated on Nov 10, 2023
Support Facts in Events
Updated on Aug 23, 2023
6.3.0
Updated on Jul 13, 2023
6.2.0
Updated on Jul 03, 2023
v2.3.6
Updated on May 08, 2019
Use Array.isArray instead of instanceof to test Array parameters to address edge cases
Updated on Apr 26, 2019
JavaScript (97.53%)
TypeScript (2.24%)
Shell (0.24%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
ISC License
2,848 Stars
528 Commits
494 Forks
69 Watchers
5 Branches
27 Contributors
Updated on Jul 14, 2025
Latest Version
4.0.3
Package Id
@tiledesk/tiledesk-json-rules-engine@4.0.3
Size
16.20 kB
NPM Version
6.8.0
Node Version
11.10.0
Published on
Apr 09, 2020
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
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
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).
1import { Engine } from 'json-rules-engine' 2 3/** 4 * Setup a new engine 5 */ 6let engine = new Engine() 7 8// define a rule for detecting the player has exceeded foul limits. Foul out any player who: 9// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes) 10engine.addRule({ 11 conditions: { 12 any: [{ 13 all: [{ 14 fact: 'gameDuration', 15 operator: 'equal', 16 value: 40 17 }, { 18 fact: 'personalFoulCount', 19 operator: 'greaterThanInclusive', 20 value: 5 21 }] 22 }, { 23 all: [{ 24 fact: 'gameDuration', 25 operator: 'equal', 26 value: 48 27 }, { 28 fact: 'personalFoulCount', 29 operator: 'greaterThanInclusive', 30 value: 6 31 }] 32 }] 33 }, 34 event: { // define the event to fire when the conditions evaluate truthy 35 type: 'fouledOut', 36 params: { 37 message: 'Player has fouled out!' 38 } 39 } 40}) 41 42/** 43 * Define facts the engine will use to evaluate the conditions above. 44 * Facts may also be loaded asynchronously at runtime; see the advanced example below 45 */ 46let facts = { 47 personalFoulCount: 6, 48 gameDuration: 40 49} 50 51// Run the engine to evaluate 52engine 53 .run(facts) 54 .then(results => { 55 // 'results' is an object containing successful events, and an Almanac instance containing facts 56 results.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.
1import { Engine } from '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((results) => { 64 console.log(facts.accountId + ' is a ' + results.events.map(event => event.params.message)) 65 }) 66 .catch(err => console.log(err.stack)) 67 68/* 69 * OUTPUT: 70 * 71 * loading account information... // <-- API call is made ONCE and results recycled for all 3 conditions 72 * lincoln is a current microsoft employee taking christmas day off 73 */
This is available in the examples
The examples above provide a simple demonstrations of what json-rules-engine
can do. To learn more about the advanced features and techniques,
see the docs and read through the examples. There is also a walkthrough available.
Rules may be easily converted to JSON and persisted to a database, file system, or elsewhere. To convert a rule to JSON, simply call the rule.toJSON()
method. Later, a rule may be restored by feeding the json into the Rule constructor.
1// save somewhere...
2let jsonString = rule.toJSON()
3
4// ...later:
5let rule = new Rule(jsonString)
Why aren't "fact" methods persistable? This is by design, for several reasons. Firstly, facts are by definition business logic bespoke to your application, and therefore lie outside the scope of this library. Secondly, many times this request indicates a design smell; try thinking of other ways to compose the rules and facts to accomplish the same objective. Finally, persisting fact methods would involve serializing javascript code, and restoring it later via eval()
. If you have a strong desire for this feature, the node-rules project supports this (though be aware the capability is enabled via eval()
.
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'
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