mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
Use soft-tabs instead of tabs, minor whitespace cleanup
This commit is contained in:
@@ -1,13 +1,10 @@
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
extends: 'flickr',
|
extends: 'flickr',
|
||||||
|
|
||||||
env: {
|
env: {
|
||||||
browser: true,
|
browser: true,
|
||||||
node: true
|
node: true
|
||||||
},
|
},
|
||||||
|
|
||||||
rules: {}
|
rules: {}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,54 +1,52 @@
|
|||||||
|
|
||||||
var alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
|
var alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
|
||||||
var base = alphabet.length;
|
var base = alphabet.length;
|
||||||
|
|
||||||
// Create a lookup table to fetch character index
|
// Create a lookup table to fetch character index
|
||||||
var alphabetLookup = alphabet.split('').reduce(function (lookup, char, index) {
|
var alphabetLookup = alphabet.split('').reduce(function (lookup, char, index) {
|
||||||
lookup[char] = index;
|
lookup[char] = index;
|
||||||
return lookup;
|
return lookup;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
function assertNonNegativeSafeInteger(val) {
|
function assertNonNegativeSafeInteger(val) {
|
||||||
if (typeof val !== 'number' || isNaN(val) || val < 0 || val > Number.MAX_SAFE_INTEGER || Math.floor(val) !== val) {
|
if (typeof val !== 'number' || isNaN(val) || val < 0 || val > Number.MAX_SAFE_INTEGER || Math.floor(val) !== val) {
|
||||||
throw new Error('Value passed is not a non-negative safe integer.');
|
throw new Error('Value passed is not a non-negative safe integer.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertString(str) {
|
function assertString(str) {
|
||||||
if (typeof str !== 'string') {
|
if (typeof str !== 'string') {
|
||||||
throw new Error('Value passed is not a string.');
|
throw new Error('Value passed is not a string.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertBase58Character(character) {
|
function assertBase58Character(character) {
|
||||||
if (alphabetLookup[character] === undefined) {
|
if (alphabetLookup[character] === undefined) {
|
||||||
throw new Error('Value passed is not a valid Base58 string.');
|
throw new Error('Value passed is not a valid Base58 string.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
exports.encode = function (num) {
|
exports.encode = function (num) {
|
||||||
var str = '';
|
var str = '';
|
||||||
var modulus;
|
var modulus;
|
||||||
|
|
||||||
num = Number(num);
|
num = Number(num);
|
||||||
|
|
||||||
assertNonNegativeSafeInteger(num);
|
assertNonNegativeSafeInteger(num);
|
||||||
|
|
||||||
while (num >= base) {
|
while (num >= base) {
|
||||||
modulus = num % base;
|
modulus = num % base;
|
||||||
str = alphabet[modulus] + str;
|
str = alphabet[modulus] + str;
|
||||||
num = Math.floor(num / base);
|
num = Math.floor(num / base);
|
||||||
}
|
}
|
||||||
|
|
||||||
return alphabet[num] + str;
|
return alphabet[num] + str;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.decode = function (str) {
|
exports.decode = function (str) {
|
||||||
assertString(str);
|
assertString(str);
|
||||||
|
|
||||||
return str.split('').reverse().reduce(function (num, character, index) {
|
return str.split('').reverse().reduce(function (num, character, index) {
|
||||||
assertBase58Character(character);
|
assertBase58Character(character);
|
||||||
return num + alphabetLookup[character] * Math.pow(base, index);
|
return num + alphabetLookup[character] * Math.pow(base, index);
|
||||||
}, 0);
|
}, 0);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,116 +1,113 @@
|
|||||||
|
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var examples = require('./examples');
|
var examples = require('./examples');
|
||||||
var base58 = require('..');
|
var base58 = require('..');
|
||||||
|
|
||||||
function exampleRunner(callback) {
|
function exampleRunner(callback) {
|
||||||
Object.keys(examples).forEach(function (str) {
|
Object.keys(examples).forEach(function (str) {
|
||||||
callback(str, examples[str]);
|
callback(str, examples[str]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('Base58', function () {
|
describe('Base58', function () {
|
||||||
|
before(function () {
|
||||||
|
var valid = true;
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
before(function () {
|
exampleRunner(function (str, num) {
|
||||||
var valid = true;
|
count++;
|
||||||
var count = 0;
|
if (typeof str !== 'string') {
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
if (typeof num !== 'number') {
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
exampleRunner(function (str, num) {
|
assert.strictEqual(count > 0, true, 'Expected there to be examples');
|
||||||
count++;
|
assert.strictEqual(valid, true, 'Expected the examples to be valid');
|
||||||
if (typeof str !== 'string') {
|
});
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
if (typeof num !== 'number') {
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.strictEqual(count > 0, true, 'Expected there to be examples');
|
describe('.encode', function () {
|
||||||
assert.strictEqual(valid, true, 'Expected the examples to be valid');
|
it('encodes number to Base58 string', function () {
|
||||||
});
|
exampleRunner(function (str, num) {
|
||||||
|
assert.strictEqual(base58.encode(num), str);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('.encode', function () {
|
describe('when passed a string only containing numbers', function () {
|
||||||
it('encodes number to Base58 string', function () {
|
it('encodes string after first converting it to an integer', function () {
|
||||||
exampleRunner(function (str, num) {
|
exampleRunner(function (str, num) {
|
||||||
assert.strictEqual(base58.encode(num), str);
|
assert.strictEqual(base58.encode(num.toString()), str);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('when passed a string only containing numbers', function () {
|
describe('when passed a non number', function () {
|
||||||
it('encodes string after first converting it to an integer', function () {
|
it('throws an error', function () {
|
||||||
exampleRunner(function (str, num) {
|
assert.throws(function () {
|
||||||
assert.strictEqual(base58.encode(num.toString()), str);
|
base58.encode('hi');
|
||||||
});
|
}, function (err) {
|
||||||
});
|
return err.message === 'Value passed is not a non-negative safe integer.';
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('when passed a non number', function () {
|
describe('when passed a float', function () {
|
||||||
it('throws an error', function () {
|
it('throws an error', function () {
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
base58.encode('hi');
|
base58.encode(3.14);
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
return err.message === 'Value passed is not a non-negative safe integer.';
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a float', function () {
|
describe('when passed a negative number', function () {
|
||||||
it('throws an error', function () {
|
it('throws an error', function () {
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
base58.encode(3.14);
|
base58.encode(-300);
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
return err.message === 'Value passed is not a non-negative safe integer.';
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a negative number', function () {
|
describe('when passed a non-safe integer', function () {
|
||||||
it('throws an error', function () {
|
it('throws an error', function () {
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
base58.encode(-300);
|
base58.encode(1E100);
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
return err.message === 'Value passed is not a non-negative safe integer.';
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('when passed a non-safe integer', function () {
|
describe('.decode', function () {
|
||||||
it('throws an error', function () {
|
it('decodes base58 string to number', function () {
|
||||||
assert.throws(function () {
|
exampleRunner(function (str, num) {
|
||||||
base58.encode(1E100);
|
assert.strictEqual(base58.decode(str), num);
|
||||||
}, function (err) {
|
});
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('.decode', function () {
|
describe('when passed a non string', function () {
|
||||||
it('decodes base58 string to number', function () {
|
it('throws an error', function () {
|
||||||
exampleRunner(function (str, num) {
|
assert.throws(function () {
|
||||||
assert.strictEqual(base58.decode(str), num);
|
base58.decode(123);
|
||||||
});
|
}, function (err) {
|
||||||
});
|
return err.message === 'Value passed is not a string.';
|
||||||
|
});
|
||||||
describe('when passed a non string', function () {
|
});
|
||||||
it('throws an error', function () {
|
});
|
||||||
assert.throws(function () {
|
|
||||||
base58.decode(123);
|
|
||||||
}, function (err) {
|
|
||||||
return err.message === 'Value passed is not a string.';
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('when passed a non base58 string', function () {
|
|
||||||
it('throws an error', function () {
|
|
||||||
assert.throws(function () {
|
|
||||||
base58.decode('>_<');
|
|
||||||
}, function (err) {
|
|
||||||
return err.message === 'Value passed is not a valid Base58 string.';
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
describe('when passed a non base58 string', function () {
|
||||||
|
it('throws an error', function () {
|
||||||
|
assert.throws(function () {
|
||||||
|
base58.decode('>_<');
|
||||||
|
}, function (err) {
|
||||||
|
return err.message === 'Value passed is not a valid Base58 string.';
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
'6hKMCS': 3471391110,
|
'6hKMCS': 3471391110,
|
||||||
'6hDrmR': 3470152229,
|
'6hDrmR': 3470152229,
|
||||||
|
|||||||
Reference in New Issue
Block a user