@lucaspaganini/value-objects
Version:
TypeScript first validation and class creation library
66 lines (65 loc) • 2.04 kB
TypeScript
export interface VOIntegerOptions {
/**
* Minimum inclusive acceptable value.
* Can't be bigger than `max`.
*/
min?: number;
/**
* Maximum inclusive acceptable value.
* Can't be smaller than `min`.
*/
max?: number;
}
export interface VOIntegerInstance {
valueOf(): number;
}
export interface VOIntegerConstructor {
new (r: number): VOIntegerInstance;
}
/**
* Function to create an integer number value object constructor.
*
* > NOTE: If you want to accept floating point numbers and convert them to
* integers, you can use {@link VOFloat} and set the precision option to 0.
*
* @param options Customizations for the returned class constructor
* @return Class constructor that accepts a (integer) number for instantiation
* and returns that number when {@link VOIntegerInstance.valueOf} is called.
*
* @example
* ```typescript
* class MyInteger extends VOInteger() {}
*
* const int1 = new MyInteger(5); // OK
* int1.valueOf(); // 5
*
* const int2 = new MyInteger(5.0); // OK
* int2.valueOf(); // 5
*
* const int3 = new MyInteger(5.5); // Runtime error: Not an integer
* ```
*
* @example
* ```typescript
* class NaturalNumber extends VOInteger({ min: 0 }) {} // OK
* new NaturalNumber(0); // OK
* new NaturalNumber(1000000); // Ok
* new NaturalNumber(-1); // Runtime error: Too small
* new NaturalNumber(1.5); // Runtime error: Not an integer
* ```
*
* @example
* ```typescript
* class MyFloatRangeInteger extends VOInteger({ min: -100.5, max: 100.5 }) {} // OK
* new MyFloatRangeInteger(-100); // OK
* new MyFloatRangeInteger(100); // Ok
* new MyFloatRangeInteger(-101); // Runtime error: Too small
* new MyFloatRangeInteger(101); // Runtime error: Too big
* ```
*
* @example
* ```typescript
* class MyInvalidInteger extends VOInteger({ min: 100, max: -100 }) {} // Runtime error: Invalid logic (options.min should not be bigger than options.max)
* ```
*/
export declare const VOInteger: (options?: VOIntegerOptions) => VOIntegerConstructor;