mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ddfa8632e7 | |||
| 24f4d2af29 | |||
| 3ea2aa4939 | |||
| 1751b9930d | |||
| 97d3366fb6 | |||
| 62b8f73f82 | |||
| 1241478006 | |||
| 0874ee4087 | |||
| 3dbac7b5d4 | |||
| 7e8e25b617 | |||
| a7f443a25f | |||
| 8eaae0b86a | |||
| 35dd6f57a1 | |||
|
|
2fa51650b8 | ||
|
|
8fe37d2db5 | ||
|
|
8aef7a5081 | ||
|
|
39a0f67e35 | ||
|
|
deb7b6892c | ||
|
|
47c42ff4a5 | ||
| 637ec5c6da | |||
| 5f6cc4c169 | |||
| ffc71e6c1a | |||
| 9be24ba511 | |||
| 51d70572b1 | |||
| 96b27b9505 | |||
| ed8cbd3fa2 |
12
.eslintrc.js
12
.eslintrc.js
@@ -1,10 +1,14 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
extends: 'flickr',
|
|
||||||
|
|
||||||
env: {
|
env: {
|
||||||
browser: true,
|
browser: true,
|
||||||
node: true
|
node: true
|
||||||
},
|
},
|
||||||
|
extends: ["prettier"],
|
||||||
rules: {}
|
plugins: ["prettier"],
|
||||||
|
rules: {
|
||||||
|
"prettier/prettier": "error"
|
||||||
|
},
|
||||||
|
parserOptions: {
|
||||||
|
"ecmaVersion": 6
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,2 +1,6 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
|
.eslintrc.js
|
||||||
|
.travis.yml
|
||||||
|
Makefile
|
||||||
node_modules
|
node_modules
|
||||||
|
package-lock.json
|
||||||
|
|||||||
17
.travis.yml
17
.travis.yml
@@ -1,5 +1,16 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
|
cache:
|
||||||
|
directories:
|
||||||
|
- node_modules
|
||||||
node_js:
|
node_js:
|
||||||
- 4
|
- "node"
|
||||||
- 6
|
- "6"
|
||||||
- 8
|
- "7"
|
||||||
|
- "8"
|
||||||
|
- "9"
|
||||||
|
- "10"
|
||||||
|
- "11"
|
||||||
|
|
||||||
|
script:
|
||||||
|
- npm run lint
|
||||||
|
- npm test
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2012 Jim Myhrberg.
|
Copyright (c) 2018 Jim Myhrberg.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
a copy of this software and associated documentation files (the
|
a copy of this software and associated documentation files (the
|
||||||
|
|||||||
15
Makefile
15
Makefile
@@ -1,22 +1,13 @@
|
|||||||
NPM_EXECUTABLE_HOME := node_modules/.bin
|
NPM_EXECUTABLE_HOME := node_modules/.bin
|
||||||
PATH := ${NPM_EXECUTABLE_HOME}:${PATH}
|
PATH := ${NPM_EXECUTABLE_HOME}:${PATH}
|
||||||
|
|
||||||
hooks: .git/hooks/pre-commit
|
publish: npm-dep
|
||||||
hooks: .git/hooks/pre-push
|
|
||||||
|
|
||||||
.git/hooks/pre-commit: hook.sh
|
|
||||||
cp $< $@
|
|
||||||
|
|
||||||
.git/hooks/pre-push: hook.sh
|
|
||||||
cp $< $@
|
|
||||||
|
|
||||||
publish:
|
|
||||||
npm publish
|
npm publish
|
||||||
|
|
||||||
test:
|
test: npm-dep
|
||||||
npm run test
|
npm run test
|
||||||
|
|
||||||
lint:
|
lint: npm-dep
|
||||||
npm run lint
|
npm run lint
|
||||||
|
|
||||||
npm-dep:
|
npm-dep:
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ Flickr short URL is: `http://flic.kr/p/brXijP`
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var Base58 = require('base58');
|
const Base58 = require('base58');
|
||||||
Base58.encode(6857269519); // 'brXijP'
|
Base58.int_to_base58(6857269519); // 'brXijP'
|
||||||
Base58.decode('brXijP'); // 6857269519
|
Base58.base58_to_int('brXijP'); // 6857269519
|
||||||
```
|
```
|
||||||
|
|
||||||
## Credit
|
## Credit
|
||||||
@@ -34,4 +34,4 @@ name.
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Released under the MIT license. Copyright (c) 2012 Jim Myhrberg.
|
Released under the MIT license. Copyright (c) 2018 Jim Myhrberg.
|
||||||
|
|||||||
8
hook.sh
8
hook.sh
@@ -1,8 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
set -ex
|
|
||||||
|
|
||||||
npm run lint
|
|
||||||
npm run test
|
|
||||||
|
|
||||||
node package.json
|
|
||||||
1165
package-lock.json
generated
Normal file
1165
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
28
package.json
28
package.json
@@ -1,14 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "base58",
|
"name": "base58",
|
||||||
"version": "1.0.1",
|
"version": "2.0.1",
|
||||||
"keywords": "base58, flickr",
|
"keywords": [
|
||||||
"description": "Flickr Flavored Base58 Encoding and Decoding",
|
"base58, flickr"
|
||||||
"licenses": [
|
|
||||||
{
|
|
||||||
"type": "MIT",
|
|
||||||
"url": "https://raw.github.com/jimeh/node-base58/master/LICENSE.md"
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
|
"description": "Flickr Flavored Base58 Encoding and Decoding",
|
||||||
|
"license": "MIT",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Jim Myhrberg",
|
"name": "Jim Myhrberg",
|
||||||
"email": "contact@jimeh.me"
|
"email": "contact@jimeh.me"
|
||||||
@@ -22,15 +19,18 @@
|
|||||||
},
|
},
|
||||||
"main": "./src/base58",
|
"main": "./src/base58",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 4"
|
"node": ">= 6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^4.1.1",
|
"eslint": "^5.6.0",
|
||||||
"eslint-config-flickr": "^1.3.1",
|
"eslint-config-prettier": "^3.0.0",
|
||||||
"mocha": "^3.4.2"
|
"eslint-plugin-prettier": "^2.7.0",
|
||||||
|
"mocha": "^5.2.0",
|
||||||
|
"prettier": "^1.14.3"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"lint": "eslint . --fix",
|
"lint": "eslint .",
|
||||||
"test": "mocha test"
|
"lint-fix": "eslint . --fix",
|
||||||
|
"test": "mocha"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,39 @@
|
|||||||
var alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
|
const alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
|
||||||
var base = alphabet.length;
|
const 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) {
|
const alphabetLookup = [...alphabet].reduce((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 (
|
||||||
throw new Error('Value passed is not a non-negative safe integer.');
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.int_to_base58 = exports.encode = function(num) {
|
||||||
var str = '';
|
let str = "";
|
||||||
var modulus;
|
let modulus;
|
||||||
|
|
||||||
num = Number(num);
|
num = Number(num);
|
||||||
|
|
||||||
@@ -42,10 +48,10 @@ exports.encode = function (num) {
|
|||||||
return alphabet[num] + str;
|
return alphabet[num] + str;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.decode = function (str) {
|
exports.base58_to_int = exports.decode = function(str) {
|
||||||
assertString(str);
|
assertString(str);
|
||||||
|
|
||||||
return str.split('').reverse().reduce(function (num, character, index) {
|
return [...str].reverse().reduce((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,6 +1,8 @@
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
env: {
|
env: {
|
||||||
mocha: true
|
mocha: true
|
||||||
|
},
|
||||||
|
parserOptions: {
|
||||||
|
"ecmaVersion": 6
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,112 +1,148 @@
|
|||||||
var assert = require('assert');
|
const assert = require("assert");
|
||||||
var examples = require('./examples');
|
const examples = require("./examples");
|
||||||
var base58 = require('..');
|
const 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 () {
|
before(function() {
|
||||||
var valid = true;
|
var valid = true;
|
||||||
var count = 0;
|
var count = 0;
|
||||||
|
|
||||||
exampleRunner(function (str, num) {
|
exampleRunner(function(str, num) {
|
||||||
count++;
|
count++;
|
||||||
if (typeof str !== 'string') {
|
if (typeof str !== "string") {
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
if (typeof num !== 'number') {
|
if (typeof num !== "number") {
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.strictEqual(count > 0, true, 'Expected there to be examples');
|
assert.strictEqual(count > 0, true, "Expected there to be examples");
|
||||||
assert.strictEqual(valid, true, 'Expected the examples to be valid');
|
assert.strictEqual(valid, true, "Expected the examples to be valid");
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('.encode', function () {
|
describe("backwards compatibility", function() {
|
||||||
it('encodes number to Base58 string', function () {
|
it(".encode() is an alias to .int_to_base58()", function() {
|
||||||
exampleRunner(function (str, num) {
|
assert.strictEqual(base58.encode, base58.int_to_base58);
|
||||||
assert.strictEqual(base58.encode(num), str);
|
});
|
||||||
|
|
||||||
|
it(".decode() is an alias to .base58_to_int()", function() {
|
||||||
|
assert.strictEqual(base58.decode, base58.base58_to_int);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(".int_to_base58()", function() {
|
||||||
|
it("encodes number to Base58 string", function() {
|
||||||
|
exampleRunner(function(str, num) {
|
||||||
|
assert.strictEqual(base58.int_to_base58(num), str);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a string only containing numbers', function () {
|
describe("when passed a string only containing numbers", function() {
|
||||||
it('encodes string after first converting it to an integer', 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.toString()), str);
|
assert.strictEqual(base58.int_to_base58(num.toString()), str);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a non number', function () {
|
describe("when passed a non number", function() {
|
||||||
it('throws an error', function () {
|
it("throws an error", function() {
|
||||||
assert.throws(function () {
|
assert.throws(
|
||||||
base58.encode('hi');
|
function() {
|
||||||
}, function (err) {
|
base58.int_to_base58("hi");
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
},
|
||||||
});
|
function(err) {
|
||||||
|
return (
|
||||||
|
err.message === "Value passed is not a non-negative safe integer."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a float', function () {
|
describe("when passed a float", function() {
|
||||||
it('throws an error', function () {
|
it("throws an error", function() {
|
||||||
assert.throws(function () {
|
assert.throws(
|
||||||
base58.encode(3.14);
|
function() {
|
||||||
}, function (err) {
|
base58.int_to_base58(3.14);
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
},
|
||||||
});
|
function(err) {
|
||||||
|
return (
|
||||||
|
err.message === "Value passed is not a non-negative safe integer."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a negative number', function () {
|
describe("when passed a negative number", function() {
|
||||||
it('throws an error', function () {
|
it("throws an error", function() {
|
||||||
assert.throws(function () {
|
assert.throws(
|
||||||
base58.encode(-300);
|
function() {
|
||||||
}, function (err) {
|
base58.int_to_base58(-300);
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
},
|
||||||
});
|
function(err) {
|
||||||
|
return (
|
||||||
|
err.message === "Value passed is not a non-negative safe integer."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a non-safe integer', function () {
|
describe("when passed a non-safe integer", function() {
|
||||||
it('throws an error', function () {
|
it("throws an error", function() {
|
||||||
assert.throws(function () {
|
assert.throws(
|
||||||
base58.encode(1E100);
|
function() {
|
||||||
}, function (err) {
|
base58.int_to_base58(1e100);
|
||||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
},
|
||||||
});
|
function(err) {
|
||||||
|
return (
|
||||||
|
err.message === "Value passed is not a non-negative safe integer."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('.decode', function () {
|
describe(".base58_to_int()", function() {
|
||||||
it('decodes base58 string to number', function () {
|
it("decodes base58 string to number", function() {
|
||||||
exampleRunner(function (str, num) {
|
exampleRunner(function(str, num) {
|
||||||
assert.strictEqual(base58.decode(str), num);
|
assert.strictEqual(base58.base58_to_int(str), num);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a non string', function () {
|
describe("when passed a non string", function() {
|
||||||
it('throws an error', function () {
|
it("throws an error", function() {
|
||||||
assert.throws(function () {
|
assert.throws(
|
||||||
base58.decode(123);
|
function() {
|
||||||
}, function (err) {
|
base58.base58_to_int(123);
|
||||||
return err.message === 'Value passed is not a string.';
|
},
|
||||||
});
|
function(err) {
|
||||||
|
return err.message === "Value passed is not a string.";
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when passed a non base58 string', function () {
|
describe("when passed a non base58 string", function() {
|
||||||
it('throws an error', function () {
|
it("throws an error", function() {
|
||||||
assert.throws(function () {
|
assert.throws(
|
||||||
base58.decode('>_<');
|
function() {
|
||||||
}, function (err) {
|
base58.base58_to_int(">_<");
|
||||||
return err.message === 'Value passed is not a valid Base58 string.';
|
},
|
||||||
});
|
function(err) {
|
||||||
|
return err.message === "Value passed is not a valid Base58 string.";
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
1000
test/examples.js
1000
test/examples.js
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user