()
| 19 | const { Engine } = require('json-rules-engine') |
| 20 | |
| 21 | async function start () { |
| 22 | /** |
| 23 | * Setup a new engine |
| 24 | */ |
| 25 | const engine = new Engine() |
| 26 | |
| 27 | /** |
| 28 | * Define a 'startsWith' custom operator, for use in later rules |
| 29 | */ |
| 30 | engine.addOperator('startsWith', (factValue, jsonValue) => { |
| 31 | if (!factValue.length) return false |
| 32 | return factValue[0].toLowerCase() === jsonValue.toLowerCase() |
| 33 | }) |
| 34 | |
| 35 | /** |
| 36 | * Add rule for detecting words that start with 'a' |
| 37 | */ |
| 38 | const ruleA = { |
| 39 | conditions: { |
| 40 | all: [{ |
| 41 | fact: 'word', |
| 42 | operator: 'startsWith', |
| 43 | value: 'a' |
| 44 | }] |
| 45 | }, |
| 46 | event: { |
| 47 | type: 'start-with-a' |
| 48 | } |
| 49 | } |
| 50 | engine.addRule(ruleA) |
| 51 | |
| 52 | /* |
| 53 | * Add rule for detecting words that start with 'b' |
| 54 | */ |
| 55 | const ruleB = { |
| 56 | conditions: { |
| 57 | all: [{ |
| 58 | fact: 'word', |
| 59 | operator: 'startsWith', |
| 60 | value: 'b' |
| 61 | }] |
| 62 | }, |
| 63 | event: { |
| 64 | type: 'start-with-b' |
| 65 | } |
| 66 | } |
| 67 | engine.addRule(ruleB) |
| 68 | |
| 69 | // utility for printing output |
| 70 | const printEventType = { |
| 71 | 'start-with-a': 'start with "a"', |
| 72 | 'start-with-b': 'start with "b"' |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Register listeners with the engine for rule success and failure |
| 77 | */ |
| 78 | let facts |
no test coverage detected
searching dependent graphs…