Skip to content

Commit 3a9ecab

Browse files
committed
chore: remove redundant else and add check in loop
1 parent e37547a commit 3a9ecab

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

packages/util-endpoints/src/utils/evaluateTemplate.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,47 @@ export const evaluateTemplate = (template: string, options: EvaluateOptions) =>
1010
} as Record<string, string>;
1111

1212
let currentIndex = 0;
13-
while (true) {
13+
while (currentIndex < template.length) {
1414
const openingBraceIndex = template.indexOf("{", currentIndex);
1515

1616
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.
1818
evaluatedTemplateArr.push(template.slice(currentIndex));
1919
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+
}
5121

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("{}");
5234
currentIndex = closingBraceIndex + 1;
35+
continue;
5336
}
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;
5454
}
5555

5656
return evaluatedTemplateArr.join("");

0 commit comments

Comments
 (0)