std/bytes/concat.ts
Asher Gomez a079dd3fed
docs(bytes,collections): fix doc checker and documentation (#4691)
docs(bytes,collection): fix doc checker and documentation
2024-05-08 16:18:26 +10:00

35 lines
940 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* Concatenate an array of byte slices into a single slice.
*
* @param buffers Array of byte slices to concatenate.
* @returns A new byte slice containing all the input slices concatenated.
*
* @example Basic usage
* ```ts
* import { concat } from "@std/bytes/concat";
* import { assertEquals } from "@std/assert/assert-equals"
*
* const a = new Uint8Array([0, 1, 2]);
* const b = new Uint8Array([3, 4, 5]);
*
* assertEquals(concat([a, b]), new Uint8Array([0, 1, 2, 3, 4, 5]));
* ```
*/
export function concat(buffers: Uint8Array[]): Uint8Array {
let length = 0;
for (const buffer of buffers) {
length += buffer.length;
}
const output = new Uint8Array(length);
let index = 0;
for (const buffer of buffers) {
output.set(buffer, index);
index += buffer.length;
}
return output;
}