UNPKG

1.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.u8aToU8a = void 0;
4const toU8a_js_1 = require("../hex/toU8a.js");
5const buffer_js_1 = require("../is/buffer.js");
6const hex_js_1 = require("../is/hex.js");
7const u8a_js_1 = require("../is/u8a.js");
8const toU8a_js_2 = require("../string/toU8a.js");
9/**
10 * @name u8aToU8a
11 * @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
12 * @description
13 * `null` or `undefined` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
14 * @example
15 * <BR>
16 *
17 * ```javascript
18 * import { u8aToU8a } from '@polkadot/util';
19 *
20 * u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
21 * u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
22 * ```
23 */
24function u8aToU8a(value) {
25 return (0, u8a_js_1.isU8a)(value)
26 // NOTE isBuffer needs to go here since it actually extends
27 // Uint8Array on Node.js environments, so all Buffer are Uint8Array,
28 // but Uint8Array is not Buffer
29 ? (0, buffer_js_1.isBuffer)(value)
30 ? new Uint8Array(value)
31 : value
32 : (0, hex_js_1.isHex)(value)
33 ? (0, toU8a_js_1.hexToU8a)(value)
34 : Array.isArray(value)
35 ? new Uint8Array(value)
36 : (0, toU8a_js_2.stringToU8a)(value);
37}
38exports.u8aToU8a = u8aToU8a;