()
| 35 | } |
| 36 | |
| 37 | async function start () { |
| 38 | const engine = new Engine() |
| 39 | .addRule({ |
| 40 | conditions: { |
| 41 | all: [ |
| 42 | { |
| 43 | fact: 'age', |
| 44 | params: { |
| 45 | // the addOne pipe adds one to the value |
| 46 | pipes: ['addOne'] |
| 47 | }, |
| 48 | operator: 'greaterThanInclusive', |
| 49 | value: 21 |
| 50 | } |
| 51 | ] |
| 52 | }, |
| 53 | event: { |
| 54 | type: 'Over 21(ish)' |
| 55 | } |
| 56 | }) |
| 57 | |
| 58 | engine.on('success', async (event, almanac) => { |
| 59 | const name = await almanac.factValue('name') |
| 60 | const age = await almanac.factValue('age') |
| 61 | console.log(`${name} is ${age} years old and ${'is'.green} ${event.type}`) |
| 62 | }) |
| 63 | |
| 64 | engine.on('failure', async (event, almanac) => { |
| 65 | const name = await almanac.factValue('name') |
| 66 | const age = await almanac.factValue('age') |
| 67 | console.log(`${name} is ${age} years old and ${'is not'.red} ${event.type}`) |
| 68 | }) |
| 69 | |
| 70 | const createAlmanacWithPipes = () => { |
| 71 | const almanac = new PipedAlmanac() |
| 72 | almanac.addPipe('addOne', (v) => v + 1) |
| 73 | return almanac |
| 74 | } |
| 75 | |
| 76 | // first run Bob who is less than 20 |
| 77 | await engine.run({ name: 'Bob', age: 19 }, { almanac: createAlmanacWithPipes() }) |
| 78 | |
| 79 | // second run Alice who is 21 |
| 80 | await engine.run({ name: 'Alice', age: 21 }, { almanac: createAlmanacWithPipes() }) |
| 81 | |
| 82 | // third run Chad who is 20 |
| 83 | await engine.run({ name: 'Chad', age: 20 }, { almanac: createAlmanacWithPipes() }) |
| 84 | } |
| 85 | |
| 86 | start() |
| 87 |
no test coverage detected
searching dependent graphs…