PositiveInteger
Integer value object constrained to non-negative values.
Import
typescript
import { PositiveInteger } from '@haskou/value-objects';Signature
typescript
class PositiveInteger extends PositiveNumberConstructor
typescript
constructor(value: number | NumberValueObject)Validation
Accepts integers greater than or equal to 0.
Throws
This class can throw:
InvalidPositiveNumberErrorInvalidIntegerErrorInvalidNumberError
Example
typescript
import { PositiveInteger } from '@haskou/value-objects';
const zero = new PositiveInteger(0);
const items = new PositiveInteger(3);
items.subtract(1).valueOf(); // 2
items.subtract(4); // throws InvalidPositiveNumberError
items.divide(2); // throws InvalidIntegerErrorNotes
- Like
PositiveNumber, it accepts0. It is not anIntegersubclass:instanceof Integerisfalse. - The sign is checked first, so
-0.5throwsInvalidPositiveNumberError, notInvalidIntegerError. - Arithmetic is inherited from
NumberValueObjectand goes through the constructor, so a result that is negative or not an integer throws.