@@ -10,47 +10,47 @@ export const evaluateTemplate = (template: string, options: EvaluateOptions) =>
10
10
} as Record < string , string > ;
11
11
12
12
let currentIndex = 0 ;
13
- while ( true ) {
13
+ while ( currentIndex < template . length ) {
14
14
const openingBraceIndex = template . indexOf ( "{" , currentIndex ) ;
15
15
16
16
if ( openingBraceIndex === - 1 ) {
17
- // No more opening braces, add the rest of the template and break
17
+ // No more opening braces, add the rest of the template and break.
18
18
evaluatedTemplateArr . push ( template . slice ( currentIndex ) ) ;
19
19
break ;
20
- } else {
21
- evaluatedTemplateArr . push ( template . slice ( currentIndex , openingBraceIndex ) ) ;
22
- const closingBraceIndex = template . indexOf ( "}" , openingBraceIndex ) ;
23
-
24
- if ( closingBraceIndex === - 1 ) {
25
- // Invalid template, but pass as it is.
26
- evaluatedTemplateArr . push ( template . slice ( openingBraceIndex ) ) ;
27
- break ;
28
- }
29
-
30
- if ( closingBraceIndex === openingBraceIndex + 1 ) {
31
- // Empty parameter, pass as it is.
32
- evaluatedTemplateArr . push ( template . slice ( openingBraceIndex , closingBraceIndex + 1 ) ) ;
33
- currentIndex = closingBraceIndex + 1 ;
34
- continue ;
35
- }
36
-
37
- if ( template [ openingBraceIndex + 1 ] === "{" && template [ closingBraceIndex + 1 ] === "}" ) {
38
- // Escaped expression. Do not evaluate.
39
- evaluatedTemplateArr . push ( template . slice ( openingBraceIndex + 1 , closingBraceIndex ) ) ;
40
- currentIndex = closingBraceIndex + 2 ;
41
- }
42
-
43
- const parameterName = template . substring ( openingBraceIndex + 1 , closingBraceIndex ) ;
44
-
45
- if ( parameterName . includes ( "#" ) ) {
46
- const [ refName , attrName ] = parameterName . split ( "#" ) ;
47
- evaluatedTemplateArr . push ( getAttr ( templateContext [ refName ] , attrName ) as string ) ;
48
- } else {
49
- evaluatedTemplateArr . push ( templateContext [ parameterName ] ) ;
50
- }
20
+ }
51
21
22
+ evaluatedTemplateArr . push ( template . slice ( currentIndex , openingBraceIndex ) ) ;
23
+ const closingBraceIndex = template . indexOf ( "}" , openingBraceIndex ) ;
24
+
25
+ if ( closingBraceIndex === - 1 ) {
26
+ // No more closing braces, add the rest of the template and break.
27
+ evaluatedTemplateArr . push ( template . slice ( openingBraceIndex ) ) ;
28
+ break ;
29
+ }
30
+
31
+ if ( closingBraceIndex === openingBraceIndex + 1 ) {
32
+ // Empty parameter, pass as it is.
33
+ evaluatedTemplateArr . push ( "{}" ) ;
52
34
currentIndex = closingBraceIndex + 1 ;
35
+ continue ;
53
36
}
37
+
38
+ if ( template [ openingBraceIndex + 1 ] === "{" && template [ closingBraceIndex + 1 ] === "}" ) {
39
+ // Escaped expression. Do not evaluate.
40
+ evaluatedTemplateArr . push ( template . slice ( openingBraceIndex + 1 , closingBraceIndex ) ) ;
41
+ currentIndex = closingBraceIndex + 2 ;
42
+ }
43
+
44
+ const parameterName = template . substring ( openingBraceIndex + 1 , closingBraceIndex ) ;
45
+
46
+ if ( parameterName . includes ( "#" ) ) {
47
+ const [ refName , attrName ] = parameterName . split ( "#" ) ;
48
+ evaluatedTemplateArr . push ( getAttr ( templateContext [ refName ] , attrName ) as string ) ;
49
+ } else {
50
+ evaluatedTemplateArr . push ( templateContext [ parameterName ] ) ;
51
+ }
52
+
53
+ currentIndex = closingBraceIndex + 1 ;
54
54
}
55
55
56
56
return evaluatedTemplateArr . join ( "" ) ;
0 commit comments