55
66"use strict" ;
77
8+ const TypeUnknown = 0 ;
9+ const TypeNull = 1 ;
10+ const TypeString = 2 ;
11+ const TypeNumber = 3 ;
12+ const TypeBoolean = 4 ;
13+ const TypeRegExp = 5 ;
14+ const TypeConditional = 6 ;
15+ const TypeArray = 7 ;
16+ const TypeConstArray = 8 ;
17+ const TypeIdentifier = 9 ;
18+ const TypeWrapped = 10 ;
19+ const TypeTemplateString = 11 ;
20+
821class BasicEvaluatedExpression {
922
1023 constructor ( ) {
24+ this . type = TypeUnknown ;
1125 this . range = null ;
26+ this . falsy = false ;
27+ this . truthy = false ;
28+ this . bool = null ;
29+ this . number = null ;
30+ this . regExp = null ;
31+ this . string = null ;
32+ this . quasis = null ;
33+ this . array = null ;
34+ this . items = null ;
35+ this . options = null ;
36+ this . prefix = null ;
37+ this . postfix = null ;
1238 }
1339
1440 isNull ( ) {
15- return ! ! this . null ;
41+ return this . type === TypeNull ;
1642 }
1743
1844 isString ( ) {
19- return Object . prototype . hasOwnProperty . call ( this , "string" ) ;
45+ return this . type === TypeString ;
2046 }
2147
2248 isNumber ( ) {
23- return Object . prototype . hasOwnProperty . call ( this , "number" ) ;
49+ return this . type === TypeNumber ;
2450 }
2551
2652 isBoolean ( ) {
27- return Object . prototype . hasOwnProperty . call ( this , "bool" ) ;
53+ return this . type === TypeBoolean ;
2854 }
2955
3056 isRegExp ( ) {
31- return Object . prototype . hasOwnProperty . call ( this , "regExp" ) ;
57+ return this . type === TypeRegExp ;
3258 }
3359
3460 isConditional ( ) {
35- return Object . prototype . hasOwnProperty . call ( this , "options" ) ;
61+ return this . type === TypeConditional ;
3662 }
3763
3864 isArray ( ) {
39- return Object . prototype . hasOwnProperty . call ( this , "items" ) ;
65+ return this . type === TypeArray ;
4066 }
4167
4268 isConstArray ( ) {
43- return Object . prototype . hasOwnProperty . call ( this , "array" ) ;
69+ return this . type === TypeConstArray ;
4470 }
4571
4672 isIdentifier ( ) {
47- return Object . prototype . hasOwnProperty . call ( this , "identifier" ) ;
73+ return this . type === TypeIdentifier ;
4874 }
4975
5076 isWrapped ( ) {
51- return Object . prototype . hasOwnProperty . call ( this , "prefix" ) || Object . prototype . hasOwnProperty . call ( this , "postfix" ) ;
77+ return this . type === TypeWrapped ;
5278 }
5379
5480 isTemplateString ( ) {
55- return Object . prototype . hasOwnProperty . call ( this , "quasis" ) ;
81+ return this . type === TypeTemplateString ;
5682 }
5783
5884 isTruthy ( ) {
@@ -68,108 +94,94 @@ class BasicEvaluatedExpression {
6894 else if ( this . falsy ) return false ;
6995 else if ( this . isBoolean ( ) ) return this . bool ;
7096 else if ( this . isNull ( ) ) return false ;
71- else if ( this . isString ( ) ) return ! ! this . string ;
72- else if ( this . isNumber ( ) ) return ! ! this . number ;
97+ else if ( this . isString ( ) ) return this . string !== "" ;
98+ else if ( this . isNumber ( ) ) return this . number !== 0 ;
7399 else if ( this . isRegExp ( ) ) return true ;
74100 else if ( this . isArray ( ) ) return true ;
75101 else if ( this . isConstArray ( ) ) return true ;
76102 else if ( this . isWrapped ( ) ) return this . prefix && this . prefix . asBool ( ) || this . postfix && this . postfix . asBool ( ) ? true : undefined ;
77103 else if ( this . isTemplateString ( ) ) {
78- if ( this . quasis . length === 1 ) return this . quasis [ 0 ] . asBool ( ) ;
79- for ( let i = 0 ; i < this . quasis . length ; i ++ ) {
80- if ( this . quasis [ i ] . asBool ( ) ) return true ;
104+ for ( const quasi of this . quasis ) {
105+ if ( quasi . asBool ( ) ) return true ;
81106 }
82107 // can't tell if string will be empty without executing
83108 }
84109 return undefined ;
85110 }
86111
87- setString ( str ) {
88- if ( str === null )
89- delete this . string ;
90- else
91- this . string = str ;
112+ setString ( string ) {
113+ this . type = TypeString ;
114+ this . string = string ;
92115 return this ;
93116 }
94117
95118 setNull ( ) {
96- this . null = true ;
119+ this . type = TypeNull ;
97120 return this ;
98121 }
99122
100- setNumber ( num ) {
101- if ( num === null )
102- delete this . number ;
103- else
104- this . number = num ;
123+ setNumber ( number ) {
124+ this . type = TypeNumber ;
125+ this . number = number ;
105126 return this ;
106127 }
107128
108129 setBoolean ( bool ) {
109- if ( bool === null )
110- delete this . bool ;
111- else
112- this . bool = bool ;
130+ this . type = TypeBoolean ;
131+ this . bool = bool ;
113132 return this ;
114133 }
115134
116135 setRegExp ( regExp ) {
117- if ( regExp === null )
118- delete this . regExp ;
119- else
120- this . regExp = regExp ;
136+ this . type = TypeRegExp ;
137+ this . regExp = regExp ;
121138 return this ;
122139 }
123140
124141 setIdentifier ( identifier ) {
125- if ( identifier === null )
126- delete this . identifier ;
127- else
128- this . identifier = identifier ;
142+ this . type = TypeIdentifier ;
143+ this . identifier = identifier ;
129144 return this ;
130145 }
131146
132147 setWrapped ( prefix , postfix ) {
148+ this . type = TypeWrapped ;
133149 this . prefix = prefix ;
134150 this . postfix = postfix ;
135151 return this ;
136152 }
137153
138- unsetWrapped ( ) {
139- delete this . prefix ;
140- delete this . postfix ;
154+ setOptions ( options ) {
155+ this . type = TypeConditional ;
156+ this . options = options ;
141157 return this ;
142158 }
143159
144- setOptions ( options ) {
145- if ( options === null )
146- delete this . options ;
147- else
148- this . options = options ;
160+ addOptions ( options ) {
161+ if ( ! this . options ) {
162+ this . type = TypeConditional ;
163+ this . options = [ ] ;
164+ }
165+ for ( const item of options )
166+ this . options . push ( item ) ;
149167 return this ;
150168 }
151169
152170 setItems ( items ) {
153- if ( items === null )
154- delete this . items ;
155- else
156- this . items = items ;
171+ this . type = TypeArray ;
172+ this . items = items ;
157173 return this ;
158174 }
159175
160176 setArray ( array ) {
161- if ( array === null )
162- delete this . array ;
163- else
164- this . array = array ;
177+ this . type = TypeConstArray ;
178+ this . array = array ;
165179 return this ;
166180 }
167181
168182 setTemplateString ( quasis ) {
169- if ( quasis === null )
170- delete this . quasis ;
171- else
172- this . quasis = quasis ;
183+ this . type = TypeTemplateString ;
184+ this . quasis = quasis ;
173185 return this ;
174186 }
175187
@@ -185,14 +197,6 @@ class BasicEvaluatedExpression {
185197 return this ;
186198 }
187199
188- addOptions ( options ) {
189- if ( ! this . options ) this . options = [ ] ;
190- options . forEach ( item => {
191- this . options . push ( item ) ;
192- } ) ;
193- return this ;
194- }
195-
196200 setRange ( range ) {
197201 this . range = range ;
198202 return this ;
0 commit comments