# @hazae41/bytes

> Utilities to deal with Uint8Array

Latest version **1.2.11** (published 2024-08-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hazae41/bytes
pnpm add @hazae41/bytes
yarn add @hazae41/bytes
bun add @hazae41/bytes
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.11 |
| Published | 2024-08-31 |
| First published | 2023-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 53.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | hazae41 |
| Maintainers | hazae41 |
| Keywords | bytes, zero-copy, encoding, decoding, buffer, uint8array, protocol, packets, reader, writer, offset, asn1, der, ber, octets, stream, low-level, alloc, allocation, allocator, typescript, esmodules, tested, unit-tested |

## Links

- npm: https://www.npmjs.com/package/@hazae41/bytes
- Repository: https://github.com/hazae41/bytes
- Issues: https://github.com/hazae41/bytes/issues
- npm.io page: https://npm.io/package/@hazae41/bytes

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 1.2.11 (latest) — 2024-08-31
- 1.2.10 — 2024-08-31
- 1.2.9 — 2023-11-16
- 1.2.8 — 2023-11-15
- 1.2.7 — 2023-11-11
- 1.2.6 — 2023-11-05
- 1.2.5 — 2023-11-05
- 1.2.3 — 2023-10-11
- 1.2.2 — 2023-09-08
- 1.2.1 — 2023-09-07
- 1.2.0 — 2023-09-07
- 1.1.22 — 2023-08-29
- 1.1.21 — 2023-06-13
- 1.1.20 — 2023-06-13
- 1.1.19 — 2023-06-01
- … 24 more at https://npm.io/package/@hazae41/bytes/versions

## 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<number>
```

#### Runtime type-guarding

```tsx
if (Bytes.is(bytesX, 8))
  bytesX // Bytes<8>
else
  bytesX // Bytes<number>
```

#### Type-guarded runtime equality check

```tsx
if (Bytes.equals(bytesX, bytes8))
  bytesX // Bytes<8>
else
  bytesX // Bytes<number>
```

#### 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>
```

---
_Source: https://npm.io/package/@hazae41/bytes · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
