-
Notifications
You must be signed in to change notification settings - Fork 491
/
Copy pathfact-decorator.js
39 lines (33 loc) · 945 Bytes
/
fact-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
38
39
import Fact from "./fact";
class FactDecorator {
/**
*
* @param {string} id
* @param {function} cb Function that computes the new fact value by invoking a 3rd argument
* that is a function to produce the next value
* @param {object} options options to override the defaults from the decorated fact
*/
constructor(id, cb, options) {
this.id = id
this.cb = cb
if (options) {
this.options = options
} else {
this.options = {}
}
}
/**
*
* @param {Fact} fact to decorate
* @returns {Fact} the decorated fact
*/
decorate(fact) {
const next = fact.calculate.bind(fact);
return new Fact(
`${this.id}:${fact.id}`,
(params, almanac) => this.cb(params, almanac, next),
Object.assign({}, this.options, fact.options)
)
}
}
export default FactDecorator