import { isBigInt } from "./is.ts" /** * @description Check whether a value is zero as a bigint. * * @example * ``` * // Expect: true * const example1 = bigintIsZero(0n) * // Expect: false * const example2 = bigintIsZero(2n) * ``` */ export const bigintIsZero = (value: unknown): value is 0n => { return isBigInt(value) && value === 0n } /** * @description Check whether a value is a positive bigint. * * @example * ``` * // Expect: true * const example1 = bigintIsPositive(3n) * // Expect: false * const example2 = bigintIsPositive(-1n) * ``` */ export const bigintIsPositive = (value: unknown): value is bigint => { return isBigInt(value) && value > 0n } /** * @description Check whether a value is a negative bigint. * * @example * ``` * // Expect: true * const example1 = bigintIsNegative(-3n) * // Expect: false * const example2 = bigintIsNegative(1n) * ``` */ export const bigintIsNegative = (value: unknown): value is bigint => { return isBigInt(value) && value < 0n } /** * @description Get the absolute value of a bigint. * * @example * ``` * // Expect: 5n * const example1 = bigintAbs(-5n) * // Expect: 2n * const example2 = bigintAbs(2n) * ``` */ export const bigintAbs = (value: bigint): bigint => { return value < 0n ? -value : value } /** * @description Get the smaller of two bigint values. * * @example * ``` * // Expect: 2n * const example1 = bigintMinOf(2n, 9n) * // Expect: -1n * const example2 = bigintMinOf(5n, -1n) * ``` */ export const bigintMinOf = (a: bigint, b: bigint): bigint => { return a < b ? a : b } /** * @description Get the larger of two bigint values. * * @example * ``` * // Expect: 9n * const example1 = bigintMaxOf(2n, 9n) * // Expect: 5n * const example2 = bigintMaxOf(5n, -1n) * ``` */ export const bigintMaxOf = (a: bigint, b: bigint): bigint => { return a > b ? a : b } /** * @description Clamp a bigint between two bounds. * * @example * ``` * // Expect: 5n * const example1 = bigintClamp(0n, 10n, 5n) * // Expect: 0n * const example2 = bigintClamp(0n, 10n, -3n) * ``` */ export const bigintClamp = (min: bigint, max: bigint, value: bigint): bigint => { const minValue = bigintMinOf(min, max) const maxValue = bigintMaxOf(min, max) if (value < minValue) { return minValue } if (value > maxValue) { return maxValue } return value }