base58: cleanup variable names
The encode/decode functions are also now similar syntactically.
This commit is contained in:
parent
61c57adbf8
commit
77d4325d4b
1 changed files with 8 additions and 8 deletions
|
@ -12,7 +12,7 @@ var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
||||||
var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii')
|
var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii')
|
||||||
var ALPHABET_MAP = {}
|
var ALPHABET_MAP = {}
|
||||||
for(var i = 0; i < ALPHABET.length; i++) {
|
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)
|
var BASE = BigInteger.valueOf(58)
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ function encode(buffer) {
|
||||||
function decode(string) {
|
function decode(string) {
|
||||||
if (string.length === 0) return new Buffer(0)
|
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++) {
|
for (var i = 0; i < string.length; i++) {
|
||||||
num = num.multiply(BASE)
|
num = num.multiply(BASE)
|
||||||
|
@ -55,16 +55,16 @@ function decode(string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// deal with leading zeros
|
// deal with leading zeros
|
||||||
var i = 0
|
var j = 0
|
||||||
while ((i < string.length) && (string[i] === ALPHABET[0])) {
|
while ((j < string.length) && (string[j] === ALPHABET[0])) {
|
||||||
i++
|
j++
|
||||||
}
|
}
|
||||||
|
|
||||||
var buffer = num.toBuffer()
|
var buffer = num.toBuffer()
|
||||||
var leadz = new Buffer(i)
|
var leadingZeros = new Buffer(j)
|
||||||
leadz.fill(0)
|
leadingZeros.fill(0)
|
||||||
|
|
||||||
return Buffer.concat([leadz, buffer])
|
return Buffer.concat([leadingZeros, buffer])
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Add table
Reference in a new issue