ts-data-forge
    Preparing search index...
    SafeInt: {
        abs: (x: WithSmallInt) => ToNonNegative<SafeInt>;
        add: (x: WithSmallInt, y: WithSmallInt) => SafeInt;
        clamp: (x: number) => SafeInt;
        div: (x: WithSmallInt, y: ToNonZeroIntWithSmallInt<SafeInt>) => SafeInt;
        is: (a: number) => a is SafeInt;
        max: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt;
        MAX_VALUE: SafeUint;
        min: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt;
        MIN_VALUE: SafeInt;
        mul: (x: WithSmallInt, y: WithSmallInt) => SafeInt;
        pow: (x: WithSmallInt, y: WithSmallInt) => SafeInt;
        random: (
            min?: WithSmallInt<SafeInt, 40>,
            max?: WithSmallInt<SafeInt, 40>,
        ) => SafeInt;
        sub: (x: WithSmallInt, y: WithSmallInt) => SafeInt;
    } = ...

    Namespace providing type-safe operations for SafeInt branded types.

    SafeInt represents integers that can be exactly represented in JavaScript's number type without precision loss. The range is [±(2^53 - 1)], which covers approximately ±9 quadrillion.

    All operations automatically clamp results to stay within the safe range, preventing precision loss that occurs with larger integers. This makes SafeInt ideal for:

    • Financial calculations requiring exact cents
    • Database IDs and counters
    • Array indices and sizes
    • Any integer arithmetic requiring precision guarantees

    Type Declaration

    • abs: (x: WithSmallInt) => ToNonNegative<SafeInt>

      Returns the absolute value of a safe integer.

      Note: Math.abs(MIN_SAFE_INTEGER) would exceed MAX_SAFE_INTEGER, so this function clamps the result to maintain the safe integer guarantee.

      const negative = asSafeInt(-900);
      const absolute = SafeInt.abs(negative);

      assert(absolute === 900);
      assert.ok(SafeInt.is(absolute));
    • add: (x: WithSmallInt, y: WithSmallInt) => SafeInt

      Adds two SafeInt values.

      const sum = SafeInt.add(asSafeInt(9), asSafeInt(4));

      assert(sum === 13);
      assert.ok(SafeInt.is(sum));
    • clamp: (x: number) => SafeInt

      Clamps a number to the safe integer range.

      const aboveRange = SafeInt.clamp(1e20);
      const withinRange = SafeInt.clamp(123);
      const belowRange = SafeInt.clamp(-1e20);

      assert(aboveRange === Number.MAX_SAFE_INTEGER);
      assert(withinRange === 123);
      assert(belowRange === Number.MIN_SAFE_INTEGER);
    • div: (x: WithSmallInt, y: ToNonZeroIntWithSmallInt<SafeInt>) => SafeInt

      Divides one SafeInt by another using floor division.

      Performs mathematical floor division: ⌊a / b⌋. The divisor must be non-zero (enforced by type constraints).

      const quotient = SafeInt.div(asSafeInt(-17), asSafeInt(5));

      assert(quotient === -4);
      assert.ok(SafeInt.is(quotient));
    • is: (a: number) => a is SafeInt

      Type guard that checks if a value is a safe integer.

      assert.ok(isSafeInt(Number.MAX_SAFE_INTEGER));
      assert.notOk(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));
      assert.ok(SafeInt.is(Number.MIN_SAFE_INTEGER));

      isSafeInt for usage examples

    • Readonlymax: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt

      Returns the maximum value from a list of safe integers.

      const largest = SafeInt.max(asSafeInt(25), asSafeInt(-14), asSafeInt(99));

      assert(largest === 99);
    • ReadonlyMAX_VALUE: SafeUint

      The maximum safe integer value (2^53 - 1).

    • Readonlymin: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt

      Returns the minimum value from a list of safe integers.

      const smallest = SafeInt.min(asSafeInt(25), asSafeInt(-14), asSafeInt(99));

      assert(smallest === -14);
    • ReadonlyMIN_VALUE: SafeInt

      The minimum safe integer value (-(2^53 - 1)).

    • mul: (x: WithSmallInt, y: WithSmallInt) => SafeInt

      Multiplies two SafeInt values.

      const product = SafeInt.mul(asSafeInt(-8), asSafeInt(7));

      assert(product === -56);
      assert.ok(SafeInt.is(product));
    • pow: (x: WithSmallInt, y: WithSmallInt) => SafeInt

      Raises a SafeInt to the power of another SafeInt.

      const base = asSafeInt(3);
      const exponent = asSafeInt(5);
      const power = SafeInt.pow(base, exponent);

      assert(power === 243);
      assert.ok(SafeInt.is(power));
    • random: (min?: WithSmallInt<SafeInt, 40>, max?: WithSmallInt<SafeInt, 40>) => SafeInt

      Generates a random safe integer within the specified range (inclusive).

      The range is inclusive on both ends. If min > max, they are automatically swapped.

      const min = asSafeInt(-10);
      const max = asSafeInt(10);
      const randomValue = SafeInt.random(min, max);

      assert.ok(SafeInt.is(randomValue));
      assert.ok(randomValue >= -10 && randomValue <= 10);
    • sub: (x: WithSmallInt, y: WithSmallInt) => SafeInt

      Subtracts one SafeInt from another.

      const difference = SafeInt.sub(asSafeInt(9), asSafeInt(14));

      assert(difference === -5);
      assert.ok(SafeInt.is(difference));