std@0.116.0/bytes

Deno standard library
GitHub Stars
2187
Go to latest
import * as mod from "https://deno.land/std@0.116.0/bytes/mod.ts";

Functions

concat

Concatenate multiple binary arrays and return new one.

contains

Determines whether the source array includes the pattern array.

copy

Copy bytes from one Uint8Array to another. Bytes from src which don't fit into dst will not be copied.

endsWith

Check whether binary array ends with suffix.

equals

Check whether binary arrays are equal to each other.

includes

Determines whether the source array includes the pattern array.

indexOf

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

lastIndexOf

Find last index of binary pattern from source. If not found, then return -1.

repeat

Repeat bytes. returns a new byte slice consisting of count copies of b.

startsWith

Check whether binary array starts with prefix.

Bytes

The bytes module provides helper functions to manipulate byte slices.

Usage

The following functions are exposed in mod.ts.

indexOf

Finds the first index of a binary pattern within a given binary array, or returns -1 if the pattern is not present.

import { indexOf } from "https://deno.land/std@0.116.0/bytes/mod.ts";

indexOf(
  new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
  new Uint8Array([0, 1, 2]),
); // => returns 2

indexOf(
  new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
  new Uint8Array([0, 1, 2]),
  3,
); // => returns 5

lastIndexOf

Finds the last index of a binary pattern within a given binary array, or returns -1 if the pattern is not present.

import { lastIndexOf } from "https://deno.land/std@0.116.0/bytes/mod.ts";

lastIndexOf(
  new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
  new Uint8Array([0, 1, 2]),
); // => returns 5

lastIndexOf(
  new Uint8Array([0, 1, 2, 3, 3, 0, 1, 2]),
  new Uint8Array([0, 1, 2]),
  3,
); // => returns 0

equals

Checks whether two given binary arrays are equal to each other.

import { equals } from "https://deno.land/std@0.116.0/bytes/mod.ts";

equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3])); // returns true
equals(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 4])); // returns false

startsWith

Checks whether a binary array starts with a binary array prefix.

import { startsWith } from "https://deno.land/std@0.116.0/bytes/mod.ts";

startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns true
startsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns false

endsWith

Checks whether binary array ends with a binary array suffix.

import { endsWith } from "https://deno.land/std@0.116.0/bytes/mod.ts";

endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1])); // returns false
endsWith(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2])); // returns true

repeat

Creates a binary array consisting of multiple copies of a given binary array.

import { repeat } from "https://deno.land/std@0.116.0/bytes/mod.ts";

repeat(new Uint8Array([1]), 3); // returns Uint8Array(3) [ 1, 1, 1 ]

concat

Concatenates multiple binary arrays into a new one.

import { concat } from "https://deno.land/std@0.116.0/bytes/mod.ts";

concat(new Uint8Array([1, 2]), new Uint8Array([3, 4])); // returns Uint8Array(4) [ 1, 2, 3, 4 ]

concat(
  new Uint8Array([1, 2]),
  new Uint8Array([3, 4]),
  new Uint8Array([5, 6]),
  new Uint8Array([7, 8]),
); // => returns Uint8Array(8) [ 1, 2, 3, 4, 5, 6, 7, 8 ]

includes

Checks that a given binary array includes a sequence corresponding to a pattern array.

import { includes } from "https://deno.land/std@0.116.0/bytes/mod.ts";

includes(
  new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
  new Uint8Array([0, 1, 2]),
); // => returns true

includes(
  new Uint8Array([1, 2, 0, 1, 2, 0, 2, 1, 3]),
  new Uint8Array([2, 2]),
); // => returns false

copy

Copies bytes from one binary array to another one.

import { copy } from "https://deno.land/std@0.116.0/bytes/mod.ts";

const dest = new Uint8Array(4);
const src = Uint8Array.of(1, 2, 3, 4);
const len = copy(src, dest); // returns len = 4