mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
30 lines
675 B
TypeScript
30 lines
675 B
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* Concatenate an array of {@linkcode Uint8Array}s.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* import { concat } from "@std/bytes/concat";
|
|
*
|
|
* const a = new Uint8Array([0, 1, 2]);
|
|
* const b = new Uint8Array([3, 4, 5]);
|
|
* concat([a, b]); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ]
|
|
* ```
|
|
*/
|
|
export function concat(buf: Uint8Array[]): Uint8Array {
|
|
let length = 0;
|
|
for (const b of buf) {
|
|
length += b.length;
|
|
}
|
|
const output = new Uint8Array(length);
|
|
let index = 0;
|
|
for (const b of buf) {
|
|
output.set(b, index);
|
|
index += b.length;
|
|
}
|
|
|
|
return output;
|
|
}
|