-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
integer.ts
54 lines (51 loc) · 1.69 KB
/
integer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { IntegerArbitrary } from './_internals/IntegerArbitrary';
/**
* Constraints to be applied on {@link integer}
* @remarks Since 2.6.0
* @public
*/
export interface IntegerConstraints {
/**
* Lower bound for the generated integers (included)
* @defaultValue -0x80000000
* @remarks Since 2.6.0
*/
min?: number;
/**
* Upper bound for the generated integers (included)
* @defaultValue 0x7fffffff
* @remarks Since 2.6.0
*/
max?: number;
}
/**
* Build fully set IntegerConstraints from a partial data
* @internal
*/
function buildCompleteIntegerConstraints(constraints: IntegerConstraints): Required<IntegerConstraints> {
const min = constraints.min !== undefined ? constraints.min : -0x80000000;
const max = constraints.max !== undefined ? constraints.max : 0x7fffffff;
return { min, max };
}
/**
* For integers between min (included) and max (included)
*
* @param constraints - Constraints to apply when building instances (since 2.6.0)
*
* @remarks Since 0.0.1
* @public
*/
export function integer(constraints: IntegerConstraints = {}): Arbitrary<number> {
const fullConstraints = buildCompleteIntegerConstraints(constraints);
if (fullConstraints.min > fullConstraints.max) {
throw new Error('fc.integer maximum value should be equal or greater than the minimum one');
}
if (!Number.isInteger(fullConstraints.min)) {
throw new Error('fc.integer minimum value should be an integer');
}
if (!Number.isInteger(fullConstraints.max)) {
throw new Error('fc.integer maximum value should be an integer');
}
return new IntegerArbitrary(fullConstraints.min, fullConstraints.max);
}