ts-data-forge
    Preparing search index...
    NonPositiveInt: {
        add: (x: WithSmallInt, y: WithSmallInt) => NonPositiveInt;
        clamp: (x: number) => NonPositiveInt;
        is: (a: number) => a is NonPositiveInt;
        max: (...values: readonly WithSmallInt<NonPositiveInt>[]) => NonPositiveInt;
        MAX_VALUE: 0;
        min: (...values: readonly WithSmallInt<NonPositiveInt>[]) => NonPositiveInt;
        pow: (x: WithSmallInt, y: WithSmallInt) => NonPositiveInt;
        random: (
            min?: WithSmallInt<NonPositiveInt>,
            max?: WithSmallInt<NonPositiveInt>,
        ) => NonPositiveInt;
        sub: (x: WithSmallInt, y: WithSmallInt) => NonPositiveInt;
    }

    Namespace providing type-safe operations for NonPositiveInt branded types.

    NonPositiveInt represents integers that are less than or equal to zero (<= 0). All operations automatically clamp results to maintain the non-positive constraint, ensuring that arithmetic operations never produce positive values.

    This type is essential for:

    • Debts and negative balances
    • Penalties and reductions
    • Offsets and adjustments (downward)
    • Negative counts or differences
    • Depth or altitude below a reference point

    Type Declaration

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

      Adds two non-positive integers.

      The result is clamped to the non-positive integer range. If the sum is positive, it is clamped to 0.

      assert.strictEqual(NonPositiveInt.add(-5, -3), -8);

      assert.strictEqual(NonPositiveInt.add(-5, 10), 0);
    • clamp: (x: number) => NonPositiveInt

      Clamps a number to the non-positive integer range.

      Any positive value is clamped to 0 (the maximum non-positive integer). Negative values are preserved as long as they are valid integers.

      assert.strictEqual(NonPositiveInt.clamp(5), 0);

      assert.strictEqual(NonPositiveInt.clamp(-5), -5);

      assert.strictEqual(NonPositiveInt.clamp(0), 0);
    • is: (a: number) => a is NonPositiveInt

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

      const value: number = -42;

      if (NonPositiveInt.is(value)) {
      // value is now typed as NonPositiveInt
      const debt = value;
      }
    • Readonlymax: (...values: readonly WithSmallInt<NonPositiveInt>[]) => NonPositiveInt

      Returns the larger of two non-positive integers.

      Since non-positive integers range from negative infinity to 0, the larger value is the one closer to zero (less negative).

      const result = NonPositiveInt.max(-5, -10);

      assert.strictEqual(result, -5);
    • ReadonlyMAX_VALUE: 0

      The maximum value for a non-positive integer.

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

      Returns the smaller of two non-positive integers.

      Since non-positive integers range from negative infinity to 0, the smaller value is the one further from zero (more negative).

      const result = NonPositiveInt.min(-5, -10);

      assert.strictEqual(result, -10);
    • pow: (x: WithSmallInt, y: WithSmallInt) => NonPositiveInt

      Raises a non-positive integer to the power of another non-positive integer.

      The result is clamped to the non-positive integer range.

      const result = NonPositiveInt.pow(-2, -1);
      
    • random: (
          min?: WithSmallInt<NonPositiveInt>,
          max?: WithSmallInt<NonPositiveInt>,
      ) => NonPositiveInt

      Generates a random non-positive integer value.

      const randomValue = NonPositiveInt.random();

      assert.isTrue(NonPositiveInt.is(randomValue));
    • sub: (x: WithSmallInt, y: WithSmallInt) => NonPositiveInt

      Subtracts one non-positive integer from another.

      The result is clamped to the non-positive integer range. If the result is positive, it is clamped to 0.

      assert.strictEqual(NonPositiveInt.sub(-10, -3), -7);

      assert.strictEqual(NonPositiveInt.sub(-10, 5), 0);