From e93fa6110bfe510216b558e1b256fac4c267b1a2 Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Wed, 23 Mar 2022 21:46:28 +0100 Subject: sort of convert encoding to typescript --- webAO/encoding.ts | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 webAO/encoding.ts (limited to 'webAO/encoding.ts') 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, '') + .replace(/&/g, '') + .replace(/%/g, '') + .replace(/\$/g, ''); +} + +/** + * Unescapes a string to AO1 escape codes. + * @param {string} estring the string to be unescaped + */ +export function unescapeChat(estring: string) { + return estring + .replace(//g, '#') + .replace(//g, '&') + .replace(//g, '%') + .replace(//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(/ 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)); +} -- cgit From 810c1d22b1e52c1662bb3cac518ebfa14ea2d59f Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Wed, 23 Mar 2022 21:52:05 +0100 Subject: type the encoding functions --- webAO/encoding.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'webAO/encoding.ts') diff --git a/webAO/encoding.ts b/webAO/encoding.ts index 930886f..1018144 100644 --- a/webAO/encoding.ts +++ b/webAO/encoding.ts @@ -2,7 +2,7 @@ * Escapes a string to AO1 escape codes. * @param {string} estring the string to be escaped */ -export function escapeChat(estring: string) { +export function escapeChat(estring: string): string { return estring .replace(/#/g, '') .replace(/&/g, '') @@ -14,7 +14,7 @@ export function escapeChat(estring: string) { * Unescapes a string to AO1 escape codes. * @param {string} estring the string to be unescaped */ -export function unescapeChat(estring: string) { +export function unescapeChat(estring: string): string { return estring .replace(//g, '#') .replace(//g, '&') @@ -28,7 +28,7 @@ export function unescapeChat(estring: string) { * XXX: This is unnecessary if we use `createTextNode` instead! * @param {string} unsafe an unsanitized string */ -export function safeTags(unsafe: string) { +export function safeTags(unsafe: string): string { if (unsafe) { return unsafe .replace(/>/g, '>') @@ -41,7 +41,7 @@ export function safeTags(unsafe: string) { * Encode text on client side. * @param {string} estring the string to be encoded */ -export function encodeChat(estring: string) { +export function encodeChat(estring: string): string { return estring; } @@ -49,7 +49,7 @@ export function encodeChat(estring: string) { * Decodes text on client side. * @param {string} estring the string to be decoded */ -export function decodeChat(estring: string) { +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))); } @@ -58,7 +58,7 @@ export function decodeChat(estring: string) { * XXX: a nasty hack made by gameboyprinter. * @param {string} msg chat message to prepare for display */ -export function prepChat(msg: string) { +export function prepChat(msg: string): string { // TODO: make this less awful return unescapeChat(decodeChat(msg)); } -- cgit