Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Utilities to deal with Uint8Array

NotificationsYou must be signed in to change notification settings

hazae41/bytes

Repository files navigation

Utilities to deal with sized Uint8Array

npm i @hazae41/bytes

Node Package 📦

Features

  • 100% TypeScript and ESM
  • Rust-like patterns
  • Strongly typed Uint8Array and ArrayLike size
  • Zero-cost abstraction over Uint8Array and ArrayLike
  • Zero-copy conversion from ArrayBufferView
  • Use native Buffer for faster execution on Node
  • Unit-tested

Usage

Sized bytes

constbytes8=Bytes.alloc(8)// Bytes<8>

Unsafe-allocated sized bytes

constbytes8=Bytes.allocUnsafe(8)// Bytes<8>

Random sized bytes

constbytes8=Bytes.random(8)// Bytes<8>

Unknown-sized bytes

constbytesX=newUint8Array(8)// Bytes<number>

Runtime type-guarding

if(Bytes.is(bytesX,8))bytesX// Bytes<8>elsebytesX// Bytes<number>

Type-guarded runtime equality check

if(Bytes.equals(bytesX,bytes8))bytesX// Bytes<8>elsebytesX// Bytes<number>

Runtime casting with Result pattern

constbytes16=Bytes.tryCast(bytesX,16).unwrap()// Bytes<16>

Conversion from sized arrays

constsized4=Sized.cast([1,2,3,4],4).unwrap()// Sized<4>constbytes4=Bytes.from(sized4)// Bytes<4>

Utf8, Hex, Base64, Ascii encoding

Bytes.fromUtf8(Bytes.toUtf8(bytesX))
Bytes.fromHex(Bytes.toHex(bytesX))
Bytes.fromBase64(Bytes.toBase64(bytesX))
Bytes.fromAscii(Bytes.toAscii(bytesX))

BigInt conversion

Bytes.fromBigInt(Bytes.toBigInt(bytesX))

Sized slicing and padding

constbytes8=Bytes.sliceOrPadStart(bytesX,8)// Bytes<8>

[8]ページ先頭

©2009-2025 Movatter.jp