Comparing version
27
index.js
@@ -36,18 +36,13 @@ // base-x encoding | ||
var string = '' | ||
// deal with leading zeros | ||
for (var k = 0; source[k] === 0 && k < source.length - 1; ++k) { | ||
digits.push(0) | ||
} | ||
for (var k = 0; source[k] === 0 && k < source.length - 1; ++k) string += ALPHABET[0] | ||
// convert digits to a string | ||
for (var ii = 0, jj = digits.length - 1; ii <= jj; ++ii, --jj) { | ||
var tmp = ALPHABET[digits[ii]] | ||
digits[ii] = ALPHABET[digits[jj]] | ||
digits[jj] = tmp | ||
} | ||
for (var q = digits.length - 1; q >= 0; --q) string += ALPHABET[digits[q]] | ||
return digits.join('') | ||
return string | ||
} | ||
function decode (string) { | ||
function decodeUnsafe (string) { | ||
if (string.length === 0) return [] | ||
@@ -58,3 +53,3 @@ | ||
var value = ALPHABET_MAP[string[i]] | ||
if (value === undefined) throw new Error('Non-base' + BASE + ' character') | ||
if (value === undefined) return | ||
@@ -81,6 +76,14 @@ for (var j = 0, carry = value; j < bytes.length; ++j) { | ||
function decode (string) { | ||
var array = decodeUnsafe(string) | ||
if (array) return array | ||
throw new Error('Non-base' + BASE + ' character') | ||
} | ||
return { | ||
encode: encode, | ||
decodeUnsafe: decodeUnsafe, | ||
decode: decode | ||
} | ||
} |
{ | ||
"name": "base-x", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Fast base encoding / decoding of any given alphabet", | ||
@@ -32,3 +32,2 @@ "keywords": [ | ||
"scripts": { | ||
"benchmark:node": "node benchmark/benchmark.js", | ||
"standard": "standard", | ||
@@ -39,6 +38,5 @@ "test": "npm run unit && npm run standard", | ||
"devDependencies": { | ||
"benchmark": "^2.0.0", | ||
"standard": "^5.4.1", | ||
"standard": "^6.0.8", | ||
"tape": "^4.5.1" | ||
} | ||
} |
# base-x | ||
[](http://travis-ci.org/cryptocoinjs/base-x) | ||
[](https://www.npmjs.org/package/base-x) | ||
[](https://www.npmjs.org/package/base-x) | ||
[](https://travis-ci.org/cryptocoinjs/base-x) | ||
@@ -22,2 +22,5 @@ [](https://github.com/feross/standard) | ||
console.log(decoded) | ||
// => [128,237,219,220,17,104,241,218,234,219,211,228,76,10,106,249,133,131,164,153,222,91,25,19,164,248,99] | ||
console.log(Buffer.from(decoded)) | ||
// => <Buffer 80 ed db dc 11 68 f1 da ea db d3 e4 4c 1e 3f 8f 5a 28 4c 20 29 f7 8a d2 6a f9 85 83 a4 99 de 5b 19> | ||
@@ -35,2 +38,4 @@ | ||
------------- | ------------- | ||
2 | `01` | ||
8 | `01234567` | ||
11 | `0123456789a` | ||
@@ -37,0 +42,0 @@ 16 | `0123456789abcdef` |
6994
3.13%2
-33.33%69
1.47%86
6.17%