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

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

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

    Type Declaration

    • add: (x: WithSmallInt, y: WithSmallInt) => PositiveSafeInt

      Adds two positive safe integers, returning a + b as a PositiveSafeInt.

      const sum = PositiveSafeInt.add(
      asPositiveSafeInt(1000),
      asPositiveSafeInt(2048),
      );

      assert.isTrue(sum === 3048);

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

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

      const quotient = PositiveSafeInt.div(
      asPositiveSafeInt(25),
      asPositiveSafeInt(4),
      );

      const clamped = PositiveSafeInt.div(
      asPositiveSafeInt(5),
      asPositiveSafeInt(50),
      );

      assert.isTrue(quotient === 6);

      assert.isTrue(clamped === 1);
    • fromNumber: (x: number) => PositiveSafeInt

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

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

      const belowRange = PositiveSafeInt.fromNumber(0);

      const withinRange = PositiveSafeInt.fromNumber(123);

      const aboveRange = PositiveSafeInt.fromNumber(Number.MAX_SAFE_INTEGER + 10);

      assert.isTrue(belowRange === 1);

      assert.isTrue(withinRange === 123);

      assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);
    • is: (a: number) => a is PositiveSafeInt

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

      assert.isTrue(isPositiveSafeInt(1));

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

      assert.isFalse(isPositiveSafeInt(0));

      assert.isTrue(PositiveSafeInt.is(42));

      isPositiveSafeInt for usage examples

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

      Returns the largest of the given positive safe integers.

      const largest = PositiveSafeInt.max(
      asPositiveSafeInt(10),
      asPositiveSafeInt(5),
      );

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

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

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

      Returns the smallest of the given positive safe integers.

      const smallest = PositiveSafeInt.min(
      asPositiveSafeInt(10),
      asPositiveSafeInt(5),
      );

      assert.isTrue(smallest === 5);
    • MIN_VALUE: 1

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

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

      Multiplies two positive safe integers, returning a * b as a PositiveSafeInt.

      const product = PositiveSafeInt.mul(
      asPositiveSafeInt(50),
      asPositiveSafeInt(20),
      );

      assert.isTrue(product === 1000);

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

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

      const base = asPositiveSafeInt(3);

      const exponent = asPositiveSafeInt(3);

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

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

      Generates a random PositiveSafeInt within the given range.

      The range is inclusive on both ends.

      const min = asPositiveSafeInt(1);

      const max = asPositiveSafeInt(6);

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

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

      assert.isTrue(randomValue >= 1 && randomValue <= 6);
    • sub: (x: WithSmallInt, y: WithSmallInt) => PositiveSafeInt

      Subtracts two positive safe integers, returning a - b as a PositiveSafeInt.

      const difference = PositiveSafeInt.sub(
      asPositiveSafeInt(10),
      asPositiveSafeInt(20),
      );

      assert.isTrue(difference === 1);

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