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

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

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

    Unlike SafeInt, PositiveInt allows values outside the safe integer range (±2^53 − 1), so very large magnitudes may lose precision in JavaScript's number type.

    Type Declaration

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

      Adds two positive integers, returning a + b as a PositiveInt.

      const sum = PositiveInt.add(asPositiveInt(4), asPositiveInt(5));

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

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

      const quotient = PositiveInt.div(asPositiveInt(9), asPositiveInt(2));

      const clamped = PositiveInt.div(asPositiveInt(3), asPositiveInt(10));

      assert.isTrue(quotient === 4);

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

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

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

      const belowRange = PositiveInt.fromNumber(0);

      const withinRange = PositiveInt.fromNumber(10);

      assert.isTrue(belowRange === 1);

      assert.isTrue(withinRange === 10);
    • is: (a: number) => a is PositiveInt

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

      assert.isTrue(isPositiveInt(5));

      assert.isFalse(isPositiveInt(0));

      assert.isTrue(PositiveInt.is(10));

      isPositiveInt for usage examples

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

      Returns the largest of the given positive integers.

      const largest = PositiveInt.max(
      asPositiveInt(9),
      asPositiveInt(3),
      asPositiveInt(12),
      );

      assert.isTrue(largest === 12);
    • Readonlymin: (...values: readonly WithSmallInt<PositiveInt>[]) => PositiveInt

      Returns the smallest of the given positive integers.

      const smallest = PositiveInt.min(
      asPositiveInt(9),
      asPositiveInt(3),
      asPositiveInt(12),
      );

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

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

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

      Multiplies two positive integers, returning a * b as a PositiveInt.

      const product = PositiveInt.mul(asPositiveInt(3), asPositiveInt(7));

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

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

      const base = asPositiveInt(2);

      const exponent = asPositiveInt(4);

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

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

      Generates a random PositiveInt within the given range.

      The range is inclusive on both ends.

      const min = asPositiveInt(3);

      const max = asPositiveInt(6);

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

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

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

      Subtracts two positive integers, returning a - b as a PositiveInt.

      const difference = PositiveInt.sub(asPositiveInt(5), asPositiveInt(7));

      assert.isTrue(difference === 1);