| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const getProperty = (obj, name) => { | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 	name = name.split("."); | 
					
						
							| 
									
										
										
										
											2017-11-15 16:28:45 +08:00
										 |  |  | 	for(let i = 0; i < name.length - 1; i++) { | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 		obj = obj[name[i]]; | 
					
						
							| 
									
										
										
										
											2017-12-13 23:05:21 +08:00
										 |  |  | 		if(typeof obj !== "object" || !obj || Array.isArray(obj)) return; | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return obj[name.pop()]; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const setProperty = (obj, name, value) => { | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 	name = name.split("."); | 
					
						
							| 
									
										
										
										
											2017-11-15 16:28:45 +08:00
										 |  |  | 	for(let i = 0; i < name.length - 1; i++) { | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 		if(typeof obj[name[i]] !== "object" && typeof obj[name[i]] !== "undefined") return; | 
					
						
							| 
									
										
										
										
											2017-12-13 23:05:21 +08:00
										 |  |  | 		if(Array.isArray(obj[name[i]])) return; | 
					
						
							| 
									
										
										
										
											2016-12-14 19:03:24 +08:00
										 |  |  | 		if(!obj[name[i]]) obj[name[i]] = {}; | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 		obj = obj[name[i]]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	obj[name.pop()] = value; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | class OptionsDefaulter { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							| 
									
										
										
										
											2017-01-11 17:51:58 +08:00
										 |  |  | 		this.defaults = {}; | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | 		this.config = {}; | 
					
						
							| 
									
										
										
										
											2016-10-29 17:11:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | 	process(options) { | 
					
						
							| 
									
										
										
										
											2017-09-14 15:22:29 +08:00
										 |  |  | 		options = Object.assign({}, options); | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | 		for(let name in this.defaults) { | 
					
						
							|  |  |  | 			switch(this.config[name]) { | 
					
						
							|  |  |  | 				case undefined: | 
					
						
							|  |  |  | 					if(getProperty(options, name) === undefined) | 
					
						
							|  |  |  | 						setProperty(options, name, this.defaults[name]); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case "call": | 
					
						
							|  |  |  | 					setProperty(options, name, this.defaults[name].call(this, getProperty(options, name), options), options); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case "make": | 
					
						
							|  |  |  | 					if(getProperty(options, name) === undefined) | 
					
						
							|  |  |  | 						setProperty(options, name, this.defaults[name].call(this, options), options); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case "append": | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						let oldValue = getProperty(options, name); | 
					
						
							|  |  |  | 						if(!Array.isArray(oldValue)) oldValue = []; | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 						oldValue.push(...this.defaults[name]); | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | 						setProperty(options, name, oldValue); | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					throw new Error("OptionsDefaulter cannot process " + this.config[name]); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-09-14 15:22:29 +08:00
										 |  |  | 		return options; | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | 	set(name, config, def) { | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 		if(def !== undefined) { | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | 			this.defaults[name] = def; | 
					
						
							|  |  |  | 			this.config[name] = config; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.defaults[name] = config; | 
					
						
							|  |  |  | 			delete this.config[name]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-03 00:45:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = OptionsDefaulter; |