UNPKG

1.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.u8aConcatStrict = exports.u8aConcat = void 0;
4const toU8a_js_1 = require("./toU8a.js");
5/**
6 * @name u8aConcat
7 * @summary Creates a concatenated Uint8Array from the inputs.
8 * @description
9 * Concatenates the input arrays into a single `UInt8Array`.
10 * @example
11 * <BR>
12 *
13 * ```javascript
14 * import { { u8aConcat } from '@polkadot/util';
15 *
16 * u8aConcat(
17 * new Uint8Array([1, 2, 3]),
18 * new Uint8Array([4, 5, 6])
19 * ); // [1, 2, 3, 4, 5, 6]
20 * ```
21 */
22function u8aConcat(...list) {
23 const count = list.length;
24 const u8as = new Array(count);
25 let length = 0;
26 for (let i = 0; i < count; i++) {
27 u8as[i] = (0, toU8a_js_1.u8aToU8a)(list[i]);
28 length += u8as[i].length;
29 }
30 return u8aConcatStrict(u8as, length);
31}
32exports.u8aConcat = u8aConcat;
33/**
34 * @name u8aConcatStrict
35 * @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
36 */
37function u8aConcatStrict(u8as, length = 0) {
38 const count = u8as.length;
39 let offset = 0;
40 if (!length) {
41 for (let i = 0; i < count; i++) {
42 length += u8as[i].length;
43 }
44 }
45 const result = new Uint8Array(length);
46 for (let i = 0; i < count; i++) {
47 result.set(u8as[i], offset);
48 offset += u8as[i].length;
49 }
50 return result;
51}
52exports.u8aConcatStrict = u8aConcatStrict;