UNPKG

904 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { u8aToU8a } from "./toU8a.js";
4/**
5 * @name u8aConcat
6 * @summary Creates a concatenated Uint8Array from the inputs.
7 * @description
8 * Concatenates the input arrays into a single `UInt8Array`.
9 * @example
10 * <BR>
11 *
12 * ```javascript
13 * import { { u8aConcat } from '@polkadot/util';
14 *
15 * u8aConcat(
16 * new Uint8Array([1, 2, 3]),
17 * new Uint8Array([4, 5, 6])
18 * ); // [1, 2, 3, 4, 5, 6]
19 * ```
20 */
21
22export function u8aConcat(...list) {
23 let length = 0;
24 let offset = 0;
25 const u8as = new Array(list.length);
26
27 for (let i = 0; i < list.length; i++) {
28 u8as[i] = u8aToU8a(list[i]);
29 length += u8as[i].length;
30 }
31
32 const result = new Uint8Array(length);
33
34 for (let i = 0; i < u8as.length; i++) {
35 result.set(u8as[i], offset);
36 offset += u8as[i].length;
37 }
38
39 return result;
40}
\No newline at end of file