-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
192 lines (167 loc) · 5.45 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
const queryParser = require('./condition-parser');
/** @typedef {import('./condition-parser').Condition} Condition */
/** @typedef {import('./condition-parser').TruthyCondition} TruthyCondition */
/** @typedef {import('./condition-parser').NumericValueCondition} NumericValueCondition */
/** @typedef {import('./condition-parser').ValueCondition} ValueCondition */
/** @typedef {import('./condition-parser').DuoCondition} DuoCondition */
/** @typedef {undefined|boolean|null|number|string|{ [Key in string]?: FulfillsInputValue }} FulfillsInputValueRaw */
/** @typedef {FulfillsInputValueRaw|FulfillsInputValueRaw[]} FulfillsInputValue */
/** @typedef {{ [Key in string]?: FulfillsInputValue }} FulfillsInput */
/**
* @param {string} query
* @returns {Condition}
*/
const compileCondition = function (query) {
return queryParser.parse(query);
};
/**
* @param {number} value
* @param {NumericValueCondition} condition
* @returns {boolean}
*/
const matchNumericValueAgainstCondition = function (value, condition) {
const {
operator,
value: conditionValue,
} = condition;
if (typeof conditionValue !== 'number') {
throw new TypeError(`Expected a numeric condition value for operator "${operator}", instead got: ${typeof conditionValue}`);
}
switch (operator) {
case '<':
return value < conditionValue;
case '>':
return value > conditionValue;
case '<=':
return value <= conditionValue;
case '>=':
return value >= conditionValue;
default:
throw new Error(`Unknown operator "${operator}"`);
}
};
/**
* @param {FulfillsInputValue} value
* @param {TruthyCondition|ValueCondition} condition
* @returns {boolean}
*/
const matchValueAgainstCondition = function (value, condition) {
const {
operator,
value: conditionValue,
} = condition;
if (operator === undefined && conditionValue === undefined) {
return !!value;
}
if (typeof operator !== 'string') {
throw new TypeError(`Expected a string operator, instead got: ${operator}`);
}
switch (operator) {
case '===':
return value === conditionValue;
case '!==':
return value !== conditionValue;
case '==':
// eslint-disable-next-line eqeqeq
return value == conditionValue;
case '!=':
// eslint-disable-next-line eqeqeq
return value != conditionValue;
case '<':
case '>':
case '<=':
case '>=':
if (typeof value !== 'number') {
throw new TypeError(`Expected a numeric value to for use with operator "${operator}", instead got: ${typeof value}`);
}
return matchNumericValueAgainstCondition(value, condition);
default:
throw new Error(`Unknown operator "${operator}"`);
}
};
/**
* @param {FulfillsInputValue} obj
* @param {import('./condition-parser').Property|undefined} remainingProperty
* @param {TruthyCondition|ValueCondition} condition
* @returns {boolean}
*/
const matchIndividualCondition = function (obj, remainingProperty, condition) {
if (obj === undefined || obj === null || !remainingProperty || !remainingProperty.length) {
return matchValueAgainstCondition(obj, condition);
}
const [key, ...nextInPropertyChain] = remainingProperty;
if (typeof key === 'string') {
/** @type {FulfillsInputValue} */
let objValueByKey;
if (Array.isArray(obj) || typeof obj === 'string') {
if (key === 'length') {
objValueByKey = obj[key];
}
} else if (typeof obj === 'object') {
objValueByKey = obj[key];
}
return matchIndividualCondition(objValueByKey, nextInPropertyChain, condition);
}
// To support eg. arrays, we need some more complex types
if (typeof key === 'object') {
if (key.type === 'array') {
if (!Array.isArray(obj)) return false;
return obj.some(item => matchIndividualCondition(item, key.property, condition));
}
throw new Error('Unimplemented key type');
}
throw new TypeError('Unknown key type');
};
/**
* @param {FulfillsInputValue} obj
* @param {DuoCondition} condition
* @returns {boolean}
*/
const matchDuoCondition = function (obj, condition) {
if (matchCondition(obj, condition.left)) {
if (condition.operator === 'AND') {
return matchCondition(obj, condition.right);
} else if (condition.operator === 'OR') {
return true;
} else {
throw new Error(`Unknown operator "${condition.operator}"`);
}
} else if (condition.operator === 'AND') {
return false;
} else if (condition.operator === 'OR') {
return matchCondition(obj, condition.right);
} else {
throw new Error(`Unknown operator "${condition.operator}"`);
}
};
/**
* @param {FulfillsInputValue} obj
* @param {Condition} condition
* @returns {boolean}
*/
const matchCondition = function (obj, condition) {
if (condition.property !== undefined) {
return matchIndividualCondition(obj, condition.property, condition);
}
if (!condition.operator && condition.not) {
return !matchCondition(obj, condition.not);
}
if (condition.operator && condition.left && condition.right) {
return matchDuoCondition(obj, condition);
}
throw new Error('Unimplemented condition structure');
};
/**
* @param {FulfillsInput} obj
* @param {string|Condition} condition
* @returns {boolean}
*/
const fulfills = function (obj, condition) {
if (typeof condition === 'string') {
condition = compileCondition(condition);
}
return matchCondition(obj, condition);
};
module.exports = fulfills;
module.exports.compileCondition = compileCondition;