5
5
6
6
"use strict" ;
7
7
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
+
8
21
class BasicEvaluatedExpression {
9
22
10
23
constructor ( ) {
24
+ this . type = TypeUnknown ;
11
25
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 ;
12
38
}
13
39
14
40
isNull ( ) {
15
- return ! ! this . null ;
41
+ return this . type === TypeNull ;
16
42
}
17
43
18
44
isString ( ) {
19
- return Object . prototype . hasOwnProperty . call ( this , "string" ) ;
45
+ return this . type === TypeString ;
20
46
}
21
47
22
48
isNumber ( ) {
23
- return Object . prototype . hasOwnProperty . call ( this , "number" ) ;
49
+ return this . type === TypeNumber ;
24
50
}
25
51
26
52
isBoolean ( ) {
27
- return Object . prototype . hasOwnProperty . call ( this , "bool" ) ;
53
+ return this . type === TypeBoolean ;
28
54
}
29
55
30
56
isRegExp ( ) {
31
- return Object . prototype . hasOwnProperty . call ( this , "regExp" ) ;
57
+ return this . type === TypeRegExp ;
32
58
}
33
59
34
60
isConditional ( ) {
35
- return Object . prototype . hasOwnProperty . call ( this , "options" ) ;
61
+ return this . type === TypeConditional ;
36
62
}
37
63
38
64
isArray ( ) {
39
- return Object . prototype . hasOwnProperty . call ( this , "items" ) ;
65
+ return this . type === TypeArray ;
40
66
}
41
67
42
68
isConstArray ( ) {
43
- return Object . prototype . hasOwnProperty . call ( this , "array" ) ;
69
+ return this . type === TypeConstArray ;
44
70
}
45
71
46
72
isIdentifier ( ) {
47
- return Object . prototype . hasOwnProperty . call ( this , "identifier" ) ;
73
+ return this . type === TypeIdentifier ;
48
74
}
49
75
50
76
isWrapped ( ) {
51
- return Object . prototype . hasOwnProperty . call ( this , "prefix" ) || Object . prototype . hasOwnProperty . call ( this , "postfix" ) ;
77
+ return this . type === TypeWrapped ;
52
78
}
53
79
54
80
isTemplateString ( ) {
55
- return Object . prototype . hasOwnProperty . call ( this , "quasis" ) ;
81
+ return this . type === TypeTemplateString ;
56
82
}
57
83
58
84
isTruthy ( ) {
@@ -68,108 +94,94 @@ class BasicEvaluatedExpression {
68
94
else if ( this . falsy ) return false ;
69
95
else if ( this . isBoolean ( ) ) return this . bool ;
70
96
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 ;
73
99
else if ( this . isRegExp ( ) ) return true ;
74
100
else if ( this . isArray ( ) ) return true ;
75
101
else if ( this . isConstArray ( ) ) return true ;
76
102
else if ( this . isWrapped ( ) ) return this . prefix && this . prefix . asBool ( ) || this . postfix && this . postfix . asBool ( ) ? true : undefined ;
77
103
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 ;
81
106
}
82
107
// can't tell if string will be empty without executing
83
108
}
84
109
return undefined ;
85
110
}
86
111
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 ;
92
115
return this ;
93
116
}
94
117
95
118
setNull ( ) {
96
- this . null = true ;
119
+ this . type = TypeNull ;
97
120
return this ;
98
121
}
99
122
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 ;
105
126
return this ;
106
127
}
107
128
108
129
setBoolean ( bool ) {
109
- if ( bool === null )
110
- delete this . bool ;
111
- else
112
- this . bool = bool ;
130
+ this . type = TypeBoolean ;
131
+ this . bool = bool ;
113
132
return this ;
114
133
}
115
134
116
135
setRegExp ( regExp ) {
117
- if ( regExp === null )
118
- delete this . regExp ;
119
- else
120
- this . regExp = regExp ;
136
+ this . type = TypeRegExp ;
137
+ this . regExp = regExp ;
121
138
return this ;
122
139
}
123
140
124
141
setIdentifier ( identifier ) {
125
- if ( identifier === null )
126
- delete this . identifier ;
127
- else
128
- this . identifier = identifier ;
142
+ this . type = TypeIdentifier ;
143
+ this . identifier = identifier ;
129
144
return this ;
130
145
}
131
146
132
147
setWrapped ( prefix , postfix ) {
148
+ this . type = TypeWrapped ;
133
149
this . prefix = prefix ;
134
150
this . postfix = postfix ;
135
151
return this ;
136
152
}
137
153
138
- unsetWrapped ( ) {
139
- delete this . prefix ;
140
- delete this . postfix ;
154
+ setOptions ( options ) {
155
+ this . type = TypeConditional ;
156
+ this . options = options ;
141
157
return this ;
142
158
}
143
159
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 ) ;
149
167
return this ;
150
168
}
151
169
152
170
setItems ( items ) {
153
- if ( items === null )
154
- delete this . items ;
155
- else
156
- this . items = items ;
171
+ this . type = TypeArray ;
172
+ this . items = items ;
157
173
return this ;
158
174
}
159
175
160
176
setArray ( array ) {
161
- if ( array === null )
162
- delete this . array ;
163
- else
164
- this . array = array ;
177
+ this . type = TypeConstArray ;
178
+ this . array = array ;
165
179
return this ;
166
180
}
167
181
168
182
setTemplateString ( quasis ) {
169
- if ( quasis === null )
170
- delete this . quasis ;
171
- else
172
- this . quasis = quasis ;
183
+ this . type = TypeTemplateString ;
184
+ this . quasis = quasis ;
173
185
return this ;
174
186
}
175
187
@@ -185,14 +197,6 @@ class BasicEvaluatedExpression {
185
197
return this ;
186
198
}
187
199
188
- addOptions ( options ) {
189
- if ( ! this . options ) this . options = [ ] ;
190
- options . forEach ( item => {
191
- this . options . push ( item ) ;
192
- } ) ;
193
- return this ;
194
- }
195
-
196
200
setRange ( range ) {
197
201
this . range = range ;
198
202
return this ;
0 commit comments