-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy pathengine-default-fact-decorators.js
64 lines (58 loc) · 2.35 KB
/
engine-default-fact-decorators.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { JSONPath } from "jsonpath-plus";
import FactDecorator from "./fact-decorator";
export const PathDecorator = new FactDecorator("path", (params, almanac, next) => {
if (Object.prototype.hasOwnProperty.call(params, 'path')) {
const path = params.path
const paramCopy = Object.assign({}, params)
delete paramCopy.path
return Promise.resolve(next(paramCopy, almanac)).then(factValue => {
if (factValue != null && typeof factValue === 'object') {
const pathValue = JSONPath({ json: factValue, path, wrap: false })
debug('condition::evaluate extracting object', { property: path, received: pathValue })
return pathValue
} else {
debug('condition::evaluate could not compute object path of non-object', { path, factValue, type: typeof factValue })
return factValue
}
})
} else {
return next(params, almanac);
}
})
export const KeysOfDecorator = new FactDecorator("keysOf", (params, almanac, next) => {
const n = next(params, almanac)
if (n != null) {
if (Object.prototype.hasOwnProperty.call(n, 'keys') && typeof n.keys === 'function') {
return Array.from(n.keys())
}
return Object.keys(n)
}
return n;
})
export const ValuesOfDecorator = new FactDecorator("valuesOf", (params, almanac, next) => {
const n = next(params,almanac)
if (n != null) {
if (Object.prototype.hasOwnProperty(n, 'values') && typeof n.values === 'function') {
return Array.from(n.values())
}
return Object.values(n)
}
return n
})
export const SizeOfDecorator = new FactDecorator("sizeOf", (params, almanac, next) => {
const n = next(params, almanac)
if (n != null) {
if (Object.prototype.hasOwnProperty(n, 'length')) {
return n.length
} else if (Object.prototype.hasOwnProperty(n, 'size') && typeof n.size === 'function') {
return n.size()
}
}
return 1
})
/**
* Options (arg 3) are merged onto fact options and override
* This allows us to do things like create a noCache version of a fact
* noCache:name for instance would access the name fact without hitting the cache
*/
export const NoCacheDecorator = new FactDecorator("noCache", (params, almanac, next) => next(params, almanac), { cache: false })