|
|
@ -12,7 +12,7 @@ var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' |
|
|
|
var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii') |
|
|
|
var ALPHABET_MAP = {} |
|
|
|
for(var i = 0; i < ALPHABET.length; i++) { |
|
|
|
ALPHABET_MAP[ALPHABET[i]] = BigInteger.valueOf(i) |
|
|
|
ALPHABET_MAP[ALPHABET.charAt(i)] = BigInteger.valueOf(i) |
|
|
|
} |
|
|
|
var BASE = BigInteger.valueOf(58) |
|
|
|
|
|
|
@ -21,7 +21,7 @@ function encode(buffer) { |
|
|
|
var result = new Buffer(buffer.length << 1) |
|
|
|
|
|
|
|
var i = result.length - 1 |
|
|
|
while (bi.compareTo(BigInteger.ZERO) > 0) { |
|
|
|
while (bi.signum() > 0) { |
|
|
|
var remainder = bi.mod(BASE) |
|
|
|
bi = bi.divide(BASE) |
|
|
|
|
|
|
@ -43,7 +43,7 @@ function encode(buffer) { |
|
|
|
function decode(string) { |
|
|
|
if (string.length === 0) return new Buffer(0) |
|
|
|
|
|
|
|
var num = BigInteger.ZERO.clone() |
|
|
|
var num = BigInteger.ZERO |
|
|
|
|
|
|
|
for (var i = 0; i < string.length; i++) { |
|
|
|
num = num.multiply(BASE) |
|
|
@ -55,16 +55,16 @@ function decode(string) { |
|
|
|
} |
|
|
|
|
|
|
|
// deal with leading zeros
|
|
|
|
var i = 0 |
|
|
|
while ((i < string.length) && (string[i] === ALPHABET[0])) { |
|
|
|
i++ |
|
|
|
var j = 0 |
|
|
|
while ((j < string.length) && (string[j] === ALPHABET[0])) { |
|
|
|
j++ |
|
|
|
} |
|
|
|
|
|
|
|
var buffer = num.toBuffer() |
|
|
|
var leadz = new Buffer(i) |
|
|
|
leadz.fill(0) |
|
|
|
var leadingZeros = new Buffer(j) |
|
|
|
leadingZeros.fill(0) |
|
|
|
|
|
|
|
return Buffer.concat([leadz, buffer]) |
|
|
|
return Buffer.concat([leadingZeros, buffer]) |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
|
|