Skip to content

BigNum

const BigNum: object

Defined in: Data/BigNum.ts:17

Safe conversion and arithmetic utilities for arbitrary-precision integers (bigint). All functions are pure and data-last to compose cleanly with pipe.

abs: (value) => bigint

Returns absolute value of a bigint.

bigint

bigint

BigNum.abs(-42n); // 42n

add: (amount) => (value) => bigint

Adds amount to value. Data-last curried signature: add(amount)(value) = value + amount.

bigint

(value) => bigint

BigNum.subtract to subtract an amount from a bigint.

pipe(10n, BigNum.add(5n)); // 15n

between: (min, max) => (value) => boolean

Returns true when the bigint is between min and max (both inclusive).

bigint

bigint

(value) => boolean

BigNum.inRange for half-open range checking [start, end).

pipe(5n, BigNum.between(1n, 10n)); // true
pipe(0n, BigNum.between(1n, 10n)); // false

clamp: (min, max) => (value) => bigint

Clamps value between min and max (inclusive).

bigint

bigint

(value) => bigint

pipe(150n, BigNum.clamp(0n, 100n)); // 100n

divide: (divisor) => (dividend) => Maybe<bigint>

Divides dividend by divisor. Returns None if divisor is 0n.

bigint

(dividend) => Maybe<bigint>

pipe(20n, BigNum.divide(4n)); // Some(5n)
pipe(5n, BigNum.divide(0n));  // None

from: object

Safe constructors and converters from raw primitives to BigInt.

number: (value) => Maybe<bigint>

Safely converts a number into a bigint. Returns None for floats, NaN, or non-safe integers.

number

Maybe<bigint>

BigNum.from.number(42);   // Some(42n)
BigNum.from.number(3.14); // None

string: (text) => Maybe<bigint>

Safely parses a string into a bigint. Returns None if parsing fails.

string

Maybe<bigint>

BigNum.from.string("123"); // Some(123n)
BigNum.from.string("abc"); // None

inRange: (start, end) => (value) => boolean

Returns true if value is in the range [start, end) (inclusive start, exclusive end).

bigint

bigint

(value) => boolean

BigNum.between for fully closed range checking [min, max].

pipe(5n, BigNum.inRange(1n, 10n)); // true

is: object

Predicates and type guards for BigInt values.

even: (value) => boolean

Returns true when the bigint is an even integer.

bigint

boolean

BigNum.is.odd to check if a bigint is odd.

BigNum.is.even(4n); // true
BigNum.is.even(3n); // false

negative: (value) => boolean

Returns true when the bigint is strictly less than zero (0n).

bigint

boolean

BigNum.is.positive to check if a bigint is greater than zero.

BigNum.is.negative(-5n); // true
BigNum.is.negative(0n);  // false
BigNum.is.negative(5n);  // false

odd: (value) => boolean

Returns true when the bigint is an odd integer.

bigint

boolean

BigNum.is.even to check if a bigint is even.

BigNum.is.odd(3n); // true
BigNum.is.odd(4n); // false

positive: (value) => boolean

Returns true when the bigint is strictly greater than zero (0n).

bigint

boolean

BigNum.is.negative to check if a bigint is less than zero.

BigNum.is.positive(5n);  // true
BigNum.is.positive(0n);  // false
BigNum.is.positive(-5n); // false

zero: (value) => boolean

Returns true when the bigint is equal to zero (0n).

bigint

boolean

BigNum.is.zero(0n); // true
BigNum.is.zero(5n); // false

max: (values) => Maybe<bigint>

Computes the maximum of a list of bigints. Returns None if the list is empty.

readonly bigint[]

Maybe<bigint>

BigNum.min to compute the minimum value.

BigNum.max([1n, 5n, 3n]); // Some(5n)
BigNum.max([]);           // None

min: (values) => Maybe<bigint>

Computes the minimum of a list of bigints. Returns None if the list is empty.

readonly bigint[]

Maybe<bigint>

BigNum.max to compute the maximum value.

BigNum.min([5n, 1n, 3n]); // Some(1n)
BigNum.min([]);           // None

multiply: (factor) => (value) => bigint

Multiplies value by factor. Data-last curried signature: multiply(factor)(value) = value * factor.

bigint

(value) => bigint

BigNum.divide to divide a bigint by a divisor.

pipe(6n, BigNum.multiply(7n)); // 42n

remainder: (divisor) => (dividend) => Maybe<bigint>

Computes remainder of dividend / divisor. Returns None if divisor is 0n.

bigint

(dividend) => Maybe<bigint>

BigNum.divide for full division.

pipe(10n, BigNum.remainder(3n)); // Some(1n)
pipe(5n, BigNum.remainder(0n));  // None

subtract: (amount) => (from) => bigint

Subtracts amount from from. Data-last curried signature: subtract(amount)(from) = from - amount.

bigint

(from) => bigint

BigNum.add to add an amount to a bigint.

pipe(10n, BigNum.subtract(3n)); // 7n

sum: (values) => bigint

Computes the sum of a list of bigints. Returns 0n if the list is empty.

readonly bigint[]

bigint

BigNum.sum([1n, 2n, 3n]); // 6n
BigNum.sum([]);           // 0n

to: object

Converters from BigInt to native types.

number: (value) => Maybe<number>

Safely converts a bigint to a number. Returns None if the value is outside JavaScript’s safe integer range.

bigint

Maybe<number>

BigNum.to.number(42n);                    // Some(42)
BigNum.to.number(9007199254740993n);      // None
import { BigNum, pipe } from "@pplnd/core";

const result = pipe(
  BigNum.from.string("100"),
  Maybe.map(BigNum.add(50n))
); // Some(150n)