Correct numberToHex(0) conversion (#1661)
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Ensure `{bn, n}ToHex` respects `bitLength` with 0 values
|
||||
- Ensure `{bn, n, number}ToHex(0)` always respects `bitLength`
|
||||
- Upgrade dependencies to latest stable versions
|
||||
|
||||
|
||||
|
||||
@@ -4,22 +4,22 @@
|
||||
import { numberToHex } from '.';
|
||||
|
||||
describe('numberToHex', (): void => {
|
||||
it('converts undefined to 0x', (): void => {
|
||||
it('converts undefined to 0x00', (): void => {
|
||||
expect(
|
||||
numberToHex()
|
||||
).toEqual('0x');
|
||||
).toEqual('0x00');
|
||||
});
|
||||
|
||||
it('converts null to 0x', (): void => {
|
||||
it('converts null to 0x00000000 (with bitLength)', (): void => {
|
||||
expect(
|
||||
numberToHex(null)
|
||||
).toEqual('0x');
|
||||
numberToHex(null, 32)
|
||||
).toEqual('0x00000000');
|
||||
});
|
||||
|
||||
it('converts Nan to 0x', (): void => {
|
||||
expect(
|
||||
numberToHex(NaN)
|
||||
).toEqual('0x');
|
||||
).toEqual('0x00');
|
||||
});
|
||||
|
||||
it('converts 0 to 0x00', (): void => {
|
||||
|
||||
@@ -21,11 +21,7 @@ import { hexFixLength } from '../hex/fixLength';
|
||||
* ```
|
||||
*/
|
||||
export function numberToHex (value?: number | null, bitLength = -1): HexString {
|
||||
if (value === undefined || value === null || isNaN(value)) {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
const hex = value.toString(16);
|
||||
const hex = (!value || Number.isNaN(value) ? 0 : value).toString(16);
|
||||
|
||||
return hexFixLength(hex.length % 2 ? `0${hex}` : hex, bitLength, true);
|
||||
}
|
||||
|
||||
@@ -7,19 +7,19 @@ describe('numberToU8a', (): void => {
|
||||
it('converts undefined to empty', (): void => {
|
||||
expect(
|
||||
numberToU8a()
|
||||
).toEqual(new Uint8Array());
|
||||
).toEqual(new Uint8Array(1));
|
||||
});
|
||||
|
||||
it('converts null to empty', (): void => {
|
||||
expect(
|
||||
numberToU8a(null)
|
||||
).toEqual(new Uint8Array());
|
||||
).toEqual(new Uint8Array(1));
|
||||
});
|
||||
|
||||
it('converts NaN to empty', (): void => {
|
||||
expect(
|
||||
numberToU8a(NaN)
|
||||
).toEqual(new Uint8Array());
|
||||
).toEqual(new Uint8Array(1));
|
||||
});
|
||||
|
||||
it('converts 0 to u8a', (): void => {
|
||||
|
||||
@@ -19,9 +19,7 @@ import { numberToHex } from './toHex';
|
||||
* ```
|
||||
*/
|
||||
export function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
|
||||
return value === undefined || value === null || isNaN(value)
|
||||
? new Uint8Array()
|
||||
: hexToU8a(
|
||||
numberToHex(value, bitLength)
|
||||
);
|
||||
return hexToU8a(
|
||||
numberToHex(value, bitLength)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user