svelte render: simplify replaces

This commit is contained in:
Alice Gaudon 2021-04-28 17:03:27 +02:00
parent 05069b15d8
commit bd2b7e7579

View File

@ -104,42 +104,20 @@ export default class SvelteViewEngine extends ViewEngine {
const props = JSON.stringify(localMap); const props = JSON.stringify(localMap);
// Replaces // Replaces
const replaces: { [key: string]: string } = { const replaceMap: Record<string, string> = {
canonicalViewName: canonicalViewName, canonicalViewName: canonicalViewName,
props: props, props: props,
head: head, head: head,
html: html, html: html,
css: css, css: css,
}; };
const replaceOperations: Record<number, { return rawOutput.replace(
key: string; new RegExp(Object.keys(replaceMap).map(str => `%${str}%`).join('|'), 'g'),
replaceValue: string; (substring) => {
} | undefined> = {}; console.log(substring);
for (const entry of Object.entries(replaces)) { return replaceMap[substring.slice(1, substring.length - 1)];
const matches = rawOutput.matchAll(new RegExp(`%${entry[0]}%`, 'g')); },
for (const match of matches) { );
if (typeof match.index === 'number') {
replaceOperations[match.index] = {
key: `%${entry[0]}%`,
replaceValue: entry[1],
};
}
}
}
let output = '';
for (let i = 0; i < rawOutput.length; i++) {
const replaceOperation = replaceOperations[i];
if (replaceOperation) {
output += replaceOperation.replaceValue;
i += replaceOperation.key.length - 1;
} else {
output += rawOutput[i];
}
}
return output;
} }
public async stop(): Promise<void> { public async stop(): Promise<void> {