From 4ce43a304fc90100eb70134f58b7045d8e8ab45b Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Fri, 4 Apr 2025 09:39:25 +0300 Subject: [PATCH] utils: modernize hex functions Signed-off-by: XhmikosR --- scripts/js/utils.js | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/scripts/js/utils.js b/scripts/js/utils.js index 1ee7e542..8932e600 100644 --- a/scripts/js/utils.js +++ b/scripts/js/utils.js @@ -513,26 +513,19 @@ function parseQueryString() { return Object.fromEntries(params.entries()); } -// https://stackoverflow.com/q/21647928 -function hexEncode(string) { - let result = ""; - for (let i = 0; i < string.length; i++) { - const hex = string.codePointAt(i).toString(16); - result += ("000" + hex).slice(-4); - } +function hexEncode(text) { + if (typeof text !== "string" || text.length === 0) return ""; - return result; + return [...text].map(char => char.codePointAt(0).toString(16).padStart(4, "0")).join(""); } -// https://stackoverflow.com/q/21647928 -function hexDecode(string) { - const hexes = string.match(/.{1,4}/g) || []; - let back = ""; - for (const hex of hexes) { - back += String.fromCodePoint(Number.parseInt(hex, 16)); - } +function hexDecode(text) { + if (typeof text !== "string" || text.length === 0) return ""; - return back; + const hexes = text.match(/.{1,4}/g); + if (!hexes || hexes.length === 0) return ""; + + return hexes.map(hex => String.fromCodePoint(Number.parseInt(hex, 16))).join(""); } function listsAlert(type, items, data) {