1.2.11 • Published 1 year ago
@hazae41/bytes v1.2.11
Bytes
Utilities to deal with sized Uint8Array
npm i @hazae41/bytesFeatures
- 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
const bytes8 = Bytes.alloc(8) // Bytes<8>Unsafe-allocated sized bytes
const bytes8 = Bytes.allocUnsafe(8) // Bytes<8>Random sized bytes
const bytes8 = Bytes.random(8) // Bytes<8>Unknown-sized bytes
const bytesX = new Uint8Array(8) // Bytes<number>Runtime type-guarding
if (Bytes.is(bytesX, 8))
bytesX // Bytes<8>
else
bytesX // Bytes<number>Type-guarded runtime equality check
if (Bytes.equals(bytesX, bytes8))
bytesX // Bytes<8>
else
bytesX // Bytes<number>Runtime casting with Result pattern
const bytes16 = Bytes.tryCast(bytesX, 16).unwrap() // Bytes<16>Conversion from sized arrays
const sized4 = Sized.cast([1, 2, 3, 4], 4).unwrap() // Sized<4>
const bytes4 = 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
const bytes8 = Bytes.sliceOrPadStart(bytesX, 8) // Bytes<8>1.2.10
1 year ago
1.2.11
1 year ago
1.2.0
2 years ago
1.2.8
2 years ago
1.2.7
2 years ago
1.2.6
2 years ago
1.2.5
2 years ago
1.2.3
2 years ago
1.2.2
2 years ago
1.2.1
2 years ago
1.2.9
2 years ago
1.1.22
2 years ago
1.1.1
3 years ago
1.1.0
3 years ago
1.1.9
3 years ago
1.1.8
3 years ago
1.1.7
3 years ago
1.1.6
3 years ago
1.1.5
3 years ago
1.1.4
3 years ago
1.1.3
3 years ago
1.1.2
3 years ago
1.1.12
3 years ago
1.1.11
3 years ago
1.1.10
3 years ago
1.1.16
2 years ago
1.1.15
2 years ago
1.1.14
2 years ago
1.1.13
2 years ago
1.1.19
2 years ago
1.1.18
2 years ago
1.1.17
2 years ago
1.1.21
2 years ago
1.1.20
2 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago