-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
54 lines (42 loc) · 1.21 KB
/
common.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
'use strict';
var toUrlRe = /[+/=]/g;
var toOriginalRe = /[-_]/g;
var originalToUrl = function (encoded) {
return encoded.replace(toUrlRe, function (match) {
if (match == '=')
return '';
else if (match == '+')
return '-';
else if (match == '/')
return '_';
});
};
var urlRoOriginal = function (encoded) {
return padBase64(encoded.replace(toOriginalRe, function (match) {
if (match == '-')
return '+';
else if (match == '_')
return '/';
}));
};
var padBase64 = function (encoded) {
var missing = 4 - (encoded.length % 4);
if (missing !== 4) {
encoded += "====".slice(0, missing);
}
return encoded;
}
module.exports = function (encode, decode) {
var _module = {};
_module.originalToUrl = originalToUrl;
_module.urlToOriginal = urlRoOriginal;
_module.encode = encode;
_module.decode = decode;
_module.encodeUrl = function (source, inputEncoding) {
return originalToUrl(encode(source, inputEncoding));
};
_module.decodeUrl = function (encoded, outputEncoding) {
return decode(urlRoOriginal(encoded), outputEncoding);
};
return _module;
}