* Returns compiled RuleSet. * @param {RuleSetRules} ruleSet raw user provided rules * @returns {RuleSet} compiled RuleSet
(ruleSet)
| 132 | * @returns {RuleSet} compiled RuleSet |
| 133 | */ |
| 134 | compile(ruleSet) { |
| 135 | /** @type {References} */ |
| 136 | const refs = new Map(); |
| 137 | const rules = this.compileRules("ruleSet", ruleSet, refs); |
| 138 | |
| 139 | /** |
| 140 | * Returns true, if the rule has matched. |
| 141 | * @param {EffectData} data data passed in |
| 142 | * @param {CompiledRule} rule the compiled rule |
| 143 | * @param {Effect[]} effects an array where effects are pushed to |
| 144 | * @returns {boolean} true, if the rule has matched |
| 145 | */ |
| 146 | const execRule = (data, rule, effects) => { |
| 147 | for (const condition of rule.conditions) { |
| 148 | const p = condition.property; |
| 149 | if (Array.isArray(p)) { |
| 150 | /** @type {EXPECTED_ANY} */ |
| 151 | let current = data; |
| 152 | for (const subProperty of p) { |
| 153 | if ( |
| 154 | current && |
| 155 | typeof current === "object" && |
| 156 | Object.prototype.hasOwnProperty.call(current, subProperty) |
| 157 | ) { |
| 158 | current = current[/** @type {keyof EffectData} */ (subProperty)]; |
| 159 | } else { |
| 160 | current = undefined; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | if (current !== undefined) { |
| 165 | if (!condition.fn(current)) return false; |
| 166 | continue; |
| 167 | } |
| 168 | } else if (p in data) { |
| 169 | const value = data[/** @type {keyof EffectData} */ (p)]; |
| 170 | if (value !== undefined) { |
| 171 | if (!condition.fn(value)) return false; |
| 172 | continue; |
| 173 | } |
| 174 | } |
| 175 | if (!condition.matchWhenEmpty) { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | for (const effect of rule.effects) { |
| 180 | if (typeof effect === "function") { |
| 181 | const returnedEffects = effect(data); |
| 182 | for (const effect of returnedEffects) { |
| 183 | effects.push(effect); |
| 184 | } |
| 185 | } else { |
| 186 | effects.push(effect); |
| 187 | } |
| 188 | } |
| 189 | if (rule.rules) { |
| 190 | for (const childRule of rule.rules) { |
| 191 | execRule(data, childRule, effects); |
nothing calls this directly
no test coverage detected