1 | // Copyright 2017-2022 @polkadot/util authors & contributors
|
2 | // SPDX-License-Identifier: Apache-2.0
|
3 | import { hexToU8a } from "../hex/toU8a.js";
|
4 | import { isNull } from "../is/null.js";
|
5 | import { isUndefined } from "../is/undefined.js";
|
6 | import { numberToHex } from "./toHex.js";
|
7 | /**
|
8 | * @name numberToU8a
|
9 | * @summary Creates a Uint8Array object from a number.
|
10 | * @description
|
11 | * `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`. With `bitLength`, it converts the value to the equivalent size.
|
12 | * @example
|
13 | * <BR>
|
14 | *
|
15 | * ```javascript
|
16 | * import { numberToU8a } from '@polkadot/util';
|
17 | *
|
18 | * numberToU8a(0x1234); // => [0x12, 0x34]
|
19 | * ```
|
20 | */
|
21 |
|
22 | export function numberToU8a(value, bitLength = -1) {
|
23 | return isUndefined(value) || isNull(value) || isNaN(value) ? new Uint8Array() : hexToU8a(numberToHex(value, bitLength));
|
24 | } |
\ | No newline at end of file |