Skip to main content
Module

std/bytes/mod.ts

The Deno Standard Library
Latest
import * as mod from "https://deno.land/std@0.224.0/bytes/mod.ts";

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 "https://deno.land/std@0.224.0/bytes/concat.ts";

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 "https://deno.land/std@0.224.0/bytes/copy.ts";

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 "https://deno.land/std@0.224.0/bytes/ends_with.ts";

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 "https://deno.land/std@0.224.0/bytes/equals.ts";

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 "https://deno.land/std@0.224.0/bytes/includes_needle.ts";

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 "https://deno.land/std@0.224.0/bytes/index_of_needle.ts";

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 "https://deno.land/std@0.224.0/bytes/last_index_of_needle.ts";

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 "https://deno.land/std@0.224.0/bytes/repeat.ts";

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 "https://deno.land/std@0.224.0/bytes/starts_with.ts";

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

startsWith(source, prefix); // true

Functions

Concatenate an array of byte slices into a single slice.

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

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

Check whether byte slices are equal to each other.

Determines whether the source array contains the needle array.

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

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

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

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