Skip to content

Commit

Permalink
Simpler separator check
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Mar 7, 2022
1 parent f54e993 commit 6c4b09e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const INPUT_VAR_NAME = "it";
const QUOTE_CHAR = '"';
const ESCAPE_CHAR = "\\";

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

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

// Escape special characters due to quoting.
if (char === QUOTE_CHAR || char === "\\") {
result += "\\";
if (char === QUOTE_CHAR || char === ESCAPE_CHAR) {
result += ESCAPE_CHAR;
}

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

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

0 comments on commit 6c4b09e

Please sign in to comment.