Skip to main content

@std/bytes@1.0.4
Built and signed on GitHub Actions

Utilities to manipulate Uint8Arrays that are not built-in to JavaScript

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 weeks ago (1.0.4)
function copy
copy(
offset?: number,
): number

Copy bytes from the source array to the destination array and returns the number of bytes copied.

If the source array is larger than what the dst array can hold, only the amount of bytes that fit in the dst array are copied.

Examples

Basic usage

import { copy } from "@std/bytes/copy";
import { assertEquals } from "@std/assert";

const src = new Uint8Array([9, 8, 7]);
const dst = new Uint8Array([0, 1, 2, 3, 4, 5]);

assertEquals(copy(src, dst), 3);
assertEquals(dst, new Uint8Array([9, 8, 7, 3, 4, 5]));

Copy with offset

import { copy } from "@std/bytes/copy";
import { assertEquals } from "@std/assert";

const src = new Uint8Array([1, 1, 1, 1]);
const dst = new Uint8Array([0, 0, 0, 0]);

assertEquals(copy(src, dst, 1), 3);
assertEquals(dst, new Uint8Array([0, 1, 1, 1]));

Defining an offset will start copying at the specified index in the destination array.

Parameters

Source array to copy from.

Destination array to copy to.

optional
offset: number = 0

Offset in the destination array to start copying to. Defaults to 0.

Return Type

Number of bytes copied.

Add Package

deno add jsr:@std/bytes

Import symbol

import { copy } from "@std/bytes/copy";

---- OR ----

Import directly with a jsr specifier

import { copy } from "jsr:@std/bytes/copy";

Add Package

npx jsr add @std/bytes

Import symbol

import { copy } from "@std/bytes/copy";

Add Package

yarn dlx jsr add @std/bytes

Import symbol

import { copy } from "@std/bytes/copy";

Add Package

pnpm dlx jsr add @std/bytes

Import symbol

import { copy } from "@std/bytes/copy";

Add Package

bunx jsr add @std/bytes

Import symbol

import { copy } from "@std/bytes/copy";