diff options
| author | stonedDiscord <Tukz@gmx.de> | 2022-03-23 22:25:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-23 22:25:15 +0100 |
| commit | ffa23343e0a0badd9e50a005359fdb79efead995 (patch) | |
| tree | b5991b499685bd94890fae7f321570044fafaf2c /webAO/encoding.ts | |
| parent | 30b4e3e8c84ce3f980fe895d4c64800fc7f00ece (diff) | |
| parent | 93212647c7775358ecd7366423d706a5efae95dc (diff) | |
Merge pull request #128 from AttorneyOnline/sypetcript
Sypetcript
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..1018144 --- /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): 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): 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): 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): string { + return estring; +} + +/** + * Decodes text on client side. + * @param {string} estring the string to be decoded + */ +export function decodeChat(estring: string): 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): string { + // TODO: make this less awful + return unescapeChat(decodeChat(msg)); +} |
