This release is 28 versions behind 1.0.6 — the latest version of @std/bytes. Jump to latest
Works with
•JSR Score100%•This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




Downloads51,003/wk
•Published2 years ago (0.208.0)
Utilities to manipulate Uint8Arrays that are not built-in to JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475// Copyright 2018-2023 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; /** * @deprecated (will be removed in 0.209.0) Pass in an array instead of a * spread of arguments. * * 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; export function concat(...buf: (Uint8Array | Uint8Array[])[]): Uint8Array { /** * @todo(iuioiua): Revert to the old implementation upon removal of the * spread signatures. * * @see {@link https://github.com/denoland/deno_std/blob/e6c61ba64d547b60076422bbc1f6ad33184cc10a/bytes/concat.ts} */ // No need to concatenate if there is only one element in array or sub-array if (buf.length === 1) { if (!Array.isArray(buf[0])) { return buf[0]; } else if (buf[0].length === 1) { return buf[0][0]; } } let length = 0; for (const b of buf) { if (Array.isArray(b)) { for (const b1 of b) { length += b1.length; } } else { length += b.length; } } const output = new Uint8Array(length); let index = 0; for (const b of buf) { if (Array.isArray(b)) { for (const b1 of b) { output.set(b1, index); index += b1.length; } } else { output.set(b, index); index += b.length; } } return output; }