Skip to main content
Home
This release is 7 versions behind 1.0.6 — the latest version of @std/bytes. Jump to latest

@std/bytes@0.224.0
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 years ago (0.224.0)

Helper functions for working with Uint8Array byte slices.

Concatenate byte slices

concat concatenates an array of byte slices into a single slice.

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 ]

Copy byte slices

copy copies bytes from the src array to the dst array and returns the number of bytes copied.

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

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

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

Check if a byte slice ends with another byte slice

endsWith returns true if the suffix array appears at the end of the source array, false otherwise.

import { endsWith } from "@std/bytes/ends-with";

const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
const suffix = new Uint8Array([1, 2, 3]);

endsWith(source, suffix); // true

Check if two byte slices are equal

equals checks whether byte slices are equal to each other.

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

const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([1, 2, 3]);
const c = new Uint8Array([4, 5, 6]);

equals(a, b); // true
equals(b, c); // false

Check if a byte slice includes another byte slice

includesNeedle determines whether the source array contains the needle array.

import { includesNeedle } from "@std/bytes/includes-needle";

const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
const needle = new Uint8Array([1, 2]);

includesNeedle(source, needle); // true

Find the index of a byte slice in another byte slice

indexOfNeedle returns the index of the first occurrence of the needle array in the source array, or -1 if it is not present.

import { indexOfNeedle } from "@std/bytes/index-of-needle";

const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
const needle = new Uint8Array([1, 2]);
const notNeedle = new Uint8Array([5, 0]);

indexOfNeedle(source, needle); // 1
indexOfNeedle(source, notNeedle); // -1

Find the last index of a byte slice in another byte slice

lastIndexOfNeedle returns the index of the last occurrence of the needle array in the source array, or -1 if it is not present.

import { lastIndexOfNeedle } from "@std/bytes/last-index-of-needle";

const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
const needle = new Uint8Array([1, 2]);
const notNeedle = new Uint8Array([5, 0]);

lastIndexOfNeedle(source, needle); // 5
lastIndexOfNeedle(source, notNeedle); // -1

Repeat a byte slice

repeat returns a new byte slice composed of count repetitions of the source array.

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

const source = new Uint8Array([0, 1, 2]);

repeat(source, 3); // Uint8Array(9) [0, 1, 2, 0, 1, 2, 0, 1, 2]

repeat(source, 0); // Uint8Array(0) []

repeat(source, -1); // Throws `RangeError`

Check if a byte slice starts with another byte slice

startsWith returns true if the prefix array appears at the start of the source array, false otherwise.

import { startsWith } from "@std/bytes/starts-with";

const source = new Uint8Array([0, 1, 2, 1, 2, 1, 2, 3]);
const prefix = new Uint8Array([0, 1, 2]);

startsWith(source, prefix); // true
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/bytes

Import symbol

import * as bytes from "@std/bytes";
or

Import directly with a jsr specifier

import * as bytes from "jsr:@std/bytes";

Add Package

pnpm i jsr:@std/bytes
or (using pnpm 10.8 or older)
pnpm dlx jsr add @std/bytes

Import symbol

import * as bytes from "@std/bytes";

Add Package

yarn add jsr:@std/bytes
or (using Yarn 4.8 or older)
yarn dlx jsr add @std/bytes

Import symbol

import * as bytes from "@std/bytes";

Add Package

vlt install jsr:@std/bytes

Import symbol

import * as bytes from "@std/bytes";

Add Package

npx jsr add @std/bytes

Import symbol

import * as bytes from "@std/bytes";

Add Package

bunx jsr add @std/bytes

Import symbol

import * as bytes from "@std/bytes";