From ebea47cde84016cd7966534f35a8fa41c5c99d44 Mon Sep 17 00:00:00 2001 From: Spencer Gouw Date: Thu, 21 Sep 2023 22:03:46 -0500 Subject: [PATCH] feat: symbol replace and more advanced enum --- symbol.ts | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/symbol.ts b/symbol.ts index fdf8fa0..0539d2e 100644 --- a/symbol.ts +++ b/symbol.ts @@ -1,7 +1,37 @@ -export const enum Symbol { +export enum Symbol { // on parse Newline = "\\nl\\", Tab = "\\tab\\", // on replace CursorEnd = "\\end\\", } + +export namespace Symbol { + export const MAP: Record = { + [Symbol.Newline]: "\n", + [Symbol.Tab]: "\t", + [Symbol.CursorEnd]: "", + }; + export function replaceSymbols(inputStr: string): string { + const result: string[] = []; + let i = 0; + + while (i < inputStr.length) { + let found = false; + for (const symbol in MAP) { + if (inputStr.startsWith(symbol, i)) { + result.push(MAP[symbol]); + i += symbol.length; + found = true; + break; + } + } + if (!found) { + result.push(inputStr[i]); + i++; + } + } + + return result.join(""); + } +}