-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
460d3ec
commit 9b7922c
Showing
5 changed files
with
161 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { describe, bench } from "vitest"; | ||
import { template } from "./index"; | ||
|
||
describe("template", () => { | ||
const fn = template("Hello {{name}}!"); | ||
|
||
bench("exec", () => { | ||
fn({ name: "Blake" }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,151 @@ | ||
const INPUT_VAR_NAME = "it"; | ||
const QUOTE_CHAR = '"'; | ||
const ESCAPE_CHAR = "\\"; | ||
|
||
export type Template<T extends object> = (data: T) => string; | ||
|
||
/** | ||
* Stringify a template into a function. | ||
*/ | ||
export function compile(value: string) { | ||
let result = QUOTE_CHAR; | ||
for (let i = 0; i < value.length; i++) { | ||
const char = value[i]; | ||
|
||
// Escape special characters due to quoting. | ||
if (char === QUOTE_CHAR || char === ESCAPE_CHAR) { | ||
result += ESCAPE_CHAR; | ||
} | ||
|
||
// Process template param. | ||
if (char === "{" && value[i + 1] === "{") { | ||
const start = i + 2; | ||
let end = 0; | ||
let withinString = ""; | ||
|
||
for (let j = start; j < value.length; j++) { | ||
const char = value[j]; | ||
if (withinString) { | ||
if (char === ESCAPE_CHAR) j++; | ||
else if (char === withinString) withinString = ""; | ||
continue; | ||
} else if (char === "}" && value[j + 1] === "}") { | ||
i = j + 1; | ||
end = j; | ||
break; | ||
} else if (char === '"' || char === "'" || char === "`") { | ||
withinString = char; | ||
} | ||
} | ||
function* parse(value: string): Generator<Token, Token> { | ||
let index = 0; | ||
|
||
if (!end) throw new TypeError(`Template parameter not closed at ${i}`); | ||
while (index < value.length) { | ||
if (value[index] === "\\") { | ||
yield { type: "ESCAPED", index, value: value[index + 1] || "" }; | ||
index += 2; | ||
continue; | ||
} | ||
|
||
if (value[index] === "{" && value[index + 1] === "{") { | ||
yield { type: "{{", index, value: "{{" }; | ||
index += 2; | ||
continue; | ||
} | ||
|
||
const param = value.slice(start, end).trim(); | ||
const sep = param[0] === "[" ? "" : "."; | ||
result += `${QUOTE_CHAR} + (${INPUT_VAR_NAME}${sep}${param}) + ${QUOTE_CHAR}`; | ||
if (value[index] === "}" && value[index + 1] === "}") { | ||
yield { type: "}}", index, value: "{{" }; | ||
index += 2; | ||
continue; | ||
} | ||
|
||
result += char; | ||
yield { type: "CHAR", index, value: value[index++] }; | ||
} | ||
|
||
return { type: "END", index, value: "" }; | ||
} | ||
|
||
interface Token { | ||
type: "{{" | "}}" | "CHAR" | "ESCAPED" | "END"; | ||
index: number; | ||
value: string; | ||
} | ||
|
||
class It { | ||
#peek?: Token; | ||
|
||
constructor(private tokens: Generator<Token, Token>) {} | ||
|
||
peek(): Token { | ||
if (!this.#peek) { | ||
const next = this.tokens.next(); | ||
this.#peek = next.value; | ||
} | ||
return this.#peek; | ||
} | ||
|
||
tryConsume(type: Token["type"]): Token | undefined { | ||
const token = this.peek(); | ||
if (token.type !== type) return undefined; | ||
this.#peek = undefined; | ||
return token; | ||
} | ||
result += QUOTE_CHAR; | ||
|
||
return `function (${INPUT_VAR_NAME}) { return ${result}; }`; | ||
consume(type: Token["type"]): Token { | ||
const token = this.peek(); | ||
if (token.type !== type) { | ||
throw new TypeError( | ||
`Unexpected ${token.type} at index ${token.index}, expected ${type}`, | ||
); | ||
} | ||
this.#peek = undefined; | ||
return token; | ||
} | ||
} | ||
|
||
/** | ||
* Fast and simple string templates. | ||
*/ | ||
export function template<T extends object = object>(value: string) { | ||
const body = compile(value); | ||
return new Function(`return (${body});`)() as Template<T>; | ||
const it = new It(parse(value)); | ||
const values: Array<string | Template<T>> = []; | ||
let text = ""; | ||
|
||
while (true) { | ||
const value = it.tryConsume("CHAR") || it.tryConsume("ESCAPED"); | ||
if (value) { | ||
text += value.value; | ||
continue; | ||
} | ||
|
||
if (text) { | ||
values.push(text); | ||
text = ""; | ||
} | ||
|
||
if (it.tryConsume("{{")) { | ||
const path: string[] = []; | ||
let key = ""; | ||
|
||
while (true) { | ||
const escaped = it.tryConsume("ESCAPED"); | ||
if (escaped) { | ||
key += escaped.value; | ||
continue; | ||
} | ||
|
||
const char = it.tryConsume("CHAR"); | ||
if (char) { | ||
if (char.value === ".") { | ||
path.push(key); | ||
key = ""; | ||
continue; | ||
} | ||
key += char.value; | ||
continue; | ||
} | ||
|
||
path.push(key); | ||
it.consume("}}"); | ||
break; | ||
} | ||
|
||
values.push(getter(path)); | ||
continue; | ||
} | ||
|
||
it.consume("END"); | ||
break; | ||
} | ||
|
||
return (data: T) => { | ||
let result = ""; | ||
for (const value of values) { | ||
result += typeof value === "string" ? value : value(data); | ||
} | ||
return result; | ||
}; | ||
} | ||
|
||
const hasOwnProperty = Object.prototype.hasOwnProperty; | ||
|
||
function getter(path: string[]) { | ||
return (data: any) => { | ||
let value = data; | ||
for (const key of path) { | ||
if (hasOwnProperty.call(value, key)) { | ||
value = value[key]; | ||
} else { | ||
throw new TypeError(`Missing ${path.map(escape).join(".")} in data`); | ||
} | ||
} | ||
return value; | ||
}; | ||
} | ||
|
||
function escape(key: string) { | ||
return key.replace(/\./g, "\\."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters