diff options
| author | stonedDiscord <Tukz@gmx.de> | 2022-03-23 21:46:28 +0100 |
|---|---|---|
| committer | stonedDiscord <Tukz@gmx.de> | 2022-03-23 21:46:28 +0100 |
| commit | e93fa6110bfe510216b558e1b256fac4c267b1a2 (patch) | |
| tree | 3bf74af19609e3991cb9d665375fce47b0845d89 /webAO/encoding.ts | |
| parent | 89a7862ed462bd952a9814d9d4be078524bcf18c (diff) | |
sort of convert encoding to typescript
Diffstat (limited to 'webAO/encoding.ts')
| -rw-r--r-- | webAO/encoding.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/webAO/encoding.ts b/webAO/encoding.ts new file mode 100644 index 0000000..930886f --- /dev/null +++ b/webAO/encoding.ts @@ -0,0 +1,64 @@ +/** + * Escapes a string to AO1 escape codes. + * @param {string} estring the string to be escaped + */ +export function escapeChat(estring: string) { + return estring + .replace(/#/g, '<num>') + .replace(/&/g, '<and>') + .replace(/%/g, '<percent>') + .replace(/\$/g, '<dollar>'); +} + +/** + * Unescapes a string to AO1 escape codes. + * @param {string} estring the string to be unescaped + */ +export function unescapeChat(estring: string) { + return estring + .replace(/<num>/g, '#') + .replace(/<and>/g, '&') + .replace(/<percent>/g, '%') + .replace(/<dollar>/g, '$'); +} + +/** + * Escapes a string to be HTML-safe. + * + * XXX: This is unnecessary if we use `createTextNode` instead! + * @param {string} unsafe an unsanitized string + */ +export function safeTags(unsafe: string) { + if (unsafe) { + return unsafe + .replace(/>/g, '>') + .replace(/</g, '<'); + } + return ''; +} + +/** + * Encode text on client side. + * @param {string} estring the string to be encoded + */ +export function encodeChat(estring: string) { + return estring; +} + +/** + * Decodes text on client side. + * @param {string} estring the string to be decoded + */ +export function decodeChat(estring: string) { + // Source: https://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode + return estring.replace(/\\u([\d\w]{1,})/gi, (match, group) => String.fromCharCode(parseInt(group, 16))); +} + +/** + * XXX: a nasty hack made by gameboyprinter. + * @param {string} msg chat message to prepare for display + */ +export function prepChat(msg: string) { + // TODO: make this less awful + return unescapeChat(decodeChat(msg)); +} |
