Skip to content

BigNum

const BigNum: object

Defined in: Data/BigNum.ts:18

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

abs: (a) => bigint

Returns absolute value of a bigint.

bigint

bigint

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

add: (b) => (a) => bigint

Adds b to a. Data-last curried signature: add(b)(a) = a + b.

bigint

(a) => bigint

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

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

Clamps a between min and max (inclusive).

bigint

bigint

(a) => bigint

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

div: (b) => (a) => Maybe<bigint>

Divides a by b. Returns None if b is 0n.

bigint

(a) => Maybe<bigint>

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

from: object

number: (n) => 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: (s) => 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) => (a) => boolean

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

bigint

bigint

(a) => boolean

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

is: object

even: (b) => boolean

Returns true when the bigint is an even integer.

bigint

boolean

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

negative: (b) => boolean

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

bigint

boolean

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

odd: (b) => boolean

Returns true when the bigint is an odd integer.

bigint

boolean

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

positive: (b) => boolean

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

bigint

boolean

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

zero: (b) => boolean

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

bigint

boolean

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

max: (b) => (a) => bigint

Returns the maximum of a and b.

bigint

(a) => bigint

pipe(10n, BigNum.max(5n)); // 10n

min: (b) => (a) => bigint

Returns the minimum of a and b.

bigint

(a) => bigint

pipe(10n, BigNum.min(5n)); // 5n

mod: (b) => (a) => Maybe<bigint>

Computes remainder of a / b. Returns None if b is 0n.

bigint

(a) => Maybe<bigint>

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

mul: (b) => (a) => bigint

Multiplies a by b. Data-last curried signature: mul(b)(a) = a * b.

bigint

(a) => bigint

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

sub: (b) => (a) => bigint

Subtracts b from a. Data-last curried signature: sub(b)(a) = a - b.

bigint

(a) => bigint

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

to: object

number: (b) => 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 } from "@nlozgachev/pipelined/data";
import { pipe } from "@nlozgachev/pipelined/composition";

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