UNPKG

1.02 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { hexToU8a } from "../hex/toU8a.js";
4import { isBuffer } from "../is/buffer.js";
5import { isHex } from "../is/hex.js";
6import { isU8a } from "../is/u8a.js";
7import { 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
24export 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