Skip to content

Commit 6c4b09e

Browse files
committed
Simpler separator check
1 parent f54e993 commit 6c4b09e

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const INPUT_VAR_NAME = "it";
22
const QUOTE_CHAR = '"';
3+
const ESCAPE_CHAR = "\\";
34

45
export type Template<T extends object> = (data: T) => string;
56

@@ -12,8 +13,8 @@ export function compile(value: string, displayName = "template") {
1213
const char = value[i];
1314

1415
// Escape special characters due to quoting.
15-
if (char === QUOTE_CHAR || char === "\\") {
16-
result += "\\";
16+
if (char === QUOTE_CHAR || char === ESCAPE_CHAR) {
17+
result += ESCAPE_CHAR;
1718
}
1819

1920
// Process template param.
@@ -25,7 +26,7 @@ export function compile(value: string, displayName = "template") {
2526
for (let j = start; j < value.length; j++) {
2627
const char = value[j];
2728
if (withinString) {
28-
if (char === "\\") j++;
29+
if (char === ESCAPE_CHAR) j++;
2930
else if (char === withinString) withinString = "";
3031
continue;
3132
} else if (char === "}" && value[j + 1] === "}") {
@@ -40,7 +41,7 @@ export function compile(value: string, displayName = "template") {
4041
if (!end) throw new TypeError(`Template parameter not closed at ${i}`);
4142

4243
const param = value.slice(start, end).trim();
43-
const sep = `+-/*.?:![]()%&|;={}<>,`.indexOf(param[0]) > -1 ? "" : ".";
44+
const sep = param[0] === "[" ? "" : ".";
4445
result += `${QUOTE_CHAR} + (${INPUT_VAR_NAME}${sep}${param}) + ${QUOTE_CHAR}`;
4546
continue;
4647
}

0 commit comments

Comments
 (0)