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

    Namespace providing type-safe operations for PositiveInt branded types.

    PositiveInt represents integers that are strictly greater than zero (>= 1). All operations automatically clamp results to maintain the positive constraint, ensuring that arithmetic operations never produce zero or negative values.

    This type is essential for:

    • Array lengths and sizes (length >= 1)
    • Counts and quantities that must be positive
    • Denominators in division operations
    • Loop counters and iteration counts
    • Database primary keys and IDs

    Type Declaration

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

      Adds two positive integers, ensuring the result is never less than 1.

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

      assert(sum === 9);
    • clamp: (x: number) => PositiveInt

      Clamps a number to the positive integer range.

      Since PositiveInt has a minimum value of 1, this function ensures that any input less than 1 is clamped to 1.

      const belowRange = PositiveInt.clamp(0);
      const withinRange = PositiveInt.clamp(10);

      assert(belowRange === 1);
      assert(withinRange === 10);
    • div: (x: WithSmallInt, y: ToNonZeroIntWithSmallInt<PositiveInt>) => PositiveInt

      Divides two positive integers using floor division, clamping to remain positive.

      Performs mathematical floor division: ⌊a / b⌋. If the result would be 0 (when a < b), it is clamped to 1 to maintain the positive integer constraint.

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

      assert(quotient === 4);
      assert(clamped === 1);
    • is: (a: number) => a is PositiveInt

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

      assert.ok(isPositiveInt(5));
      assert.notOk(isPositiveInt(0));
      assert.ok(PositiveInt.is(10));

      isPositiveInt for usage examples

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

      Returns the maximum value from a list of positive integers.

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

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

      Returns the minimum value from a list of positive integers.

      Since all inputs are guaranteed to be >= 1, the result is also guaranteed to be a positive integer.

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

      assert(smallest === 3);
    • ReadonlyMIN_VALUE: 1

      The minimum value for a PositiveInt.

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

      Multiplies two positive integers, ensuring the result is never less than 1.

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

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

      Raises a positive integer to a power, ensuring the result is never less than 1.

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

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

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

      Both bounds are inclusive, and both min and max must be positive integers. If min > max, they are automatically swapped.

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

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

      Subtracts two positive integers, clamping the result to remain positive.

      If the mathematical result would be <= 0, it is clamped to 1 to maintain the positive integer constraint.

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

      assert(difference === 1);