UNPKG

1.17 kBJavaScriptView Raw
1import { u8aToU8a } from './toU8a.js';
2/**
3 * @name u8aConcat
4 * @summary Creates a concatenated Uint8Array from the inputs.
5 * @description
6 * Concatenates the input arrays into a single `UInt8Array`.
7 * @example
8 * <BR>
9 *
10 * ```javascript
11 * import { { u8aConcat } from '@polkadot/util';
12 *
13 * u8aConcat(
14 * new Uint8Array([1, 2, 3]),
15 * new Uint8Array([4, 5, 6])
16 * ); // [1, 2, 3, 4, 5, 6]
17 * ```
18 */
19export function u8aConcat(...list) {
20 const u8as = new Array(list.length);
21 let length = 0;
22 for (let i = 0; i < list.length; i++) {
23 u8as[i] = u8aToU8a(list[i]);
24 length += u8as[i].length;
25 }
26 return u8aConcatStrict(u8as, length);
27}
28/**
29 * @name u8aConcatStrict
30 * @description A strict version of [[u8aConcat]], accepting only Uint8Array inputs
31 */
32export function u8aConcatStrict(u8as, length = 0) {
33 let offset = 0;
34 if (!length) {
35 for (let i = 0; i < u8as.length; i++) {
36 length += u8as[i].length;
37 }
38 }
39 const result = new Uint8Array(length);
40 for (let i = 0; i < u8as.length; i++) {
41 result.set(u8as[i], offset);
42 offset += u8as[i].length;
43 }
44 return result;
45}
\No newline at end of file