|
| 1 | +'use strict' |
| 2 | + |
| 3 | +require('colors') |
| 4 | +const { Almanac, Engine } = require('json-rules-engine') |
| 5 | + |
| 6 | +/** |
| 7 | + * Almanac that support piping values through named functions |
| 8 | + */ |
| 9 | +class PipedAlmanac extends Almanac { |
| 10 | + constructor (options) { |
| 11 | + super(options) |
| 12 | + this.pipes = new Map() |
| 13 | + } |
| 14 | + |
| 15 | + addPipe (name, pipe) { |
| 16 | + this.pipes.set(name, pipe) |
| 17 | + } |
| 18 | + |
| 19 | + factValue (factId, params, path) { |
| 20 | + let pipes = [] |
| 21 | + if (params && 'pipes' in params && Array.isArray(params.pipes)) { |
| 22 | + pipes = params.pipes |
| 23 | + delete params.pipes |
| 24 | + } |
| 25 | + return super.factValue(factId, params, path).then(value => { |
| 26 | + return pipes.reduce((value, pipeName) => { |
| 27 | + const pipe = this.pipes.get(pipeName) |
| 28 | + if (pipe) { |
| 29 | + return pipe(value) |
| 30 | + } |
| 31 | + return value |
| 32 | + }, value) |
| 33 | + }) |
| 34 | + } |
| 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 | + |
| 88 | +/* |
| 89 | + * OUTPUT: |
| 90 | + * |
| 91 | + * Bob is 19 years old and is not Over 21(ish) |
| 92 | + * Alice is 21 years old and is Over 21(ish) |
| 93 | + * Chad is 20 years old and is Over 21(ish) |
| 94 | + */ |
0 commit comments