@guillaume-docquier/tools-ts 2.3.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Range.d.ts +22 -8
- package/dist/Range.js +29 -6
- package/dist/Range.js.map +1 -1
- package/package.json +1 -1
package/dist/Range.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Result } from "./Result.js";
|
|
2
2
|
/**
|
|
3
3
|
* A finite numeric interval with an inclusive minimum and either an inclusive or exclusive maximum.
|
|
4
|
-
* Use
|
|
4
|
+
* Use the Range factory methods to create valid ranges.
|
|
5
5
|
*/
|
|
6
6
|
export type Range<TNumericType extends NumericType = NumericType> = InclusiveRange<TNumericType> | ExclusiveRange<TNumericType>;
|
|
7
7
|
/**
|
|
8
8
|
* A finite numeric interval whose maximum value is included in the range.
|
|
9
|
-
* Use the {@link Range.createMaxInclusive} factory
|
|
9
|
+
* Use the {@link Range.safeCreateMaxInclusive} or {@link Range.createMaxInclusive} factory methods to create valid InclusiveRanges.
|
|
10
10
|
*/
|
|
11
11
|
export type InclusiveRange<TNumericType extends NumericType> = {
|
|
12
12
|
/**
|
|
@@ -28,11 +28,11 @@ export type InclusiveRange<TNumericType extends NumericType> = {
|
|
|
28
28
|
/**
|
|
29
29
|
* Optional outer range that this range must stay within.
|
|
30
30
|
*/
|
|
31
|
-
readonly limits?: Range<TNumericType
|
|
31
|
+
readonly limits?: Range<TNumericType> | undefined;
|
|
32
32
|
};
|
|
33
33
|
/**
|
|
34
34
|
* A finite numeric interval whose maximum value is excluded from the range.
|
|
35
|
-
* Use the {@link Range.createMaxExclusive} factory
|
|
35
|
+
* Use the {@link Range.safeCreateMaxExclusive} or {@link Range.createMaxExclusive} factory methods to create valid ExclusiveRanges.
|
|
36
36
|
*/
|
|
37
37
|
export type ExclusiveRange<TNumericType extends NumericType> = {
|
|
38
38
|
/**
|
|
@@ -54,27 +54,41 @@ export type ExclusiveRange<TNumericType extends NumericType> = {
|
|
|
54
54
|
/**
|
|
55
55
|
* Optional outer range that this range must stay within.
|
|
56
56
|
*/
|
|
57
|
-
readonly limits?: Range<TNumericType
|
|
57
|
+
readonly limits?: Range<TNumericType> | undefined;
|
|
58
58
|
};
|
|
59
59
|
type NumericType = "float" | "integer";
|
|
60
60
|
/**
|
|
61
61
|
* Helpers for creating, validating, comparing, and narrowing numeric ranges.
|
|
62
62
|
*/
|
|
63
63
|
export declare const Range: {
|
|
64
|
+
/**
|
|
65
|
+
* Creates a range that includes its maximum value.
|
|
66
|
+
* The function throws if the Range parameters are invalid. You should use this for trusted code only.
|
|
67
|
+
*
|
|
68
|
+
* @param data The range values, excluding the discriminator added by this function.
|
|
69
|
+
*/
|
|
70
|
+
createMaxInclusive: <TNumericType extends NumericType>(data: Omit<InclusiveRange<TNumericType>, "type">) => InclusiveRange<TNumericType>;
|
|
64
71
|
/**
|
|
65
72
|
* Creates a range that includes its maximum value.
|
|
66
73
|
* The function returns a Failure if the Range parameters are invalid.
|
|
67
74
|
*
|
|
68
|
-
* @param
|
|
75
|
+
* @param data The range values, excluding the discriminator added by this function.
|
|
76
|
+
*/
|
|
77
|
+
safeCreateMaxInclusive: <TNumericType extends NumericType>(data: Omit<InclusiveRange<TNumericType>, "type">) => Result<InclusiveRange<TNumericType>, string>;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a range that excludes its maximum value.
|
|
80
|
+
* The function throws if the Range parameters are invalid. You should use this for trusted code only.
|
|
81
|
+
*
|
|
82
|
+
* @param data The range values, excluding the discriminator added by this function.
|
|
69
83
|
*/
|
|
70
|
-
|
|
84
|
+
createMaxExclusive: <TNumericType extends NumericType>(data: Omit<ExclusiveRange<TNumericType>, "type">) => ExclusiveRange<TNumericType>;
|
|
71
85
|
/**
|
|
72
86
|
* Creates a range that excludes its maximum value.
|
|
73
87
|
* The function returns a Failure if the Range parameters are invalid.
|
|
74
88
|
*
|
|
75
89
|
* @param range The range values, excluding the discriminator added by this function.
|
|
76
90
|
*/
|
|
77
|
-
|
|
91
|
+
safeCreateMaxExclusive: <TNumericType extends NumericType>(range: Omit<ExclusiveRange<TNumericType>, "type">) => Result<ExclusiveRange<TNumericType>, string>;
|
|
78
92
|
/**
|
|
79
93
|
* Creates a new range with the same discriminator, numeric type, and limits as another range.
|
|
80
94
|
*
|
package/dist/Range.js
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
import { Result } from "./Result.js";
|
|
2
|
+
import { Assert } from "./Assert.js";
|
|
2
3
|
/**
|
|
3
4
|
* Helpers for creating, validating, comparing, and narrowing numeric ranges.
|
|
4
5
|
*/
|
|
5
6
|
export const Range = {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a range that includes its maximum value.
|
|
9
|
+
* The function throws if the Range parameters are invalid. You should use this for trusted code only.
|
|
10
|
+
*
|
|
11
|
+
* @param data The range values, excluding the discriminator added by this function.
|
|
12
|
+
*/
|
|
13
|
+
createMaxInclusive: (data) => {
|
|
14
|
+
const rangeResult = Range.safeCreateMaxInclusive(data);
|
|
15
|
+
Assert.isSuccess(rangeResult);
|
|
16
|
+
return rangeResult.value;
|
|
17
|
+
},
|
|
6
18
|
/**
|
|
7
19
|
* Creates a range that includes its maximum value.
|
|
8
20
|
* The function returns a Failure if the Range parameters are invalid.
|
|
9
21
|
*
|
|
10
|
-
* @param
|
|
22
|
+
* @param data The range values, excluding the discriminator added by this function.
|
|
11
23
|
*/
|
|
12
|
-
|
|
24
|
+
safeCreateMaxInclusive: (data) => {
|
|
13
25
|
const inclusiveRange = {
|
|
14
|
-
...
|
|
26
|
+
...data,
|
|
15
27
|
type: "MaxInclusive",
|
|
16
28
|
};
|
|
17
29
|
const invalidReason = Range.validate(inclusiveRange);
|
|
@@ -20,13 +32,24 @@ export const Range = {
|
|
|
20
32
|
}
|
|
21
33
|
return Result.Success(inclusiveRange);
|
|
22
34
|
},
|
|
35
|
+
/**
|
|
36
|
+
* Creates a range that excludes its maximum value.
|
|
37
|
+
* The function throws if the Range parameters are invalid. You should use this for trusted code only.
|
|
38
|
+
*
|
|
39
|
+
* @param data The range values, excluding the discriminator added by this function.
|
|
40
|
+
*/
|
|
41
|
+
createMaxExclusive: (data) => {
|
|
42
|
+
const rangeResult = Range.safeCreateMaxExclusive(data);
|
|
43
|
+
Assert.isSuccess(rangeResult);
|
|
44
|
+
return rangeResult.value;
|
|
45
|
+
},
|
|
23
46
|
/**
|
|
24
47
|
* Creates a range that excludes its maximum value.
|
|
25
48
|
* The function returns a Failure if the Range parameters are invalid.
|
|
26
49
|
*
|
|
27
50
|
* @param range The range values, excluding the discriminator added by this function.
|
|
28
51
|
*/
|
|
29
|
-
|
|
52
|
+
safeCreateMaxExclusive: (range) => {
|
|
30
53
|
const exclusiveRange = {
|
|
31
54
|
...range,
|
|
32
55
|
type: "MaxExclusive",
|
|
@@ -46,14 +69,14 @@ export const Range = {
|
|
|
46
69
|
from: (fromRange, withValues) => {
|
|
47
70
|
switch (fromRange.type) {
|
|
48
71
|
case "MaxExclusive":
|
|
49
|
-
return Range.
|
|
72
|
+
return Range.safeCreateMaxExclusive({
|
|
50
73
|
numericType: fromRange.numericType,
|
|
51
74
|
min: withValues.min,
|
|
52
75
|
maxExclusive: withValues.max,
|
|
53
76
|
limits: fromRange.limits,
|
|
54
77
|
});
|
|
55
78
|
case "MaxInclusive":
|
|
56
|
-
return Range.
|
|
79
|
+
return Range.safeCreateMaxInclusive({
|
|
57
80
|
numericType: fromRange.numericType,
|
|
58
81
|
min: withValues.min,
|
|
59
82
|
maxInclusive: withValues.max,
|
package/dist/Range.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Range.js","sourceRoot":"","sources":["../src/Range.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAwEpC;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB;;;;;OAKG;IACH,kBAAkB,EAAE,CAClB,
|
|
1
|
+
{"version":3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