@@ -43,7 +43,14 @@ class ToBase32 extends Operation {
4343 if ( ! input ) return "" ;
4444 input = new Uint8Array ( input ) ;
4545
46- const alphabet = args [ 0 ] ? Utils . expandAlphRange ( args [ 0 ] ) . join ( "" ) : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=" ;
46+ const alphabet = args [ 0 ] ?
47+ Utils . expandAlphRange ( args [ 0 ] ) . join ( "" ) :
48+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=" ;
49+
50+ // Unicode-safe alphabet handling
51+ // Supports BMP + non-BMP characters (emoji, Mahjong tiles, etc.)
52+ const alphabetChars = Array . from ( alphabet ) ;
53+
4754 let output = "" ,
4855 chr1 , chr2 , chr3 , chr4 , chr5 ,
4956 enc1 , enc2 , enc3 , enc4 , enc5 , enc6 , enc7 , enc8 ,
@@ -74,10 +81,19 @@ class ToBase32 extends Operation {
7481 enc8 = 32 ;
7582 }
7683
77- output += alphabet . charAt ( enc1 ) + alphabet . charAt ( enc2 ) + alphabet . charAt ( enc3 ) +
78- alphabet . charAt ( enc4 ) + alphabet . charAt ( enc5 ) + alphabet . charAt ( enc6 ) +
79- alphabet . charAt ( enc7 ) + alphabet . charAt ( enc8 ) ;
84+ // Preserve original charAt() behavior:
85+ // out-of-range indexes return ""
86+ output +=
87+ ( alphabetChars [ enc1 ] || "" ) +
88+ ( alphabetChars [ enc2 ] || "" ) +
89+ ( alphabetChars [ enc3 ] || "" ) +
90+ ( alphabetChars [ enc4 ] || "" ) +
91+ ( alphabetChars [ enc5 ] || "" ) +
92+ ( alphabetChars [ enc6 ] || "" ) +
93+ ( alphabetChars [ enc7 ] || "" ) +
94+ ( alphabetChars [ enc8 ] || "" ) ;
8095 }
96+
8197 return output ;
8298 }
8399
0 commit comments