Skip to content

Commit e1b0887

Browse files
committed
fix: prevent crashes and enhance string support
1 parent e1f1fda commit e1b0887

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

examples/03-dynamic-facts.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async function start () {
3838
}, {
3939
fact: 'account-information',
4040
operator: 'in',
41-
value: ['active', 'paid-leave'], // 'status'' can be active or paid-leave
41+
value: ['active', 'paid-leave'], // 'status' can be active or paid-leave
4242
path: '$.status' // access the 'status' property of "account-information"
4343
}, {
4444
fact: 'account-information',
@@ -61,18 +61,25 @@ async function start () {
6161
* into the engine. The major advantage of this technique is that although there are THREE conditions
6262
* requiring this data, only ONE api call is made. This results in much more efficient runtime performance.
6363
*/
64-
engine.addFact('account-information', function (params, almanac) {
65-
return almanac.factValue('accountId')
66-
.then(accountId => {
67-
return apiClient.getAccountInformation(accountId)
68-
})
64+
engine.addFact('account-information', async function (params, almanac) {
65+
try {
66+
const accountId = await almanac.factValue('accountId')
67+
return await apiClient.getAccountInformation(accountId)
68+
} catch (err) {
69+
console.error('Error fetching account-information:', err)
70+
throw err
71+
}
6972
})
7073

7174
// define fact(s) known at runtime
7275
const facts = { accountId: 'lincoln' }
7376
const { events } = await engine.run(facts)
7477

75-
console.log(facts.accountId + ' is a ' + events.map(event => event.params.message))
78+
if (events.length > 0) {
79+
console.log(facts.accountId + ' is a ' + events.map(event => event.params.message).join(', '))
80+
} else {
81+
console.log(facts.accountId + ' did not meet conditions for the microsoft-christmas-pto rule')
82+
}
7683
}
7784
start()
7885

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"clone": "^2.1.2",
9393
"eventemitter2": "^6.4.4",
9494
"hash-it": "^6.0.0",
95+
"json-rules-engine": "^7.3.1",
9596
"jsonpath-plus": "^10.3.0"
9697
}
9798
}

src/engine-default-operators.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import Operator from './operator'
44

55
const Operators = []
6+
function isCollection (value) {
7+
return !!(value && (Array.isArray(value) || typeof value === 'string'))
8+
}
9+
610
Operators.push(new Operator('equal', (a, b) => a === b))
711
Operators.push(new Operator('notEqual', (a, b) => a !== b))
8-
Operators.push(new Operator('in', (a, b) => b.indexOf(a) > -1))
9-
Operators.push(new Operator('notIn', (a, b) => b.indexOf(a) === -1))
12+
Operators.push(new Operator('in', (a, b) => isCollection(b) && b.indexOf(a) > -1))
13+
Operators.push(new Operator('notIn', (a, b) => !isCollection(b) || b.indexOf(a) === -1))
1014

11-
Operators.push(new Operator('contains', (a, b) => a.indexOf(b) > -1, Array.isArray))
12-
Operators.push(new Operator('doesNotContain', (a, b) => a.indexOf(b) === -1, Array.isArray))
15+
Operators.push(new Operator('contains', (a, b) => a.indexOf(b) > -1, isCollection))
16+
Operators.push(new Operator('doesNotContain', (a, b) => a.indexOf(b) === -1, isCollection))
1317

1418
function numberValidator (factValue) {
15-
return Number.parseFloat(factValue).toString() !== 'NaN'
19+
return typeof factValue !== 'object' && !Number.isNaN(Number.parseFloat(factValue))
1620
}
1721
Operators.push(new Operator('lessThan', (a, b) => a < b, numberValidator))
1822
Operators.push(new Operator('lessThanInclusive', (a, b) => a <= b, numberValidator))

0 commit comments

Comments
 (0)