-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
54 lines (44 loc) · 1.51 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
var methods = {
base64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
base64URL: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
base64URLNaturalSort: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_",
base64URLASCIISort: "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
};
var methodsEnum = {
BASE64: 'base64',
BASE64URL: 'base64URL',
BASE64NATURAL: 'base64URLNaturalSort',
BASE64ASCII: 'base64URLASCIISort',
DEFAULT: 'base64URL'
};
var convertOne = function(number, method) {
var tmp = number & 63;
var array = methods[method || methodsEnum.DEFAULT];
return array[number];
};
var radix64 = function(number, method) {
if (number < 64) {
return convertOne(number, method);
}
return '' + radix64( Math.floor(number/64) , method) + convertOne(number & 63, method);
}
var ascii64 = function(number, pad) {
pad = pad || 10;
function addpadding(pad) {
return (pad <= 0 ? '' : '-'+addpadding(pad - 1));
};
var toreturn = radix64(number, methodsEnum.BASE64ASCII);
if (toreturn.length < pad) {
toreturn = addpadding(pad - toreturn.length) + toreturn;
}
return toreturn;
}
module.exports = {
radix64: radix64,
methods: methodsEnum,
ascii64: ascii64,
base64 : function(n){ return radix64(n , methodsEnum.BASE64)},
base64URL : function(n){ return radix64(n , methodsEnum.BASE64URL)},
base64Natural : function(n){ return radix64(n , methodsEnum.BASE64NATURAL)},
base64ASCII : function(n){ return radix64(n , methodsEnum.BASE64ASCII)},
}