@shepherdnerds/json-rules-engine 7.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.babelrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "presets": ["es2015", "stage-0"]
3
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "WebFetch(domain:docs.commercelayer.io)",
5
+ "Bash(npm test:*)",
6
+ "Bash(npm install:*)",
7
+ "Bash(npm run build:*)",
8
+ "Bash(pnpm install)",
9
+ "Bash(git log:*)",
10
+ "Bash(pnpm ts-node:*)",
11
+ "Bash(pnpm dev:fireball:*)",
12
+ "Bash(git remote set-url:*)",
13
+ "Bash(grep:*)",
14
+ "Bash(git merge:*)",
15
+ "Bash(git push:*)",
16
+ "Bash(pnpm publish:*)",
17
+ "WebFetch(domain:raw.githubusercontent.com)",
18
+ "Bash(git add:*)",
19
+ "Bash(git commit -m \"$\\(cat <<''EOF''\nconfigure package for GitHub Packages publishing\n\n- Rename package to @withshepherd/nested-json-rules-engine\n- Add publishConfig for GitHub Packages registry\n- Update deploy workflow to publish on release \\(not push\\)\n- Update repository URLs to point to correct repo\n\nCo-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>\nEOF\n\\)\")",
20
+ "Bash(gh run list:*)",
21
+ "Bash(pnpm install:*)"
22
+ ]
23
+ }
24
+ }
@@ -0,0 +1,45 @@
1
+ # Build, test, and publish to GitHub Packages on release
2
+
3
+ name: Deploy Package
4
+
5
+ on:
6
+ release:
7
+ types: [created]
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+
13
+ strategy:
14
+ matrix:
15
+ node-version: [18.x, 20.x, 22.x]
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - name: Use Node.js ${{ matrix.node-version }}
20
+ uses: actions/setup-node@v4
21
+ with:
22
+ node-version: ${{ matrix.node-version }}
23
+ - run: npm install
24
+ - run: npm run build --if-present
25
+ - run: npm run lint
26
+ - run: npm test
27
+
28
+ publish:
29
+ needs: build
30
+ runs-on: ubuntu-latest
31
+ permissions:
32
+ contents: read
33
+ packages: write
34
+ steps:
35
+ - uses: actions/checkout@v4
36
+ - uses: actions/setup-node@v4
37
+ with:
38
+ node-version: 20.x
39
+ registry-url: 'https://npm.pkg.github.com'
40
+ scope: '@withshepherd'
41
+ - run: npm install
42
+ - run: npm run build --if-present
43
+ - run: npm publish
44
+ env:
45
+ NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,28 @@
1
+ # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
+
4
+ name: Node.js CI
5
+
6
+ on:
7
+ pull_request:
8
+ branches: [ master, v6 ]
9
+
10
+ jobs:
11
+ build:
12
+
13
+ runs-on: ubuntu-latest
14
+
15
+ strategy:
16
+ matrix:
17
+ node-version: [18.x, 20.x, 22.x]
18
+
19
+ steps:
20
+ - uses: actions/checkout@v2
21
+ - name: Use Node.js ${{ matrix.node-version }}
22
+ uses: actions/setup-node@v1
23
+ with:
24
+ node-version: ${{ matrix.node-version }}
25
+ - run: npm install
26
+ - run: npm run build --if-present
27
+ - run: npm run lint
28
+ - run: npm test
package/CHANGELOG.md ADDED
@@ -0,0 +1,167 @@
1
+ #### 6.1.0 / 2021-06-03
2
+ * engine.removeRule() now supports removing rules by name
3
+ * Added engine.updateRule(rule)
4
+
5
+ #### 6.0.1 / 2021-03-09
6
+ * Updates Typescript types to include `failureEvents` in EngineResult.
7
+
8
+ #### 6.0.0 / 2020-12-22
9
+ * BREAKING CHANGES
10
+ * To continue using [selectn](https://github.com/wilmoore/selectn.js) syntax for condition `path`s, use the new `pathResolver` feature. Read more [here](./docs/rules.md#condition-helpers-custom-path-resolver). Add the following to the engine constructor:
11
+ ```js
12
+ const pathResolver = (object, path) => {
13
+ return selectn(path)(object)
14
+ }
15
+ const engine = new Engine(rules, { pathResolver })
16
+ ```
17
+ (fixes #205)
18
+ * Engine and Rule events `on('success')`, `on('failure')`, and Rule callbacks `onSuccess` and `onFailure` now honor returned promises; any event handler that returns a promise will be waited upon to resolve before engine execution continues. (fixes #235)
19
+ * Private `rule.event` property renamed. Use `rule.getEvent()` to avoid breaking changes in the future.
20
+ * The `success-events` fact used to store successful events has been converted to an internal data structure and will no longer appear in the almanac's facts. (fixes #187)
21
+ * NEW FEATURES
22
+ * Engine constructor now accepts a `pathResolver` option for resolving condition `path` properties. Read more [here](./docs/rules.md#condition-helpers-custom-path-resolver). (fixes #210)
23
+ * Engine.run() now returns three additional data structures:
24
+ * `failureEvents`, an array of all failed rules events. (fixes #192)
25
+ * `results`, an array of RuleResults for each successful rule (fixes #216)
26
+ * `failureResults`, an array of RuleResults for each failed rule
27
+
28
+
29
+ #### 5.3.0 / 2020-12-02
30
+ * Allow facts to have a value of `undefined`
31
+
32
+ #### 5.2.0 / 2020-11-31
33
+ * No changes; published to correct an accidental publish of untagged alpha
34
+
35
+ #### 5.0.4 / 2020-09-26
36
+ * Upgrade dependencies to latest
37
+
38
+ #### 5.0.3 / 2020-01-26
39
+ * Upgrade jsonpath-plus dependency, to fix inconsistent scalar results (#175)
40
+
41
+ #### 5.0.2 / 2020-01-18
42
+ * BUGFIX: Add missing `DEBUG` log for almanac.addRuntimeFact()
43
+
44
+ #### 5.0.1 / 2020-01-18
45
+ * BUGFIX: `DEBUG` envs works with cookies disables
46
+
47
+ #### 5.0.0 / 2019-11-29
48
+ * BREAKING CHANGES
49
+ * Rule conditions' `path` property is now interpreted using [json-path](https://goessner.net/articles/JsonPath/)
50
+ * To continue using the old syntax (provided via [selectn](https://github.com/wilmoore/selectn.js)), `npm install selectn` as a direct dependency, and `json-rules-engine` will continue to interpret legacy paths this way.
51
+ * Any path starting with `$` will be assumed to use `json-path` syntax
52
+
53
+ #### 4.1.0 / 2019-09-27
54
+ * Export Typescript definitions (@brianphillips)
55
+
56
+ #### 4.0.0 / 2019-08-22
57
+ * BREAKING CHANGES
58
+ * `engine.run()` now returns a hash of events and almanac: `{ events: [], almanac: Almanac instance }`. Previously in v3, the `run()` returned the `events` array.
59
+ * For example, `const events = await engine.run()` under v3 will need to be changed to `const { events } = await engine.run()` under v4.
60
+
61
+ #### 3.1.0 / 2019-07-19
62
+ * Feature: `rule.setName()` and `ruleResult.name`
63
+
64
+ #### 3.0.3 / 2019-07-15
65
+ * Fix "localStorage.debug" not working in browsers
66
+
67
+ #### 3.0.2 / 2019-05-23
68
+ * Fix "process" not defined error in browsers lacking node.js global shims
69
+
70
+ #### 3.0.0 / 2019-05-17
71
+ * BREAKING CHANGES
72
+ * Previously all conditions with undefined facts would resolve false. With this change, undefined facts values are treated as `undefined`.
73
+ * Greatly improved performance of `allowUndefinedfacts = true` engine option
74
+ * Reduce package bundle size by ~40%
75
+
76
+ #### 2.3.5 / 2019-04-26
77
+ * Replace debug with vanilla console.log
78
+
79
+ #### 2.3.4 / 2019-04-26
80
+ * Use Array.isArray instead of instanceof to test Array parameters to address edge cases
81
+
82
+ #### 2.3.3 / 2019-04-23
83
+ * Fix rules cache not clearing after removeRule()
84
+
85
+ #### 2.3.2 / 2018-12-28
86
+ * Upgrade all dependencies to latest
87
+
88
+ #### 2.3.1 / 2018-12-03
89
+ * IE8 compatibility: replace Array.forEach with for loop (@knalbandianbrightgrove)
90
+
91
+ #### 2.3.0 / 2018-05-03
92
+ * Engine.removeFact() - removes fact from the engine (@SaschaDeWaal)
93
+ * Engine.removeRule() - removes rule from the engine (@SaschaDeWaal)
94
+ * Engine.removeOperator() - removes operator from the engine (@SaschaDeWaal)
95
+
96
+ #### 2.2.0 / 2018-04-19
97
+ * Performance: Constant facts now perform 18-26X better
98
+ * Performance: Removes await/async transpilation and json.stringify calls, significantly improving overall performance
99
+
100
+ #### 2.1.0 / 2018-02-19
101
+ * Publish dist updates for 2.0.3
102
+
103
+ #### 2.0.3 / 2018-01-29
104
+ * Add factResult and result to the JSON generated for Condition (@bjacobso)
105
+
106
+ #### 2.0.2 / 2017-07-24
107
+ * Bugfix IE8 support
108
+
109
+ #### 2.0.1 / 2017-07-05
110
+ * Bugfix rule result serialization
111
+
112
+ #### 2.0.0 / 2017-04-21
113
+ * Publishing 2.0.0
114
+
115
+ #### 2.0.0-beta2 / 2017-04-10
116
+ * Fix fact path object checking to work with objects that have prototypes (lodash isObjectLike instead of isPlainObject)
117
+
118
+ #### 2.0.0-beta1 / 2017-04-09
119
+ * Add rule results
120
+ * Document fact .path ability to parse properties containing dots
121
+ * Bump dependencies
122
+ * BREAKING CHANGES
123
+ * `engine.on('failure', (rule, almanac))` is now `engine.on('failure', (event, almanac, ruleResult))`
124
+ * `engine.on(eventType, (eventParams, engine))` is now `engine.on(eventType, (eventParams, almanac, ruleResult))`
125
+
126
+ #### 1.5.1 / 2017-03-19
127
+ * Bugfix almanac.factValue skipping interpreting condition "path" for cached facts
128
+
129
+ #### 1.5.0 / 2017-03-12
130
+ * Add fact comparison conditions
131
+
132
+ #### 1.4.0 / 2017-01-23
133
+ * Add `allowUndefinedFacts` engine option
134
+
135
+ #### 1.3.1 / 2017-01-16
136
+ * Bump object-hash dependency to latest
137
+
138
+ #### 1.3.0 / 2016-10-24
139
+ * Rule event emissions
140
+ * Rule chaining
141
+
142
+ #### 1.2.1 / 2016-10-22
143
+ * Use Array.indexOf instead of Array.includes for older node version compatibility
144
+
145
+ #### 1.2.0 / 2016-09-13
146
+ * Fact path support
147
+
148
+ #### 1.1.0 / 2016-09-11
149
+ * Custom operator support
150
+
151
+ #### 1.0.4 / 2016-06-18
152
+ * fix issue #6; runtime facts unique to each run()
153
+
154
+ #### 1.0.3 / 2016-06-15
155
+ * fix issue #5; dependency error babel-core/register
156
+
157
+ #### 1.0.0 / 2016-05-01
158
+ * api stable; releasing 1.0
159
+ * engine.run() now returns triggered events
160
+
161
+ #### 1.0.0-beta10 / 2016-04-16
162
+ * Completed the 'fact-dependecy' advanced example
163
+ * Updated addFact and addRule engine methods to return 'this' for easy chaining
164
+
165
+ #### 1.0.0-beta9 / 2016-04-11
166
+ * Completed the 'basic' example
167
+ * [BREAKING CHANGE] update engine.on('success') and engine.on('failure') to pass the current almanac instance as the second argument, rather than the engine
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2017 Cache Hamm
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14
+ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,235 @@
1
+ ![json-rules-engine](http://i.imgur.com/MAzq7l2.png)
2
+ [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
3
+ [![Build Status](https://github.com/cachecontrol/json-rules-engine/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/cachecontrol/json-rules-engine/workflows/Node.js%20CI/badge.svg?branch=master)
4
+
5
+ [![npm version](https://badge.fury.io/js/json-rules-engine.svg)](https://badge.fury.io/js/json-rules-engine)
6
+ [![install size](https://packagephobia.now.sh/badge?p=json-rules-engine)](https://packagephobia.now.sh/result?p=json-rules-engine)
7
+ [![npm downloads](https://img.shields.io/npm/dm/json-rules-engine.svg)](https://www.npmjs.com/package/json-rules-engine)
8
+
9
+ A rules engine expressed in JSON
10
+
11
+ * [Synopsis](#synopsis)
12
+ * [Features](#features)
13
+ * [Installation](#installation)
14
+ * [Docs](#docs)
15
+ * [Examples](#examples)
16
+ * [Basic Example](#basic-example)
17
+ * [Advanced Example](#advanced-example)
18
+ * [Debugging](#debugging)
19
+ * [Node](#node)
20
+ * [Browser](#browser)
21
+ * [Related Projects](#related-projects)
22
+ * [License](#license)
23
+
24
+ ## Synopsis
25
+
26
+ ```json-rules-engine``` is a powerful, lightweight rules engine. Rules are composed of simple json structures, making them human readable and easy to persist.
27
+
28
+ ## Features
29
+
30
+ * Rules expressed in simple, easy to read JSON
31
+ * Full support for ```ALL``` and ```ANY``` boolean operators, including recursive nesting
32
+ * Fast by default, faster with configuration; priority levels and cache settings for fine tuning performance
33
+ * Secure; no use of eval()
34
+ * Isomorphic; runs in node and browser
35
+ * Lightweight & extendable; 17kb gzipped w/few dependencies
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ $ npm install json-rules-engine
41
+ ```
42
+
43
+ ## Docs
44
+
45
+ - [engine](./docs/engine.md)
46
+ - [rules](./docs/rules.md)
47
+ - [almanac](./docs/almanac.md)
48
+ - [facts](./docs/facts.md)
49
+
50
+ ## Examples
51
+
52
+ See the [Examples](./examples), which demonstrate the major features and capabilities.
53
+
54
+ ## Basic Example
55
+
56
+ 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).
57
+
58
+ ```js
59
+ const { Engine } = require('json-rules-engine')
60
+
61
+
62
+ /**
63
+ * Setup a new engine
64
+ */
65
+ let engine = new Engine()
66
+
67
+ // define a rule for detecting the player has exceeded foul limits. Foul out any player who:
68
+ // (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
69
+ engine.addRule({
70
+ conditions: {
71
+ any: [{
72
+ all: [{
73
+ fact: 'gameDuration',
74
+ operator: 'equal',
75
+ value: 40
76
+ }, {
77
+ fact: 'personalFoulCount',
78
+ operator: 'greaterThanInclusive',
79
+ value: 5
80
+ }]
81
+ }, {
82
+ all: [{
83
+ fact: 'gameDuration',
84
+ operator: 'equal',
85
+ value: 48
86
+ }, {
87
+ fact: 'personalFoulCount',
88
+ operator: 'greaterThanInclusive',
89
+ value: 6
90
+ }]
91
+ }]
92
+ },
93
+ event: { // define the event to fire when the conditions evaluate truthy
94
+ type: 'fouledOut',
95
+ params: {
96
+ message: 'Player has fouled out!'
97
+ }
98
+ }
99
+ })
100
+
101
+ /**
102
+ * Define facts the engine will use to evaluate the conditions above.
103
+ * Facts may also be loaded asynchronously at runtime; see the advanced example below
104
+ */
105
+ let facts = {
106
+ personalFoulCount: 6,
107
+ gameDuration: 40
108
+ }
109
+
110
+ // Run the engine to evaluate
111
+ engine
112
+ .run(facts)
113
+ .then(({ events }) => {
114
+ events.map(event => console.log(event.params.message))
115
+ })
116
+
117
+ /*
118
+ * Output:
119
+ *
120
+ * Player has fouled out!
121
+ */
122
+ ```
123
+
124
+ This is available in the [examples](./examples/02-nested-boolean-logic.js)
125
+
126
+ ## Advanced Example
127
+
128
+ This example demonstates an engine for identifying employees who work for Microsoft and are taking Christmas day off.
129
+
130
+ This demonstrates an engine which uses asynchronous fact data.
131
+ Fact information is loaded via API call during runtime, and the results are cached and recycled for all 3 conditions.
132
+ It also demonstates use of the condition _path_ feature to reference properties of objects returned by facts.
133
+
134
+ ```js
135
+ const { Engine } = require('json-rules-engine')
136
+
137
+ // example client for making asynchronous requests to an api, database, etc
138
+ import apiClient from './account-api-client'
139
+
140
+ /**
141
+ * Setup a new engine
142
+ */
143
+ let engine = new Engine()
144
+
145
+ /**
146
+ * Rule for identifying microsoft employees taking pto on christmas
147
+ *
148
+ * the account-information fact returns:
149
+ * { company: 'XYZ', status: 'ABC', ptoDaysTaken: ['YYYY-MM-DD', 'YYYY-MM-DD'] }
150
+ */
151
+ let microsoftRule = {
152
+ conditions: {
153
+ all: [{
154
+ fact: 'account-information',
155
+ operator: 'equal',
156
+ value: 'microsoft',
157
+ path: '$.company' // access the 'company' property of "account-information"
158
+ }, {
159
+ fact: 'account-information',
160
+ operator: 'in',
161
+ value: ['active', 'paid-leave'], // 'status' can be active or paid-leave
162
+ path: '$.status' // access the 'status' property of "account-information"
163
+ }, {
164
+ fact: 'account-information',
165
+ operator: 'contains', // the 'ptoDaysTaken' property (an array) must contain '2016-12-25'
166
+ value: '2016-12-25',
167
+ path: '$.ptoDaysTaken' // access the 'ptoDaysTaken' property of "account-information"
168
+ }]
169
+ },
170
+ event: {
171
+ type: 'microsoft-christmas-pto',
172
+ params: {
173
+ message: 'current microsoft employee taking christmas day off'
174
+ }
175
+ }
176
+ }
177
+ engine.addRule(microsoftRule)
178
+
179
+ /**
180
+ * 'account-information' fact executes an api call and retrieves account data, feeding the results
181
+ * into the engine. The major advantage of this technique is that although there are THREE conditions
182
+ * requiring this data, only ONE api call is made. This results in much more efficient runtime performance
183
+ * and fewer network requests.
184
+ */
185
+ engine.addFact('account-information', function (params, almanac) {
186
+ console.log('loading account information...')
187
+ return almanac.factValue('accountId')
188
+ .then((accountId) => {
189
+ return apiClient.getAccountInformation(accountId)
190
+ })
191
+ })
192
+
193
+ // define fact(s) known at runtime
194
+ let facts = { accountId: 'lincoln' }
195
+ engine
196
+ .run(facts)
197
+ .then(({ events }) => {
198
+ console.log(facts.accountId + ' is a ' + events.map(event => event.params.message))
199
+ })
200
+
201
+ /*
202
+ * OUTPUT:
203
+ *
204
+ * loading account information... // <-- API call is made ONCE and results recycled for all 3 conditions
205
+ * lincoln is a current microsoft employee taking christmas day off
206
+ */
207
+ ```
208
+
209
+ This is available in the [examples](./examples/03-dynamic-facts.js)
210
+
211
+ ## Debugging
212
+
213
+ To see what the engine is doing under the hood, debug output can be turned on via:
214
+
215
+ ### Node
216
+
217
+ ```bash
218
+ DEBUG=json-rules-engine
219
+ ```
220
+
221
+ ### Browser
222
+ ```js
223
+ // set debug flag in local storage & refresh page to see console output
224
+ localStorage.debug = 'json-rules-engine'
225
+ ```
226
+
227
+ ## Related Projects
228
+
229
+ https://github.com/vinzdeveloper/json-rule-editor - configuration ui for json-rules-engine:
230
+
231
+ <img width="1680" alt="rule editor 2" src="https://user-images.githubusercontent.com/61467683/82750274-dd3b3b80-9da6-11ea-96eb-434a6a1a9bc1.png">
232
+
233
+
234
+ ## License
235
+ [ISC](./LICENSE)