Cleanup hex functions (#130)
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const assert = require('../assert');
|
||||
const isHex = require('../is/hex');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
/**
|
||||
@@ -23,6 +25,8 @@ module.exports = function hexToU8a (_value?: string, bitLength: number = -1): Ui
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
assert(isHex(_value), `Expected hex value to convert, found '${_value}'`);
|
||||
|
||||
const value = hexStripPrefix(_value);
|
||||
const valLength = value.length / 2;
|
||||
const bufLength = Math.ceil(
|
||||
|
||||
@@ -19,19 +19,17 @@ describe('hexToU8a', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (no prefix)', () => {
|
||||
expect(
|
||||
hexToU8a('80000a')
|
||||
).toEqual(
|
||||
new Uint8Array([128, 0, 10])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array with the correct values (bitLengfth)', () => {
|
||||
expect(
|
||||
hexToU8a('80000a', 32)
|
||||
hexToU8a('0x80000a', 32)
|
||||
).toEqual(
|
||||
new Uint8Array([0, 128, 0, 10])
|
||||
);
|
||||
});
|
||||
|
||||
it('fails when non-hex value provided', () => {
|
||||
expect(
|
||||
() => hexToU8a('notahex')
|
||||
).toThrow(/hex value to convert/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user