12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- "use strict";
- class ObjectMatcherRulePlugin {
-
- constructor(ruleProperty, dataProperty, additionalConditionFunction) {
- this.ruleProperty = ruleProperty;
- this.dataProperty = dataProperty || ruleProperty;
- this.additionalConditionFunction = additionalConditionFunction;
- }
-
- apply(ruleSetCompiler) {
- const { ruleProperty, dataProperty } = this;
- ruleSetCompiler.hooks.rule.tap(
- "ObjectMatcherRulePlugin",
- (path, rule, unhandledProperties, result) => {
- if (unhandledProperties.has(ruleProperty)) {
- unhandledProperties.delete(ruleProperty);
- const value = rule[ruleProperty];
- for (const property of Object.keys(value)) {
- const nestedDataProperties = property.split(".");
- const condition = ruleSetCompiler.compileCondition(
- `${path}.${ruleProperty}.${property}`,
- value[property]
- );
- if (this.additionalConditionFunction) {
- result.conditions.push({
- property: [dataProperty],
- matchWhenEmpty: condition.matchWhenEmpty,
- fn: this.additionalConditionFunction
- });
- }
- result.conditions.push({
- property: [dataProperty, ...nestedDataProperties],
- matchWhenEmpty: condition.matchWhenEmpty,
- fn: condition.fn
- });
- }
- }
- }
- );
- }
- }
- module.exports = ObjectMatcherRulePlugin;
|