add quick and dirty decode function to handle escape characters in values

This commit is contained in:
Chris Kruining 2025-01-07 12:11:32 +01:00
parent 7264146d49
commit a31168ca6c
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
2 changed files with 17 additions and 2 deletions

View file

@ -10,6 +10,19 @@ export const splitAt = (subject: string, index: number): readonly [string, strin
return [subject.slice(0, index), subject.slice(index + 1)];
};
const decodeRegex = /(?<!\\)\\(t|b|n|r|f|'|"|u[0-9a-f]{1,4})/gi;
const decodeReplacer = (_: any, char: string) => ({
t: '\t',
b: '\b',
n: '\n',
r: '\r',
f: '\f',
"'": '\'',
'"': '\"',
u: String.fromCharCode(Number.parseInt(char.slice(1))),
}[char.charAt(0)] ?? '');
export const decode = (subject: string): string => subject.replace(decodeRegex, decodeReplacer);
export const debounce = <T extends (...args: any[]) => void>(callback: T, delay: number): ((...args: Parameters<T>) => void) => {
let handle: ReturnType<typeof setTimeout> | undefined;