Cleanup hex functions (#130)

This commit is contained in:
Jaco Greeff
2018-05-25 10:27:53 +02:00
committed by GitHub
parent 15caba9eee
commit db1a6adfc5
8 changed files with 16 additions and 107 deletions
+4
View File
@@ -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(
+7 -9
View File
@@ -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/);
});
});