or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-operations.mdindex.mdmemory-allocation.mdstring-encoding.mdxor-operations.md
tile.json

tessl/npm-uint8arrays

Utility functions to make dealing with Uint8Arrays easier

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/uint8arrays@5.1.x

To install, run

npx @tessl/cli install tessl/npm-uint8arrays@5.1.0

index.mddocs/

uint8arrays

uint8arrays is a comprehensive TypeScript utility library providing memory-efficient Uint8Array manipulation functions. It offers essential operations for byte array handling with automatic Node.js Buffer optimizations when available, while maintaining full compatibility across browsers and other JavaScript environments.

Package Information

  • Package Name: uint8arrays
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install uint8arrays

Core Imports

import { compare, concat, equals, fromString, toString, xor } from "uint8arrays";

Submodule imports for tree-shaking:

import { alloc, allocUnsafe } from "uint8arrays/alloc";
import { compare } from "uint8arrays/compare";
import { concat } from "uint8arrays/concat";
import { equals } from "uint8arrays/equals";
import { fromString } from "uint8arrays/from-string";
import { toString } from "uint8arrays/to-string";
import { xor } from "uint8arrays/xor";
import { xorCompare } from "uint8arrays/xor-compare";

CommonJS:

const { compare, concat, equals, fromString, toString, xor } = require("uint8arrays");

Basic Usage

import { concat, fromString, toString, equals } from "uint8arrays";
import { alloc } from "uint8arrays/alloc";

// Create and manipulate arrays
const buf1 = alloc(10); // Zero-initialized 10-byte array
const buf2 = fromString("hello", "utf8"); // Convert string to bytes
const buf3 = fromString("world", "utf8");

// Concatenate arrays
const combined = concat([buf2, new Uint8Array([32]), buf3]); // "hello world"

// Convert back to string
const text = toString(combined, "utf8"); // "hello world"

// Check equality
const areEqual = equals(buf2, fromString("hello", "utf8")); // true

// Encode to different formats
const hex = toString(buf2, "hex"); // "68656c6c6f"
const base64 = toString(buf2, "base64"); // "aGVsbG8"

Architecture

uint8arrays is built around several key principles:

  • Memory Efficiency: Uses native Uint8Array operations with Node.js Buffer optimizations when available
  • Platform Compatibility: Automatically selects the best implementation for each environment
  • Tree Shaking: Supports both main module and individual submodule imports
  • Encoding Support: Comprehensive encoding support via multiformats integration
  • Type Safety: Full TypeScript support with precise type definitions

Capabilities

Memory Allocation

Efficient Uint8Array creation with zero-initialized and unsafe allocation options. Automatically uses Node.js Buffer when available for better performance.

function alloc(size?: number): Uint8Array;
function allocUnsafe(size?: number): Uint8Array;

Memory Allocation

Array Operations

Core array manipulation functions including concatenation, comparison, and equality testing for Uint8Arrays.

function concat(arrays: Uint8Array[], length?: number): Uint8Array;
function compare(a: Uint8Array, b: Uint8Array): number;
function equals(a: Uint8Array, b: Uint8Array): boolean;

Array Operations

String Encoding

Comprehensive string encoding and decoding with support for UTF-8, hex, base64, and all multibase formats.

function fromString(string: string, encoding?: SupportedEncodings): Uint8Array;
function toString(array: Uint8Array, encoding?: SupportedEncodings): string;

type SupportedEncodings = 'utf8' | 'utf-8' | 'hex' | 'latin1' | 'ascii' | 'binary' | keyof typeof bases;

String Encoding

XOR Operations

Bitwise XOR operations and distance comparison functions for cryptographic and DHT applications.

function xor(a: Uint8Array, b: Uint8Array): Uint8Array;
function xorCompare(a: Uint8Array, b: Uint8Array): -1 | 0 | 1;

XOR Operations

Types

type SupportedEncodings = 
  | 'utf8' 
  | 'utf-8' 
  | 'hex' 
  | 'latin1' 
  | 'ascii' 
  | 'binary' 
  | 'base2'
  | 'base8'
  | 'base10'
  | 'base16'
  | 'base16upper'
  | 'base32'
  | 'base32upper'
  | 'base32pad'
  | 'base32padupper'
  | 'base32hex'
  | 'base32hexupper'
  | 'base32hexpad'
  | 'base32hexpadupper'
  | 'base32z'
  | 'base36'
  | 'base36upper'
  | 'base58btc'
  | 'base58flickr'
  | 'base64'
  | 'base64pad'
  | 'base64url'
  | 'base64urlpad';