forked from DOSYCORPS/txtmode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b64order.js
41 lines (36 loc) · 830 Bytes
/
b64order.js
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
33
34
35
36
37
38
39
40
41
"use strict";
{
// This file contains code to convert between base64 strings and numbers
// basically providing a total ordering on base64 strings
const b64order = {
atoi, itoa
};
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const acode = chars.split('');
const icode = acode.reduce( (ro,c,i) => (ro[c] = i+1, ro), {} );
try {
Object.assign( self, {b64order} );
} catch(e) {
module.exports = b64order;
}
function atoi( b64s ) {
let num = 0;
let mult = 65;
for( const char of b64s ) {
num *= mult;
num += icode[char];
}
return num;
}
function itoa( i ) {
let s = '';
let n = i;
while( n ) {
const code = (n % 65);
s = acode[code-1] + s;
n -= code;
n /= 65;
}
return s;
}
}