Add keccak creation functions (#3)
* isHex, isInstanceOf & isString functions * Split is/* for smaller imports * Add keccak functions * Split keccak into individual functions * Expand hex tests
This commit is contained in:
Generated
+1206
-36
File diff suppressed because it is too large
Load Diff
+2
-1
@@ -64,6 +64,7 @@
|
||||
"sinon-chai": "^2.14.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-runtime": "^6.26.0"
|
||||
"babel-runtime": "^6.26.0",
|
||||
"keccak": "^1.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -1,6 +1,8 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isHex = require('./is/hex');
|
||||
|
||||
function addHexPrefix (value: ?string): string {
|
||||
if (value && hasHexPrefix(value)) {
|
||||
return value;
|
||||
@@ -10,7 +12,7 @@ function addHexPrefix (value: ?string): string {
|
||||
}
|
||||
|
||||
function hasHexPrefix (value: ?string): boolean {
|
||||
return !!(value && value.substr(0, 2) === '0x');
|
||||
return !!(value && isHex(value) && value.substr(0, 2) === '0x');
|
||||
}
|
||||
|
||||
function stripHexPrefix (value: ?string): string {
|
||||
|
||||
@@ -43,6 +43,12 @@ describe('hex', () => {
|
||||
hasHexPrefix(null)
|
||||
).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false when non-string value supplied', () => {
|
||||
expect(
|
||||
hasHexPrefix(false)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('stripHexPrefix', () => {
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
function isFunction (value: any): boolean {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
|
||||
function isNumber (value: any): boolean {
|
||||
return typeof value === 'number';
|
||||
}
|
||||
|
||||
function isUndefined (value: any): boolean {
|
||||
return typeof value === 'undefined';
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isFunction,
|
||||
isNumber,
|
||||
isUndefined
|
||||
};
|
||||
@@ -1,49 +0,0 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { isFunction, isNumber, isUndefined } = require('./is');
|
||||
|
||||
describe('is', () => {
|
||||
describe('isFunction', () => {
|
||||
it('returns true on valid functions', () => {
|
||||
expect(
|
||||
isFunction(isFunction)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false of invalid functions', () => {
|
||||
expect(
|
||||
isFunction('notAFunction')
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isNumber', () => {
|
||||
it('returns true on valid numbers', () => {
|
||||
expect(
|
||||
isNumber(2)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns fals on invalid numbers', () => {
|
||||
expect(
|
||||
isNumber('2')
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isUndefined', () => {
|
||||
it('returns true on undefined values', () => {
|
||||
expect(
|
||||
isUndefined()
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on defined values', () => {
|
||||
expect(
|
||||
isUndefined(null)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
module.exports = function isFunction (value: any): boolean {
|
||||
return typeof value === 'function';
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isString = require('./string');
|
||||
|
||||
module.exports = function isHex (value: any): boolean {
|
||||
return isString(value) && /^0x[a-fA-F0-9]+$/.test(value);
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isFunction = require('./function');
|
||||
const isHex = require('./hex');
|
||||
const isInstanceOf = require('./instanceOf');
|
||||
const isNumber = require('./number');
|
||||
const isString = require('./string');
|
||||
const isUndefined = require('./undefined');
|
||||
|
||||
module.exports = {
|
||||
isFunction,
|
||||
isHex,
|
||||
isInstanceOf,
|
||||
isNumber,
|
||||
isString,
|
||||
isUndefined
|
||||
};
|
||||
@@ -0,0 +1,129 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { isFunction, isHex, isInstanceOf, isNumber, isString, isUndefined } = require('./index');
|
||||
|
||||
describe('is', () => {
|
||||
describe('isFunction', () => {
|
||||
it('returns true on valid functions', () => {
|
||||
expect(
|
||||
isFunction(isFunction)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on invalid functions', () => {
|
||||
expect(
|
||||
isFunction('notAFunction')
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isHex', () => {
|
||||
const test = '123abcd45';
|
||||
|
||||
it('returns true on hex values', () => {
|
||||
expect(
|
||||
isHex(`0x${test}`)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns true on uppercase values', () => {
|
||||
expect(
|
||||
isHex(`0x${test.toUpperCase()}`)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('return false on hex values unprefixed', () => {
|
||||
expect(
|
||||
isHex(test)
|
||||
).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false on non-string values', () => {
|
||||
expect(
|
||||
isHex(false)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isInstanceOf', () => {
|
||||
it('returns true on real instances', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), Array)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on non-allocated instances', () => {
|
||||
expect(
|
||||
isInstanceOf([], Array)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on non-instances', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false when class not specified', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).to.be.false;
|
||||
});
|
||||
|
||||
it('returns false on wrong class type', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), String)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isNumber', () => {
|
||||
it('returns true on valid numbers', () => {
|
||||
expect(
|
||||
isNumber(2)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isNumber('2')
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isString', () => {
|
||||
it('returns true on valid strings', () => {
|
||||
expect(
|
||||
isString('123')
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns true on empty strings', () => {
|
||||
expect(
|
||||
isString('')
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isString(2)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isUndefined', () => {
|
||||
it('returns true on undefined values', () => {
|
||||
expect(
|
||||
isUndefined()
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on defined values', () => {
|
||||
expect(
|
||||
isUndefined(null)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
module.exports = function isInstanceOf (value: any, clazz: any): boolean {
|
||||
return value instanceof clazz;
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
module.exports = function isNumber (value: any): boolean {
|
||||
return typeof value === 'number';
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
module.exports = function isString (value: any): boolean {
|
||||
return typeof value === 'string';
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
module.exports = function isUndefined (value: any): boolean {
|
||||
return typeof value === 'undefined';
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const createKeccak = require('keccak');
|
||||
|
||||
module.exports = function keccakAsBuffer (value: Buffer | string): Buffer {
|
||||
return createKeccak('keccak256').update(value).digest();
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const { addHexPrefix } = require('../hex');
|
||||
const keccakAsString = require('./asString');
|
||||
|
||||
module.exports = function keccakAsHex (value: Buffer | string): string {
|
||||
return addHexPrefix(
|
||||
keccakAsString(value)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const keccakAsBuffer = require('./asBuffer');
|
||||
|
||||
module.exports = function keccakAsString (value: Buffer | string): string {
|
||||
return keccakAsBuffer(value).toString('hex');
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const keccakAsBuffer = require('./asBuffer');
|
||||
const keccakAsHex = require('./asHex');
|
||||
const keccakAsString = require('./asString');
|
||||
|
||||
module.exports = {
|
||||
keccakAsBuffer,
|
||||
keccakAsHex,
|
||||
keccakAsString
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { keccakAsBuffer, keccakAsHex, keccakAsString } = require('./index');
|
||||
|
||||
describe('keccak', () => {
|
||||
const value = 'test value';
|
||||
const result = '2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e';
|
||||
|
||||
describe('keccakAsBuffer', () => {
|
||||
it('returns an hex representation', () => {
|
||||
expect(
|
||||
keccakAsBuffer(value).equals(
|
||||
Buffer.from(result, 'hex')
|
||||
)
|
||||
).to.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('keccakAsHex', () => {
|
||||
it('returns a prefixed hex representation', () => {
|
||||
expect(
|
||||
keccakAsHex(value)
|
||||
).to.equal(`0x${result}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('keccakAsString', () => {
|
||||
it('returns an un-prefixed hex representation', () => {
|
||||
expect(
|
||||
keccakAsString(value)
|
||||
).to.equal(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user