/
conditionRegisterer.js
52 lines (46 loc) · 1.33 KB
/
conditionRegisterer.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
export const conditions = {};
/**
* Get condition closure with pre-bound arguments.
*
* @param {string} name Condition name.
* @param {Array} args Condition arguments.
* @returns {Function}
*/
export function getCondition(name, args) {
if (!conditions[name]) {
throw Error(`Filter condition "${name}" does not exist.`);
}
const { condition, descriptor } = conditions[name];
let conditionArguments = args;
if (descriptor.inputValuesDecorator) {
conditionArguments = descriptor.inputValuesDecorator(conditionArguments);
}
return function(dataRow) {
return condition.apply(dataRow.meta.instance, [].concat([dataRow], [conditionArguments]));
};
}
/**
* Get condition object descriptor which defines some additional informations about this condition.
*
* @param {string} name Condition name.
* @returns {object}
*/
export function getConditionDescriptor(name) {
if (!conditions[name]) {
throw Error(`Filter condition "${name}" does not exist.`);
}
return conditions[name].descriptor;
}
/**
* Condition registerer.
*
* @param {string} name Condition name.
* @param {Function} condition Condition function.
* @param {object} descriptor Condition descriptor.
*/
export function registerCondition(name, condition, descriptor) {
descriptor.key = name;
conditions[name] = {
condition, descriptor
};
}