Correct numberToHex(0) conversion (#1661)

This commit is contained in:
Jaco
2022-10-07 07:00:46 +03:00
committed by GitHub
parent b17bfbaf34
commit 9523e6b0d3
5 changed files with 14 additions and 20 deletions
+6 -6
View File
@@ -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 => {
+1 -5
View File
@@ -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);
}
+3 -3
View File
@@ -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 => {
+3 -5
View File
@@ -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)
);
}