Files
common/packages/util/src/u8a/concat.ts
T
Jaco 3a98323e8b Minor readability improvements (#952)
* Minor readability improvements

* trigger

* Additional

* Additional
2021-04-21 10:31:28 +02:00

42 lines
973 B
TypeScript

// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { u8aToU8a } from './toU8a';
/**
* @name u8aConcat
* @summary Creates a concatenated Uint8Array from the inputs.
* @description
* Concatenates the input arrays into a single `UInt8Array`.
* @example
* <BR>
*
* ```javascript
* import { { u8aConcat } from '@polkadot/util';
*
* u8aConcat(
* new Uint8Array([1, 2, 3]),
* new Uint8Array([4, 5, 6])
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export function u8aConcat (...list: (Buffer | Uint8Array | number[] | string)[]): Uint8Array {
let length = 0;
let offset = 0;
const u8as = new Array<Uint8Array>(list.length);
for (let i = 0; i < list.length; i++) {
u8as[i] = u8aToU8a(list[i]);
length += u8as[i].length;
}
const result = new Uint8Array(length);
for (let i = 0; i < u8as.length; i++) {
result.set(u8as[i], offset);
offset += u8as[i].length;
}
return result;
}