| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | const TypeUnknown = 0; | 
					
						
							|  |  |  | const TypeNull = 1; | 
					
						
							|  |  |  | const TypeString = 2; | 
					
						
							|  |  |  | const TypeNumber = 3; | 
					
						
							|  |  |  | const TypeBoolean = 4; | 
					
						
							|  |  |  | const TypeRegExp = 5; | 
					
						
							|  |  |  | const TypeConditional = 6; | 
					
						
							|  |  |  | const TypeArray = 7; | 
					
						
							|  |  |  | const TypeConstArray = 8; | 
					
						
							|  |  |  | const TypeIdentifier = 9; | 
					
						
							|  |  |  | const TypeWrapped = 10; | 
					
						
							|  |  |  | const TypeTemplateString = 11; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | class BasicEvaluatedExpression { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeUnknown; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		this.range = null; | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.falsy = false; | 
					
						
							|  |  |  | 		this.truthy = false; | 
					
						
							|  |  |  | 		this.bool = null; | 
					
						
							|  |  |  | 		this.number = null; | 
					
						
							|  |  |  | 		this.regExp = null; | 
					
						
							|  |  |  | 		this.string = null; | 
					
						
							|  |  |  | 		this.quasis = null; | 
					
						
							|  |  |  | 		this.array = null; | 
					
						
							|  |  |  | 		this.items = null; | 
					
						
							|  |  |  | 		this.options = null; | 
					
						
							|  |  |  | 		this.prefix = null; | 
					
						
							|  |  |  | 		this.postfix = null; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isNull() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeNull; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isString() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeString; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isNumber() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeNumber; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isBoolean() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeBoolean; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isRegExp() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeRegExp; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isConditional() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeConditional; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isArray() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeArray; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isConstArray() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeConstArray; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isIdentifier() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeIdentifier; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isWrapped() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeWrapped; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isTemplateString() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		return this.type === TypeTemplateString; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 21:58:24 +08:00
										 |  |  | 	isTruthy() { | 
					
						
							|  |  |  | 		return this.truthy; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	isFalsy() { | 
					
						
							|  |  |  | 		return this.falsy; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	asBool() { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (this.truthy) return true; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		if (this.falsy) return false; | 
					
						
							|  |  |  | 		if (this.isBoolean()) return this.bool; | 
					
						
							|  |  |  | 		if (this.isNull()) return false; | 
					
						
							|  |  |  | 		if (this.isString()) return this.string !== ""; | 
					
						
							|  |  |  | 		if (this.isNumber()) return this.number !== 0; | 
					
						
							|  |  |  | 		if (this.isRegExp()) return true; | 
					
						
							|  |  |  | 		if (this.isArray()) return true; | 
					
						
							|  |  |  | 		if (this.isConstArray()) return true; | 
					
						
							|  |  |  | 		if (this.isWrapped()) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			return (this.prefix && this.prefix.asBool()) || | 
					
						
							|  |  |  | 				(this.postfix && this.postfix.asBool()) | 
					
						
							|  |  |  | 				? true | 
					
						
							|  |  |  | 				: undefined; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (this.isTemplateString()) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			for (const quasi of this.quasis) { | 
					
						
							|  |  |  | 				if (quasi.asBool()) return true; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			// can't tell if string will be empty without executing
 | 
					
						
							| 
									
										
										
										
											2016-11-15 21:03:53 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return undefined; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 	setString(string) { | 
					
						
							|  |  |  | 		this.type = TypeString; | 
					
						
							|  |  |  | 		this.string = string; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setNull() { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeNull; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 	setNumber(number) { | 
					
						
							|  |  |  | 		this.type = TypeNumber; | 
					
						
							|  |  |  | 		this.number = number; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setBoolean(bool) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeBoolean; | 
					
						
							|  |  |  | 		this.bool = bool; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setRegExp(regExp) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeRegExp; | 
					
						
							|  |  |  | 		this.regExp = regExp; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setIdentifier(identifier) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeIdentifier; | 
					
						
							|  |  |  | 		this.identifier = identifier; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setWrapped(prefix, postfix) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeWrapped; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		this.prefix = prefix; | 
					
						
							|  |  |  | 		this.postfix = postfix; | 
					
						
							|  |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 	setOptions(options) { | 
					
						
							|  |  |  | 		this.type = TypeConditional; | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 	addOptions(options) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (!this.options) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 			this.type = TypeConditional; | 
					
						
							|  |  |  | 			this.options = []; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 		for (const item of options) { | 
					
						
							|  |  |  | 			this.options.push(item); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setItems(items) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeArray; | 
					
						
							|  |  |  | 		this.items = items; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setArray(array) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeConstArray; | 
					
						
							|  |  |  | 		this.array = array; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setTemplateString(quasis) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:42:57 +08:00
										 |  |  | 		this.type = TypeTemplateString; | 
					
						
							|  |  |  | 		this.quasis = quasis; | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 19:24:47 +08:00
										 |  |  | 	setTruthy() { | 
					
						
							|  |  |  | 		this.falsy = false; | 
					
						
							|  |  |  | 		this.truthy = true; | 
					
						
							|  |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setFalsy() { | 
					
						
							|  |  |  | 		this.falsy = true; | 
					
						
							|  |  |  | 		this.truthy = false; | 
					
						
							|  |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 	setRange(range) { | 
					
						
							|  |  |  | 		this.range = range; | 
					
						
							|  |  |  | 		return this; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-15 21:03:53 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-03 05:32:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = BasicEvaluatedExpression; |