Add BN conversion utilities (#4)
* Add BN conversion utilities * Add isBN function * Make Regex constant
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { stripHexPrefix } = require('../hex');
|
||||
const isHex = require('../is/hex');
|
||||
|
||||
const ZERO_BN = new BN(0);
|
||||
|
||||
module.exports = function fromHex (value?: string): BN {
|
||||
if (!value) {
|
||||
return ZERO_BN;
|
||||
}
|
||||
|
||||
if (!isHex(value)) {
|
||||
throw new Error(`Cannot convert from non-hex value '${value}' to BN`);
|
||||
}
|
||||
|
||||
return new BN(stripHexPrefix(value), 16);
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const bnFromHex = require('./fromHex');
|
||||
const bnToHex = require('./toHex');
|
||||
|
||||
module.exports = {
|
||||
bnFromHex,
|
||||
bnToHex
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { bnFromHex, bnToHex } = require('./index');
|
||||
|
||||
describe('bn', () => {
|
||||
describe('bnFromHex', () => {
|
||||
const value = '80';
|
||||
|
||||
it('converts prefixed hex values to BN', () => {
|
||||
expect(
|
||||
bnFromHex(`0x${value}`).eq(
|
||||
new BN(value, 16)
|
||||
)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('converts null values to BN(0)', () => {
|
||||
expect(
|
||||
bnFromHex(null).eq(
|
||||
new BN(0)
|
||||
)
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('throws when trying to convert non-BN values', () => {
|
||||
expect(
|
||||
() => bnFromHex('noAHex')
|
||||
).to.throw(/Cannot convert from non-hex/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bnToHex', () => {
|
||||
it('converts BN values to a prefixed hex representation', () => {
|
||||
expect(
|
||||
bnToHex(new BN(128))
|
||||
).to.equal('0x80');
|
||||
});
|
||||
|
||||
it('converts null values to 0x', () => {
|
||||
expect(
|
||||
bnToHex(null)
|
||||
).to.equal('0x');
|
||||
});
|
||||
|
||||
it('throws when trying to convert non-BN values', () => {
|
||||
expect(
|
||||
() => bnToHex('notABn')
|
||||
).to.throw(/Cannot convert from non-BN/);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { addHexPrefix } = require('../hex');
|
||||
const isBN = require('../is/bn');
|
||||
|
||||
const ZERO_STR = '0x';
|
||||
|
||||
module.exports = function toHex (value?: BN): string {
|
||||
if (!value) {
|
||||
return ZERO_STR;
|
||||
}
|
||||
|
||||
if (!isBN(value)) {
|
||||
throw new Error(`Cannot convert from non-BN value '${value}' to hex`);
|
||||
}
|
||||
|
||||
return addHexPrefix(value.toString(16));
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
module.exports = function isBN (value: any): boolean {
|
||||
return BN.isBN(value);
|
||||
};
|
||||
+3
-1
@@ -3,6 +3,8 @@
|
||||
|
||||
const isString = require('./string');
|
||||
|
||||
const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
|
||||
|
||||
module.exports = function isHex (value: any): boolean {
|
||||
return isString(value) && /^0x[a-fA-F0-9]+$/.test(value);
|
||||
return isString(value) && HEX_REGEX.test(value);
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const isBN = require('./bn');
|
||||
const isFunction = require('./function');
|
||||
const isHex = require('./hex');
|
||||
const isInstanceOf = require('./instanceOf');
|
||||
@@ -9,6 +10,7 @@ const isString = require('./string');
|
||||
const isUndefined = require('./undefined');
|
||||
|
||||
module.exports = {
|
||||
isBN,
|
||||
isFunction,
|
||||
isHex,
|
||||
isInstanceOf,
|
||||
|
||||
+17
-1
@@ -2,9 +2,25 @@
|
||||
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
const { isFunction, isHex, isInstanceOf, isNumber, isString, isUndefined } = require('./index');
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { isBN, isFunction, isHex, isInstanceOf, isNumber, isString, isUndefined } = require('./index');
|
||||
|
||||
describe('is', () => {
|
||||
describe('isBN', () => {
|
||||
it('returns true when a BN value', () => {
|
||||
expect(
|
||||
isBN(new BN(123))
|
||||
).to.be.true;
|
||||
});
|
||||
|
||||
it('returns false on non-BN values', () => {
|
||||
expect(
|
||||
isBN(0)
|
||||
).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isFunction', () => {
|
||||
it('returns true on valid functions', () => {
|
||||
expect(
|
||||
|
||||
Reference in New Issue
Block a user