| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */ | 
					
						
							|  |  |  | /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | class ObjectMatcherRulePlugin { | 
					
						
							| 
									
										
										
										
											2024-03-11 22:56:35 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} ruleProperty the rule property | 
					
						
							|  |  |  | 	 * @param {string=} dataProperty the data property | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | 	constructor(ruleProperty, dataProperty) { | 
					
						
							|  |  |  | 		this.ruleProperty = ruleProperty; | 
					
						
							|  |  |  | 		this.dataProperty = dataProperty || ruleProperty; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(ruleSetCompiler) { | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | 		const { ruleProperty, dataProperty } = this; | 
					
						
							| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | 		ruleSetCompiler.hooks.rule.tap( | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | 			"ObjectMatcherRulePlugin", | 
					
						
							| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | 			(path, rule, unhandledProperties, result) => { | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | 				if (unhandledProperties.has(ruleProperty)) { | 
					
						
							|  |  |  | 					unhandledProperties.delete(ruleProperty); | 
					
						
							|  |  |  | 					const value = rule[ruleProperty]; | 
					
						
							| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | 					for (const property of Object.keys(value)) { | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | 						const nestedDataProperties = property.split("."); | 
					
						
							| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | 						const condition = ruleSetCompiler.compileCondition( | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | 							`${path}.${ruleProperty}.${property}`, | 
					
						
							| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | 							value[property] | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						result.conditions.push({ | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | 							property: [dataProperty, ...nestedDataProperties], | 
					
						
							| 
									
										
										
										
											2020-07-17 16:27:48 +08:00
										 |  |  | 							matchWhenEmpty: condition.matchWhenEmpty, | 
					
						
							|  |  |  | 							fn: condition.fn | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-17 04:16:06 +08:00
										 |  |  | module.exports = ObjectMatcherRulePlugin; |