shelving
Version:
Toolkit for using data in JavaScript.
23 lines (22 loc) • 1.36 kB
TypeScript
import type { AnyCaller } from "./function.js";
/** We store a sets of bytes as a `Uint8Array` byte sequence. */
export type Bytes = Uint8Array<ArrayBuffer>;
/** Types that can be converted to a `Uint8Array` byte sequence. */
export type PossibleBytes = Bytes | ArrayBuffer | string;
/** Is an unknown value a set of bytes? */
export declare function isBytes(value: unknown): value is Bytes;
/** Assert that an unknown value is a `Uint8Array` byte sequence. */
export declare function assertBytes(value: unknown, min?: number, max?: number, caller?: AnyCaller): asserts value is Bytes;
/**
* Convert an unknown value to a `Uint8Array<ArrayBuffer>` byte sequence, or `undefined` if the value cannot be converted.
*
* - `Uint8Array` values are returned as-is (if backed by an `ArrayBuffer`) or copied to a new `Uint8Array` (if not).
* - `ArrayBuffer` instances are converted to `Uint8Array`
* - Strings are encoded as UTF-8 characters in a `Uint8Array`
* - Everything else returns `undefined`
*/
export declare function getBytes(value: unknown): Uint8Array<ArrayBuffer> | undefined;
/**
* Convert a possible set of bytes to a `Uint8Array` byte sequence, or throw `RequiredError` if the value cannot be converted.
*/
export declare function requireBytes(value: PossibleBytes, min?: number, max?: number, caller?: AnyCaller): Uint8Array<ArrayBuffer>;