-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathoperator-decorator.js
37 lines (34 loc) · 1.26 KB
/
operator-decorator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict'
import Operator from './operator'
export default class OperatorDecorator {
/**
* Constructor
* @param {string} name - decorator identifier
* @param {function(factValue, jsonValue, next)} callback - callback that takes the next operator as a parameter
* @param {function} [factValueValidator] - optional validator for asserting the data type of the fact
* @returns {OperatorDecorator} - instance
*/
constructor (name, cb, factValueValidator) {
this.name = String(name)
if (!name) throw new Error('Missing decorator name')
if (typeof cb !== 'function') throw new Error('Missing decorator callback')
this.cb = cb
this.factValueValidator = factValueValidator
if (!this.factValueValidator) this.factValueValidator = () => true
}
/**
* Takes the fact result and compares it to the condition 'value', using the callback
* @param {Operator} operator - fact result
* @returns {Operator} - whether the values pass the operator test
*/
decorate (operator) {
const next = operator.evaluate.bind(operator)
return new Operator(
`${this.name}:${operator.name}`,
(factValue, jsonValue) => {
return this.cb(factValue, jsonValue, next)
},
this.factValueValidator
)
}
}