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

    Namespace providing type-safe operations for the SafeInt branded type.

    The SafeInt type represents a safe integer. Division (div) uses floor division.

    Type Declaration

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

      Returns the absolute value of a safe integer.

      The result is non-negative and keeps the SafeInt brand.

      const negative = asSafeInt(-900);

      const absolute = SafeInt.abs(negative);

      assert.isTrue(absolute === 900);

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

      Adds two safe integers, returning a + b as a SafeInt.

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

      assert.isTrue(sum === 13);

      assert.isTrue(SafeInt.is(sum));
    • div: (x: WithSmallInt, y: ToNonZeroIntWithSmallInt<SafeInt>) => SafeInt

      Divides two safe integers using floor division (⌊a / b⌋): the result is a / b rounded toward negative infinity, as a SafeInt.

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

      assert.isTrue(quotient === -4);

      assert.isTrue(SafeInt.is(quotient));
    • fromNumber: (x: number) => SafeInt

      Converts an arbitrary number into a SafeInt, rounding to the nearest integer and saturating the result into the range [MIN_VALUE, MAX_VALUE].

      Unlike asSafeInt, this is total: out-of-range inputs are clamped to the nearest representable SafeInt instead of throwing.

      const aboveRange = SafeInt.fromNumber(1e20);

      const withinRange = SafeInt.fromNumber(123);

      const belowRange = SafeInt.fromNumber(-1e20);

      assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);

      assert.isTrue(withinRange === 123);

      assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);
    • is: (a: number) => a is SafeInt

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

      assert.isTrue(isSafeInt(Number.MAX_SAFE_INTEGER));

      assert.isFalse(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));

      assert.isTrue(SafeInt.is(Number.MIN_SAFE_INTEGER));

      isSafeInt for usage examples

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

      Returns the largest of the given safe integers.

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

      assert.isTrue(largest === 99);
    • MAX_VALUE: SafeUint

      The largest value representable as SafeInt (the upper saturation target of fromNumber).

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

      Returns the smallest of the given safe integers.

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

      assert.isTrue(smallest === -14);
    • MIN_VALUE: SafeInt

      The smallest value representable as SafeInt (the lower saturation target of fromNumber).

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

      Multiplies two safe integers, returning a * b as a SafeInt.

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

      assert.isTrue(product === -56);

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

      Raises a to the power b, returning a ** b as a SafeInt (floored to an integer).

      const base = asSafeInt(3);

      const exponent = asSafeInt(5);

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

      assert.isTrue(power === 243);

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

      Generates a random SafeInt within the given range.

      The range is inclusive on both ends.

      const min = asSafeInt(-10);

      const max = asSafeInt(10);

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

      assert.isTrue(SafeInt.is(randomValue));

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

      Subtracts two safe integers, returning a - b as a SafeInt.

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

      assert.isTrue(difference === -5);

      assert.isTrue(SafeInt.is(difference));