1 | // Copyright 2017-2022 @polkadot/util authors & contributors
|
2 | // SPDX-License-Identifier: Apache-2.0
|
3 | import { hexToU8a } from "../hex/toU8a.js";
|
4 | import { isBuffer } from "../is/buffer.js";
|
5 | import { isHex } from "../is/hex.js";
|
6 | import { isU8a } from "../is/u8a.js";
|
7 | import { stringToU8a } from "../string/toU8a.js";
|
8 | /**
|
9 | * @name u8aToU8a
|
10 | * @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
|
11 | * @description
|
12 | * `null` or `undefined` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
|
13 | * @example
|
14 | * <BR>
|
15 | *
|
16 | * ```javascript
|
17 | * import { u8aToU8a } from '@polkadot/util';
|
18 | *
|
19 | * u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
|
20 | * u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
|
21 | * ```
|
22 | */
|
23 |
|
24 | export function u8aToU8a(value) {
|
25 | return value ? Array.isArray(value) || isBuffer(value) ? new Uint8Array(value) : isU8a(value) ? value : isHex(value) ? hexToU8a(value) : stringToU8a(value) : new Uint8Array();
|
26 | } |
\ | No newline at end of file |