diff --git a/src/core/operations/ToBase32.mjs b/src/core/operations/ToBase32.mjs index 44eb8b48..def7183c 100644 --- a/src/core/operations/ToBase32.mjs +++ b/src/core/operations/ToBase32.mjs @@ -43,11 +43,19 @@ class ToBase32 extends Operation { if (!input) return ""; input = new Uint8Array(input); - const alphabet = args[0] ? Utils.expandAlphRange(args[0]).join("") : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567="; + const alphabet = args[0] + ? Utils.expandAlphRange(args[0]).join("") + : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567="; + + // Unicode-safe alphabet handling + // Supports BMP + non-BMP characters (emoji, Mahjong tiles, etc.) + const alphabetChars = Array.from(alphabet); + let output = "", chr1, chr2, chr3, chr4, chr5, enc1, enc2, enc3, enc4, enc5, enc6, enc7, enc8, i = 0; + while (i < input.length) { chr1 = input[i++]; chr2 = input[i++]; @@ -74,10 +82,19 @@ class ToBase32 extends Operation { enc8 = 32; } - output += alphabet.charAt(enc1) + alphabet.charAt(enc2) + alphabet.charAt(enc3) + - alphabet.charAt(enc4) + alphabet.charAt(enc5) + alphabet.charAt(enc6) + - alphabet.charAt(enc7) + alphabet.charAt(enc8); + // Preserve original charAt() behavior: + // out-of-range indexes return "" + output += + (alphabetChars[enc1] || "") + + (alphabetChars[enc2] || "") + + (alphabetChars[enc3] || "") + + (alphabetChars[enc4] || "") + + (alphabetChars[enc5] || "") + + (alphabetChars[enc6] || "") + + (alphabetChars[enc7] || "") + + (alphabetChars[enc8] || ""); } + return output; }