aboutsummaryrefslogtreecommitdiff
path: root/webAO/encoding.js
diff options
context:
space:
mode:
authorstonedDiscord <Tukz@gmx.de>2022-03-06 20:19:45 +0100
committerGitHub <noreply@github.com>2022-03-06 20:19:45 +0100
commitccbca8b9cede972480dd1ef7a73db71a99be9609 (patch)
treee3e3c5b9232d86cf8ba18f898a90f1f2295c2b4e /webAO/encoding.js
parent8d4cd02b7cbe955f5a5ce1666fc76a51db4be5d5 (diff)
parent59c0773e95756aa62027db7a5754e7e986ca2b19 (diff)
Merge pull request #94 from caleb-mabry/add-linting
Adding AirBNB Linting style
Diffstat (limited to 'webAO/encoding.js')
-rw-r--r--webAO/encoding.js97
1 files changed, 45 insertions, 52 deletions
diff --git a/webAO/encoding.js b/webAO/encoding.js
index 04a44e7..21b5aa7 100644
--- a/webAO/encoding.js
+++ b/webAO/encoding.js
@@ -3,11 +3,11 @@
* @param {string} estring the string to be escaped
*/
export function escapeChat(estring) {
- return estring
- .replace(/#/g, "<num>")
- .replace(/&/g, "<and>")
- .replace(/%/g, "<percent>")
- .replace(/\$/g, "<dollar>");
+ return estring
+ .replace(/#/g, '<num>')
+ .replace(/&/g, '<and>')
+ .replace(/%/g, '<percent>')
+ .replace(/\$/g, '<dollar>');
}
/**
@@ -15,27 +15,26 @@ export function escapeChat(estring) {
* @param {string} estring the string to be unescaped
*/
export function unescapeChat(estring) {
- return estring
- .replace(/<num>/g, "#")
- .replace(/<and>/g, "&")
- .replace(/<percent>/g, "%")
- .replace(/<dollar>/g, "$");
+ 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 safe_tags(unsafe) {
- if (unsafe) {
- return unsafe
- .replace(/>/g, '&gt;')
- .replace(/</g, '&lt;');
- } else {
- return "";
- }
+ if (unsafe) {
+ return unsafe
+ .replace(/>/g, '&gt;')
+ .replace(/</g, '&lt;');
+ }
+ return '';
}
/**
@@ -43,24 +42,21 @@ export function safe_tags(unsafe) {
* @param {string} estring the string to be encoded
*/
export function encodeChat(estring) {
- const selectedEncoding = document.getElementById("client_encoding").value;
- if (selectedEncoding === "unicode") {
- // This approach works by escaping all special characters to Unicode escape sequences.
- // Source: https://gist.github.com/mathiasbynens/1243213
- return estring.replace(/[^\0-~]/g, function (ch) {
- return "\\u" + ("000" + ch.charCodeAt().toString(16)).slice(-4);
- });
- } else if (selectedEncoding === "utf16") {
- // Source: https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
- const buffer = new ArrayBuffer(estring.length * 2);
- const result = new Uint16Array(buffer);
- for (let i = 0, strLen = estring.length; i < strLen; i++) {
- result[i] = estring.charCodeAt(i);
- }
- return String(result);
- } else {
- return estring;
- }
+ const selectedEncoding = document.getElementById('client_encoding').value;
+ if (selectedEncoding === 'unicode') {
+ // This approach works by escaping all special characters to Unicode escape sequences.
+ // Source: https://gist.github.com/mathiasbynens/1243213
+ return estring.replace(/[^\0-~]/g, (ch) => `\\u${(`000${ch.charCodeAt().toString(16)}`).slice(-4)}`);
+ } if (selectedEncoding === 'utf16') {
+ // Source: https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
+ const buffer = new ArrayBuffer(estring.length * 2);
+ const result = new Uint16Array(buffer);
+ for (let i = 0, strLen = estring.length; i < strLen; i++) {
+ result[i] = estring.charCodeAt(i);
+ }
+ return String(result);
+ }
+ return estring;
}
/**
@@ -68,25 +64,22 @@ export function encodeChat(estring) {
* @param {string} estring the string to be decoded
*/
export function decodeChat(estring) {
- const selectedDecoding = document.getElementById("client_decoding").value;
- if (selectedDecoding === "unicode") {
- // Source: https://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode
- return estring.replace(/\\u([\d\w]{1,})/gi, function (match, group) {
- return String.fromCharCode(parseInt(group, 16));
- });
- } else if (selectedDecoding === "utf16") {
- // Source: https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
- return String.fromCharCode.apply(null, new Uint16Array(estring.split(",")));
- } else {
- return estring;
- }
+ const selectedDecoding = document.getElementById('client_decoding').value;
+ if (selectedDecoding === 'unicode') {
+ // 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)));
+ } if (selectedDecoding === 'utf16') {
+ // Source: https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
+ return String.fromCharCode.apply(null, new Uint16Array(estring.split(',')));
+ }
+ return estring;
}
/**
* XXX: a nasty hack made by gameboyprinter.
- * @param {string} msg chat message to prepare for display
+ * @param {string} msg chat message to prepare for display
*/
export function prepChat(msg) {
- // TODO: make this less awful
- return unescapeChat(decodeChat(msg));
-} \ No newline at end of file
+ // TODO: make this less awful
+ return unescapeChat(decodeChat(msg));
+}