Handle 0 on numberToU8a (#626)

* Handle 0 on numberToU8a

* Remove old test (replaced)

* base58 CHANGELOG
This commit is contained in:
Jaco Greeff
2020-07-02 07:32:25 +02:00
committed by GitHub
parent 5421e06fc9
commit 24b533689c
3 changed files with 20 additions and 7 deletions
+12 -6
View File
@@ -5,12 +5,6 @@
import { numberToU8a } from '.';
describe('numberToU8a', (): void => {
it('converts 0 to empty', (): void => {
expect(
numberToU8a(0)
).toEqual(new Uint8Array());
});
it('converts undefined to empty', (): void => {
expect(
numberToU8a()
@@ -29,6 +23,18 @@ describe('numberToU8a', (): void => {
).toEqual(new Uint8Array());
});
it('converts 0 to u8a', (): void => {
expect(
numberToU8a(0)
).toEqual(new Uint8Array([0]));
});
it('converts 0 to u8a (with length)', (): void => {
expect(
numberToU8a(0, 16)
).toEqual(new Uint8Array([0, 0]));
});
it('converts values to the u8a', (): void => {
expect(
numberToU8a(0x3456)
+3 -1
View File
@@ -3,6 +3,8 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import hexToU8a from '../hex/toU8a';
import isNull from '../is/null';
import isUndefined from '../is/undefined';
import numberToHex from './toHex';
/**
@@ -20,7 +22,7 @@ import numberToHex from './toHex';
* ```
*/
export default function numberToU8a (value?: number | null, bitLength = -1): Uint8Array {
if (!value || isNaN(value)) {
if (isUndefined(value) || isNull(value) || isNaN(value)) {
return new Uint8Array();
}