blob: 59c91ccd99940852a4f70170ebdf9796b94d95fc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
export const b2: string[] =
'01'.split('')
export const b16: string[] =
'0123456789abcdef'.split('')
export const b62: string[] =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
export function encode(n: bigint, charset: string[]): string {
const base = BigInt(charset.length)
if (n == BigInt(0)) {
return '0'
} else {
var xs = []
while (n > BigInt(0)) {
xs.push(charset[Number(n % base)])
n = n / base
}
return xs.reverse().join('')
}
}
export function decode(xs: string, charset: string[]): bigint {
const base = BigInt(charset.length)
return xs
.split('')
.reverse()
.reduce((acc, x, i) => acc + (BigInt(charset.indexOf(x)) * (base ** BigInt(i))), BigInt(0))
}
|