Merge pull request #5 from jimeh/lint-using-prettier

Lint using prettier
This commit is contained in:
2017-08-20 19:28:40 +01:00
committed by GitHub
7 changed files with 618 additions and 597 deletions

View File

@@ -1,7 +1,11 @@
module.exports = {
extends: 'semistandard',
env: {
browser: true,
node: true
},
extends: ["prettier"],
plugins: ["prettier"],
rules: {
"prettier/prettier": "error"
}
};

View File

@@ -1,15 +1,6 @@
NPM_EXECUTABLE_HOME := node_modules/.bin
PATH := ${NPM_EXECUTABLE_HOME}:${PATH}
hooks: .git/hooks/pre-commit
hooks: .git/hooks/pre-push
.git/hooks/pre-commit: hook.sh
cp $< $@
.git/hooks/pre-push: hook.sh
cp $< $@
publish: npm-dep
npm publish

View File

@@ -1,8 +0,0 @@
#!/usr/bin/env bash
set -ex
npm run lint
npm run test
node package.json

View File

@@ -26,8 +26,10 @@
},
"devDependencies": {
"eslint": "^4.1.1",
"eslint-config-semistandard": "^11.0.0",
"mocha": "^3.4.2"
"eslint-config-prettier": "^2.3.0",
"eslint-plugin-prettier": "^2.2.0",
"mocha": "^3.4.2",
"prettier": "^1.5.3"
},
"scripts": {
"lint": "eslint . --fix",

View File

@@ -1,32 +1,38 @@
var alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
var alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
var base = alphabet.length;
// 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;
return lookup;
}, {});
function assertNonNegativeSafeInteger (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.');
function assertNonNegativeSafeInteger(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.");
}
}
function assertString (str) {
if (typeof str !== 'string') {
throw new Error('Value passed is not a string.');
function assertString(str) {
if (typeof str !== "string") {
throw new Error("Value passed is not a string.");
}
}
function assertBase58Character (character) {
function assertBase58Character(character) {
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) {
var str = '';
exports.encode = function(num) {
var str = "";
var modulus;
num = Number(num);
@@ -42,10 +48,10 @@ exports.encode = function (num) {
return alphabet[num] + str;
};
exports.decode = function (str) {
exports.decode = function(str) {
assertString(str);
return str.split('').reverse().reduce(function (num, character, index) {
return str.split("").reverse().reduce(function(num, character, index) {
assertBase58Character(character);
return num + alphabetLookup[character] * Math.pow(base, index);
}, 0);

View File

@@ -1,112 +1,138 @@
var assert = require('assert');
var examples = require('./examples');
var base58 = require('..');
var assert = require("assert");
var examples = require("./examples");
var base58 = require("..");
function exampleRunner (callback) {
Object.keys(examples).forEach(function (str) {
function exampleRunner(callback) {
Object.keys(examples).forEach(function(str) {
callback(str, examples[str]);
});
}
describe('Base58', function () {
before(function () {
describe("Base58", function() {
before(function() {
var valid = true;
var count = 0;
exampleRunner(function (str, num) {
exampleRunner(function(str, num) {
count++;
if (typeof str !== 'string') {
if (typeof str !== "string") {
valid = false;
}
if (typeof num !== 'number') {
if (typeof num !== "number") {
valid = false;
}
});
assert.strictEqual(count > 0, true, 'Expected there to be examples');
assert.strictEqual(valid, true, 'Expected the examples to be valid');
assert.strictEqual(count > 0, true, "Expected there to be examples");
assert.strictEqual(valid, true, "Expected the examples to be valid");
});
describe('.encode', function () {
it('encodes number to Base58 string', function () {
exampleRunner(function (str, num) {
describe(".encode", function() {
it("encodes number to Base58 string", function() {
exampleRunner(function(str, num) {
assert.strictEqual(base58.encode(num), str);
});
});
describe('when passed a string only containing numbers', function () {
it('encodes string after first converting it to an integer', function () {
exampleRunner(function (str, num) {
describe("when passed a string only containing numbers", function() {
it("encodes string after first converting it to an integer", function() {
exampleRunner(function(str, num) {
assert.strictEqual(base58.encode(num.toString()), str);
});
});
});
describe('when passed a non number', function () {
it('throws an error', function () {
assert.throws(function () {
base58.encode('hi');
}, function (err) {
return err.message === 'Value passed is not a non-negative safe integer.';
});
describe("when passed a non number", function() {
it("throws an error", function() {
assert.throws(
function() {
base58.encode("hi");
},
function(err) {
return (
err.message === "Value passed is not a non-negative safe integer."
);
}
);
});
});
describe('when passed a float', function () {
it('throws an error', function () {
assert.throws(function () {
base58.encode(3.14);
}, function (err) {
return err.message === 'Value passed is not a non-negative safe integer.';
});
describe("when passed a float", function() {
it("throws an error", function() {
assert.throws(
function() {
base58.encode(3.14);
},
function(err) {
return (
err.message === "Value passed is not a non-negative safe integer."
);
}
);
});
});
describe('when passed a negative number', function () {
it('throws an error', function () {
assert.throws(function () {
base58.encode(-300);
}, function (err) {
return err.message === 'Value passed is not a non-negative safe integer.';
});
describe("when passed a negative number", function() {
it("throws an error", function() {
assert.throws(
function() {
base58.encode(-300);
},
function(err) {
return (
err.message === "Value passed is not a non-negative safe integer."
);
}
);
});
});
describe('when passed a non-safe integer', function () {
it('throws an error', function () {
assert.throws(function () {
base58.encode(1E100);
}, function (err) {
return err.message === 'Value passed is not a non-negative safe integer.';
});
describe("when passed a non-safe integer", function() {
it("throws an error", function() {
assert.throws(
function() {
base58.encode(1e100);
},
function(err) {
return (
err.message === "Value passed is not a non-negative safe integer."
);
}
);
});
});
});
describe('.decode', function () {
it('decodes base58 string to number', function () {
exampleRunner(function (str, num) {
describe(".decode", function() {
it("decodes base58 string to number", function() {
exampleRunner(function(str, num) {
assert.strictEqual(base58.decode(str), num);
});
});
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 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.";
}
);
});
});
});

File diff suppressed because it is too large Load Diff