add day 3

This commit is contained in:
Zoe
2022-09-25 22:37:11 -05:00
parent a09bc1d1b4
commit 1c28d2a045
7 changed files with 1053 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
export const compileToString = (template) => {
const ast = parse(template);
let fnStr = `\`\``;
ast.map(t => {
// checking to see if it is an interpolation
if (t.startsWith("{") && t.endsWith("}")) {
// append it to fnStr
const uuid = t.split(/{|}/).filter(Boolean)[0].trim().split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("");
fnStr = fnStr.substring(0, fnStr.length - 2) + ` data-token-${uuid}>\``;
fnStr += `+appState.contents.${t.split(/{|}/).filter(Boolean)[0].trim()}`;
} else {
// append the string to the fnStr
fnStr += `+\`${t}\``;
}
});
return fnStr;
}
var parse = (template) => {
let result = /{(.*?)}/g.exec(template);
const arr = [];
let firstPos;
while (result) {
firstPos = result.index;
if (firstPos !== 0) {
arr.push(template.substring(0, firstPos));
template = template.slice(firstPos);
}
arr.push(result[0]);
template = template.slice(result[0].length);
result = /{(.*?)}/g.exec(template);
}
if (template) arr.push(template);
return arr;
}