From bd2b7e7579feed695d7bae0fc247124705365821 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 28 Apr 2021 17:03:27 +0200 Subject: [PATCH] svelte render: simplify replaces --- src/frontend/SvelteViewEngine.ts | 38 +++++++------------------------- 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/src/frontend/SvelteViewEngine.ts b/src/frontend/SvelteViewEngine.ts index 824cc46..e4fdde1 100644 --- a/src/frontend/SvelteViewEngine.ts +++ b/src/frontend/SvelteViewEngine.ts @@ -104,42 +104,20 @@ export default class SvelteViewEngine extends ViewEngine { const props = JSON.stringify(localMap); // Replaces - const replaces: { [key: string]: string } = { + const replaceMap: Record = { canonicalViewName: canonicalViewName, props: props, head: head, html: html, css: css, }; - const replaceOperations: Record = {}; - for (const entry of Object.entries(replaces)) { - 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; + return rawOutput.replace( + new RegExp(Object.keys(replaceMap).map(str => `%${str}%`).join('|'), 'g'), + (substring) => { + console.log(substring); + return replaceMap[substring.slice(1, substring.length - 1)]; + }, + ); } public async stop(): Promise {