https://github.com/hazae41/bytes
Utilities to deal with Uint8Array
https://github.com/hazae41/bytes
buffer bytes helpers javascript typescript uint8array utilities web
Last synced: 11 days ago
JSON representation
Utilities to deal with Uint8Array
- Host: GitHub
- URL: https://github.com/hazae41/bytes
- Owner: hazae41
- Created: 2023-02-06T17:01:43.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2025-08-11T14:23:52.000Z (8 months ago)
- Last Synced: 2025-12-28T02:50:58.359Z (3 months ago)
- Topics: buffer, bytes, helpers, javascript, typescript, uint8array, utilities, web
- Language: TypeScript
- Homepage:
- Size: 2.4 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Bytes
Utilities to deal with sized Uint8Array
```bash
npm i @hazae41/bytes
```
[**Node Package 📦**](https://www.npmjs.com/package/@hazae41/bytes)
## 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
```tsx
const bytes8 = Bytes.alloc(8) // Bytes<8>
```
#### Unsafe-allocated sized bytes
```tsx
const bytes8 = Bytes.allocUnsafe(8) // Bytes<8>
```
#### Random sized bytes
```tsx
const bytes8 = Bytes.random(8) // Bytes<8>
```
#### Unknown-sized bytes
```tsx
const bytesX = new Uint8Array(8) // Bytes
```
#### Runtime type-guarding
```tsx
if (Bytes.is(bytesX, 8))
bytesX // Bytes<8>
else
bytesX // Bytes
```
#### Type-guarded runtime equality check
```tsx
if (Bytes.equals(bytesX, bytes8))
bytesX // Bytes<8>
else
bytesX // Bytes
```
#### Runtime casting with Result pattern
```tsx
const bytes16 = Bytes.tryCast(bytesX, 16).unwrap() // Bytes<16>
```
#### Conversion from sized arrays
```tsx
const sized4 = Sized.cast([1, 2, 3, 4], 4).unwrap() // Sized<4>
const bytes4 = Bytes.from(sized4) // Bytes<4>
```
#### Utf8, Hex, Base64, Ascii encoding
```tsx
Bytes.fromUtf8(Bytes.toUtf8(bytesX))
```
```tsx
Bytes.fromHex(Bytes.toHex(bytesX))
```
```tsx
Bytes.fromBase64(Bytes.toBase64(bytesX))
```
```tsx
Bytes.fromAscii(Bytes.toAscii(bytesX))
```
#### BigInt conversion
```tsx
Bytes.fromBigInt(Bytes.toBigInt(bytesX))
```
#### Sized slicing and padding
```tsx
const bytes8 = Bytes.sliceOrPadStart(bytesX, 8) // Bytes<8>
```