2017-08-05 20:14:17 +00:00
|
|
|
// var alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
|
|
const alphabet = 'bMJZSrnxEyq8kN3UYQL5oXwV7BCFRtvpmDf1shAuHzKicTjeG29Pg4adW6';
|
|
|
|
|
|
|
|
const base = alphabet.length;
|
2016-02-07 21:16:07 +00:00
|
|
|
|
2017-09-25 16:48:55 +00:00
|
|
|
function encode(num) {
|
|
|
|
let encoded = '';
|
|
|
|
while (num) {
|
|
|
|
const remainder = num % base;
|
|
|
|
num = Math.floor(num / base);
|
2016-02-07 21:16:07 +00:00
|
|
|
encoded = alphabet[remainder].toString() + encoded;
|
|
|
|
}
|
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:48:55 +00:00
|
|
|
function decode(str) {
|
|
|
|
let decoded = 0;
|
|
|
|
while (str) {
|
|
|
|
const index = alphabet.indexOf(str[0]);
|
|
|
|
const power = str.length - 1;
|
|
|
|
decoded += index * (Math.pow(base, power));
|
2016-02-07 21:16:07 +00:00
|
|
|
str = str.substring(1);
|
|
|
|
}
|
|
|
|
return decoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.encode = encode;
|
|
|
|
module.exports.decode = decode;
|