1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */
|
---|
9 | /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */
|
---|
10 |
|
---|
11 | class ObjectMatcherRulePlugin {
|
---|
12 | constructor(ruleProperty, dataProperty) {
|
---|
13 | this.ruleProperty = ruleProperty;
|
---|
14 | this.dataProperty = dataProperty || ruleProperty;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler
|
---|
19 | * @returns {void}
|
---|
20 | */
|
---|
21 | apply(ruleSetCompiler) {
|
---|
22 | const { ruleProperty, dataProperty } = this;
|
---|
23 | ruleSetCompiler.hooks.rule.tap(
|
---|
24 | "ObjectMatcherRulePlugin",
|
---|
25 | (path, rule, unhandledProperties, result) => {
|
---|
26 | if (unhandledProperties.has(ruleProperty)) {
|
---|
27 | unhandledProperties.delete(ruleProperty);
|
---|
28 | const value = rule[ruleProperty];
|
---|
29 | for (const property of Object.keys(value)) {
|
---|
30 | const nestedDataProperties = property.split(".");
|
---|
31 | const condition = ruleSetCompiler.compileCondition(
|
---|
32 | `${path}.${ruleProperty}.${property}`,
|
---|
33 | value[property]
|
---|
34 | );
|
---|
35 | result.conditions.push({
|
---|
36 | property: [dataProperty, ...nestedDataProperties],
|
---|
37 | matchWhenEmpty: condition.matchWhenEmpty,
|
---|
38 | fn: condition.fn
|
---|
39 | });
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 | );
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | module.exports = ObjectMatcherRulePlugin;
|
---|