UNPKG

1.16 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.u8aToU8a = u8aToU8a;
7
8var _toU8a = require("../hex/toU8a");
9
10var _buffer = require("../is/buffer");
11
12var _hex = require("../is/hex");
13
14var _u8a = require("../is/u8a");
15
16var _toU8a2 = require("../string/toU8a");
17
18// Copyright 2017-2022 @polkadot/util authors & contributors
19// SPDX-License-Identifier: Apache-2.0
20
21/**
22 * @name u8aToU8a
23 * @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
24 * @description
25 * `null` or `undefined` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
26 * @example
27 * <BR>
28 *
29 * ```javascript
30 * import { u8aToU8a } from '@polkadot/util';
31 *
32 * u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
33 * u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
34 * ```
35 */
36function u8aToU8a(value) {
37 return value ? Array.isArray(value) || (0, _buffer.isBuffer)(value) ? new Uint8Array(value) : (0, _u8a.isU8a)(value) ? value : (0, _hex.isHex)(value) ? (0, _toU8a.hexToU8a)(value) : (0, _toU8a2.stringToU8a)(value) : new Uint8Array();
38}
\No newline at end of file