UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

128 lines 4.56 kB
/** * Checks if a number is a Uint8 (8-bit unsigned integer in the range [0, 255]). * @param value The value to check. * @returns `true` if the value is a Uint8, `false` otherwise. */ export declare const isUint8: (x: number) => x is Uint8; /** * Casts a number to a Uint8 type. * @param value The value to cast. * @returns The value as a Uint8 type. * @throws {TypeError} If the value is not a valid 8-bit unsigned integer. * @example * ```typescript * const x = asUint8(255); // Uint8 * const y = asUint8(0); // Uint8 * // asUint8(-1); // throws TypeError * // asUint8(256); // throws TypeError * // asUint8(1.5); // throws TypeError * ``` */ export declare const asUint8: (x: number) => Uint8; /** * Namespace providing type-safe arithmetic operations for 8-bit unsigned integers. * * All operations automatically clamp results to the valid Uint8 range [0, 255]. * This ensures that all arithmetic maintains the 8-bit unsigned integer constraint, * with negative results clamped to 0 and overflow results clamped to MAX_VALUE. * * @example * ```typescript * const a = asUint8(200); * const b = asUint8(100); * * // Arithmetic operations with automatic clamping * const sum = Uint8.add(a, b); // Uint8 (255 - clamped to MAX_VALUE) * const diff = Uint8.sub(a, b); // Uint8 (100) * const reverseDiff = Uint8.sub(b, a); // Uint8 (0 - clamped to MIN_VALUE) * const product = Uint8.mul(a, b); // Uint8 (255 - clamped due to overflow) * * // Range operations * const clamped = Uint8.clamp(-10); // Uint8 (0) * const minimum = Uint8.min(a, b); // Uint8 (100) * const maximum = Uint8.max(a, b); // Uint8 (200) * * // Utility operations * const random = Uint8.random(asUint8(50), asUint8(150)); // Uint8 (random value in [50, 150]) * const power = Uint8.pow(asUint8(2), asUint8(7)); // Uint8 (128) * ``` */ export declare const Uint8: { /** * Type guard to check if a value is a Uint8. * @param value The value to check. * @returns `true` if the value is an 8-bit unsigned integer, `false` otherwise. */ readonly is: (x: number) => x is Uint8; /** * The minimum value for an 8-bit unsigned integer. * @readonly */ readonly MIN_VALUE: 0; /** * The maximum value for an 8-bit unsigned integer. * @readonly */ readonly MAX_VALUE: 255; /** * Returns the larger of the given Uint8 values. * @param values The Uint8 values to compare. * @returns The maximum value as a Uint8. */ readonly max: (...values: readonly Uint8[]) => Uint8; /** * Returns the smaller of the given Uint8 values. * @param values The Uint8 values to compare. * @returns The minimum value as a Uint8. */ readonly min: (...values: readonly Uint8[]) => Uint8; /** * Clamps a number to the Uint8 range. * @param value The number to clamp. * @returns The value clamped to [0, 255] as a Uint8. */ readonly clamp: (a: number) => Uint8; /** * Generates a random Uint8 value within the specified range. * @param min The minimum value (inclusive). * @param max The maximum value (inclusive). * @returns A random Uint8 between min and max. */ readonly random: (min: Uint8, max: Uint8) => Uint8; /** * Raises a Uint8 to the power of another Uint8. * @param a The base Uint8. * @param b The exponent Uint8. * @returns `a ** b` clamped to [0, 255] as a Uint8. */ readonly pow: (x: Uint8, y: Uint8) => Uint8; /** * Adds two Uint8 values. * @param a The first Uint8. * @param b The second Uint8. * @returns `a + b` clamped to [0, 255] as a Uint8. */ readonly add: (x: Uint8, y: Uint8) => Uint8; /** * Subtracts one Uint8 from another. * @param a The minuend Uint8. * @param b The subtrahend Uint8. * @returns `a - b` clamped to [0, 255] as a Uint8 (minimum 0). */ readonly sub: (x: Uint8, y: Uint8) => Uint8; /** * Multiplies two Uint8 values. * @param a The first Uint8. * @param b The second Uint8. * @returns `a * b` clamped to [0, 255] as a Uint8. */ readonly mul: (x: Uint8, y: Uint8) => Uint8; /** * Divides one Uint8 by another using floor division. * @param a The dividend Uint8. * @param b The divisor Uint8 (cannot be 0). * @returns `⌊a / b⌋` clamped to [0, 255] as a Uint8. */ readonly div: (x: Uint8, y: Exclude<Uint8, 0>) => Uint8; }; //# sourceMappingURL=uint8.d.mts.map