aboutsummaryrefslogtreecommitdiff
path: root/webAO/encoding.ts
diff options
context:
space:
mode:
authorstonedDiscord <Tukz@gmx.de>2022-03-28 23:41:56 +0200
committerstonedDiscord <Tukz@gmx.de>2022-03-28 23:41:56 +0200
commit903a13ff5b5beaaf42b4fcf8965eb55b642e1d2a (patch)
tree7e7cbf09c1ce6d2539bb2f33a9c3c86f19ce9d48 /webAO/encoding.ts
parent06ee582c4adefdb35220c63ee4a30444474e9388 (diff)
parentd3911aa9ad6bc16c70355fe11d1377d636b14565 (diff)
Merge branch 'master' into coolloading
Diffstat (limited to 'webAO/encoding.ts')
-rw-r--r--webAO/encoding.ts64
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, '&gt;')
+ .replace(/</g, '&lt;');
+ }
+ 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));
+}