3
3
const escapeRegExp = / [ " & ' < = > ] / g;
4
4
5
5
const escapeFunction = ( string ) => {
6
+ if ( ! escapeRegExp . test ( string ) ) {
7
+ return string ;
8
+ }
9
+
6
10
let escaped = "" ;
7
11
let start = 0 ;
8
12
9
- while ( escapeRegExp . test ( string ) ) {
13
+ do {
10
14
const i = escapeRegExp . lastIndex - 1 ;
11
15
12
16
switch ( string . charCodeAt ( i ) ) {
@@ -43,7 +47,7 @@ const escapeFunction = (string) => {
43
47
}
44
48
45
49
start = escapeRegExp . lastIndex ;
46
- }
50
+ } while ( escapeRegExp . test ( string ) ) ;
47
51
48
52
return escaped + string . slice ( start ) ;
49
53
} ;
@@ -55,8 +59,9 @@ const escapeFunction = (string) => {
55
59
*/
56
60
const html = ( literals , ...expressions ) => {
57
61
let accumulator = "" ;
62
+ const expressionsLength = expressions . length ;
58
63
59
- for ( let i = 0 ; i !== expressions . length ; ++ i ) {
64
+ for ( let i = 0 ; i < expressionsLength ; ++ i ) {
60
65
let literal = literals . raw [ i ] ;
61
66
let string =
62
67
typeof expressions [ i ] === "string"
@@ -76,7 +81,7 @@ const html = (literals, ...expressions) => {
76
81
accumulator += literal + string ;
77
82
}
78
83
79
- return accumulator + literals . raw [ expressions . length ] ;
84
+ return accumulator + literals . raw [ expressionsLength ] ;
80
85
} ;
81
86
82
87
/**
@@ -86,7 +91,9 @@ const html = (literals, ...expressions) => {
86
91
* @returns {Generator<string, void, void> } Generator<string, void, void>
87
92
*/
88
93
const htmlGenerator = function * ( literals , ...expressions ) {
89
- for ( let i = 0 ; i !== expressions . length ; ++ i ) {
94
+ const expressionsLength = expressions . length ;
95
+
96
+ for ( let i = 0 ; i < expressionsLength ; ++ i ) {
90
97
let expression = expressions [ i ] ;
91
98
let literal = literals . raw [ i ] ;
92
99
let string ;
@@ -165,8 +172,8 @@ const htmlGenerator = function* (literals, ...expressions) {
165
172
}
166
173
}
167
174
168
- if ( literals . raw [ expressions . length ] . length ) {
169
- yield literals . raw [ expressions . length ] ;
175
+ if ( literals . raw [ expressionsLength ] . length ) {
176
+ yield literals . raw [ expressionsLength ] ;
170
177
}
171
178
} ;
172
179
@@ -177,7 +184,9 @@ const htmlGenerator = function* (literals, ...expressions) {
177
184
* @returns {AsyncGenerator<string, void, void> } AsyncGenerator<string, void, void>
178
185
*/
179
186
const htmlAsyncGenerator = async function * ( literals , ...expressions ) {
180
- for ( let i = 0 ; i !== expressions . length ; ++ i ) {
187
+ const expressionsLength = expressions . length ;
188
+
189
+ for ( let i = 0 ; i < expressionsLength ; ++ i ) {
181
190
let expression = await expressions [ i ] ;
182
191
let literal = literals . raw [ i ] ;
183
192
let string ;
@@ -262,8 +271,8 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
262
271
}
263
272
}
264
273
265
- if ( literals . raw [ expressions . length ] . length ) {
266
- yield literals . raw [ expressions . length ] ;
274
+ if ( literals . raw [ expressionsLength ] . length ) {
275
+ yield literals . raw [ expressionsLength ] ;
267
276
}
268
277
} ;
269
278
0 commit comments