mirror of
https://github.com/rabirabirara/obsidian-jelly-snippets.git
synced 2026-07-22 07:30:22 +00:00
feat: symbol replace and more advanced enum
This commit is contained in:
parent
18e4c2e28f
commit
ebea47cde8
1 changed files with 31 additions and 1 deletions
32
symbol.ts
32
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<string, string> = {
|
||||
[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("");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue