File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 5
5
* There are 62 possible letters and numbers,
6
6
* consisting of 26 lowercase letters, 26 uppercase letters, and 10 numbers (0 to 9).
7
7
*/
8
+
9
+ const DICTIONARY = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' . split ( '' ) ;
10
+
11
+ function encodeId ( id : number ) {
12
+ const base = DICTIONARY . length ;
13
+ let encoded : string | number = '' ;
14
+
15
+ if ( id === 0 ) {
16
+ return DICTIONARY [ 0 ] ;
17
+ }
18
+
19
+ while ( id > 0 ) {
20
+ encoded += DICTIONARY [ id % base ] ;
21
+ id = Math . floor ( id / base ) ;
22
+ }
23
+
24
+ return reverseWord ( encoded ) ;
25
+ }
26
+
27
+ function reverseWord ( str : string ) {
28
+ let reversed : string = '' ;
29
+ for ( let i = str . length - 1 ; i >= 0 ; i -- ) {
30
+ reversed += str . charAt ( i ) ;
31
+ }
32
+ return reversed ;
33
+ }
You can’t perform that action at this time.
0 commit comments