ts-data-forge 11.0.0 → 12.0.0
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/README.md +2 -2
- package/dist/number/branded-types/finite-number.d.mts +47 -61
- package/dist/number/branded-types/finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/finite-number.mjs +37 -41
- package/dist/number/branded-types/finite-number.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +40 -46
- package/dist/number/branded-types/int.d.mts.map +1 -1
- package/dist/number/branded-types/int.mjs +29 -31
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/int16.d.mts +71 -59
- package/dist/number/branded-types/int16.d.mts.map +1 -1
- package/dist/number/branded-types/int16.mjs +56 -47
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.d.mts +71 -59
- package/dist/number/branded-types/int32.d.mts.map +1 -1
- package/dist/number/branded-types/int32.mjs +56 -47
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/negative-finite-number.d.mts +76 -60
- package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/negative-finite-number.mjs +62 -49
- package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/negative-int.d.mts +66 -49
- package/dist/number/branded-types/negative-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-int.mjs +48 -39
- package/dist/number/branded-types/negative-int.mjs.map +1 -1
- package/dist/number/branded-types/negative-safe-int.d.mts +64 -57
- package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-safe-int.mjs +49 -44
- package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +80 -65
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.mjs +65 -53
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +71 -58
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int16.mjs +55 -45
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +71 -59
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int32.mjs +55 -46
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.d.mts +70 -55
- package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.mjs +55 -43
- package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-int.d.mts +46 -146
- package/dist/number/branded-types/non-positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-int.mjs +33 -108
- package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.d.mts +62 -54
- package/dist/number/branded-types/non-positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.mjs +46 -41
- package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.d.mts +62 -49
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.mjs +46 -37
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int.d.mts +51 -38
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int.mjs +31 -27
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int16.d.mts +59 -54
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int16.mjs +44 -41
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.d.mts +59 -55
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int32.mjs +44 -42
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.d.mts +60 -55
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs +44 -42
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.d.mts +59 -54
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.mjs +43 -41
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.d.mts +59 -54
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.mjs +43 -41
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +82 -64
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/positive-finite-number.mjs +67 -52
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +53 -70
- package/dist/number/branded-types/positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int.mjs +42 -52
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +68 -58
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int16.mjs +52 -45
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +68 -58
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int32.mjs +52 -45
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +72 -61
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs +56 -48
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +68 -58
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint16.mjs +52 -45
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +68 -59
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint32.mjs +52 -46
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +52 -66
- package/dist/number/branded-types/safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/safe-int.mjs +45 -47
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +68 -58
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
- package/dist/number/branded-types/safe-uint.mjs +53 -46
- package/dist/number/branded-types/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.d.mts +72 -55
- package/dist/number/branded-types/uint.d.mts.map +1 -1
- package/dist/number/branded-types/uint.mjs +52 -45
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.d.mts +66 -59
- package/dist/number/branded-types/uint16.d.mts.map +1 -1
- package/dist/number/branded-types/uint16.mjs +51 -45
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.d.mts +66 -57
- package/dist/number/branded-types/uint32.d.mts.map +1 -1
- package/dist/number/branded-types/uint32.mjs +50 -45
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.d.mts +62 -66
- package/dist/number/enum/int8.d.mts.map +1 -1
- package/dist/number/enum/int8.mjs +57 -51
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.d.mts +66 -59
- package/dist/number/enum/uint8.d.mts.map +1 -1
- package/dist/number/enum/uint8.mjs +57 -51
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/number/refined-number-utils.d.mts +9 -9
- package/dist/number/refined-number-utils.d.mts.map +1 -1
- package/dist/number/refined-number-utils.mjs +7 -7
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/package.json +2 -1
- package/src/number/branded-types/finite-number.mts +47 -61
- package/src/number/branded-types/int.mts +40 -46
- package/src/number/branded-types/int16.mts +72 -60
- package/src/number/branded-types/int32.mts +72 -60
- package/src/number/branded-types/negative-finite-number.mts +78 -71
- package/src/number/branded-types/negative-finite-number.test.mts +4 -4
- package/src/number/branded-types/negative-int.mts +68 -60
- package/src/number/branded-types/negative-int.test.mts +5 -5
- package/src/number/branded-types/negative-safe-int.mts +66 -68
- package/src/number/branded-types/negative-safe-int.test.mts +4 -4
- package/src/number/branded-types/non-negative-finite-number.mts +81 -66
- package/src/number/branded-types/non-negative-int16.mts +72 -59
- package/src/number/branded-types/non-negative-int32.mts +72 -60
- package/src/number/branded-types/non-positive-finite-number.mts +71 -56
- package/src/number/branded-types/non-positive-finite-number.test.mts +7 -7
- package/src/number/branded-types/non-positive-int.mts +47 -147
- package/src/number/branded-types/non-positive-int.test.mts +7 -7
- package/src/number/branded-types/non-positive-safe-int.mts +63 -55
- package/src/number/branded-types/non-positive-safe-int.test.mts +7 -7
- package/src/number/branded-types/non-zero-finite-number.mts +62 -51
- package/src/number/branded-types/non-zero-int.mts +51 -38
- package/src/number/branded-types/non-zero-int16.mts +60 -55
- package/src/number/branded-types/non-zero-int32.mts +60 -56
- package/src/number/branded-types/non-zero-safe-int.mts +61 -56
- package/src/number/branded-types/non-zero-uint16.mts +60 -55
- package/src/number/branded-types/non-zero-uint32.mts +60 -55
- package/src/number/branded-types/positive-finite-number.mts +83 -65
- package/src/number/branded-types/positive-int.mts +54 -71
- package/src/number/branded-types/positive-int16.mts +69 -59
- package/src/number/branded-types/positive-int32.mts +69 -59
- package/src/number/branded-types/positive-safe-int.mts +73 -62
- package/src/number/branded-types/positive-uint16.mts +69 -59
- package/src/number/branded-types/positive-uint32.mts +69 -60
- package/src/number/branded-types/safe-int.mts +53 -67
- package/src/number/branded-types/safe-uint.mts +69 -59
- package/src/number/branded-types/uint.mts +73 -56
- package/src/number/branded-types/uint16.mts +67 -60
- package/src/number/branded-types/uint32.mts +67 -58
- package/src/number/enum/int8.mts +70 -73
- package/src/number/enum/int8.test.mts +7 -4
- package/src/number/enum/uint8.mts +74 -66
- package/src/number/enum/uint8.test.mts +7 -4
- package/src/number/refined-number-utils.mts +22 -19
|
@@ -3,8 +3,8 @@ export type PositiveInt = TtfImported_PositiveInt;
|
|
|
3
3
|
/**
|
|
4
4
|
* Type guard that checks if a value is a positive integer.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* Returns `true` for a positive integer — a value with no fractional component,
|
|
7
|
+
* including values outside the safe integer range (unlike `SafeInt`).
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
*
|
|
@@ -21,11 +21,10 @@ export type PositiveInt = TtfImported_PositiveInt;
|
|
|
21
21
|
*/
|
|
22
22
|
export declare const isPositiveInt: (a: number) => a is TtfImported_PositiveInt;
|
|
23
23
|
/**
|
|
24
|
-
* Casts a number to
|
|
24
|
+
* Casts a `number` to the `PositiveInt` branded type.
|
|
25
25
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* operations that require strictly positive integer values.
|
|
26
|
+
* Validates that the value is a positive integer and returns it with the
|
|
27
|
+
* `PositiveInt` brand. Throws a `TypeError` otherwise.
|
|
29
28
|
*
|
|
30
29
|
* @example
|
|
31
30
|
*
|
|
@@ -38,7 +37,7 @@ export declare const isPositiveInt: (a: number) => a is TtfImported_PositiveInt;
|
|
|
38
37
|
* ```
|
|
39
38
|
*
|
|
40
39
|
* @param value - The value to cast
|
|
41
|
-
* @returns The value as a PositiveInt
|
|
40
|
+
* @returns The value as a `PositiveInt`
|
|
42
41
|
* @throws {TypeError} If the value is not a positive integer
|
|
43
42
|
*/
|
|
44
43
|
export declare const asPositiveInt: <N extends number>(x: N) => number & {
|
|
@@ -56,20 +55,14 @@ export declare const asPositiveInt: <N extends number>(x: N) => number & {
|
|
|
56
55
|
'TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3': unknown;
|
|
57
56
|
}> & N;
|
|
58
57
|
/**
|
|
59
|
-
* Namespace providing type-safe operations for PositiveInt branded
|
|
58
|
+
* Namespace providing type-safe operations for the `PositiveInt` branded type.
|
|
60
59
|
*
|
|
61
|
-
* PositiveInt represents
|
|
62
|
-
*
|
|
63
|
-
* constraint, ensuring that arithmetic operations never produce zero or
|
|
64
|
-
* negative values.
|
|
60
|
+
* The `PositiveInt` type represents a positive integer. Division (`div`) uses
|
|
61
|
+
* floor division.
|
|
65
62
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* - Counts and quantities that must be positive
|
|
70
|
-
* - Denominators in division operations
|
|
71
|
-
* - Loop counters and iteration counts
|
|
72
|
-
* - Database primary keys and IDs
|
|
63
|
+
* Unlike `SafeInt`, `PositiveInt` allows values outside the safe integer range
|
|
64
|
+
* (±2^53 − 1), so very large magnitudes may lose precision in JavaScript's
|
|
65
|
+
* `number` type.
|
|
73
66
|
*/
|
|
74
67
|
export declare const PositiveInt: {
|
|
75
68
|
/**
|
|
@@ -91,16 +84,12 @@ export declare const PositiveInt: {
|
|
|
91
84
|
*/
|
|
92
85
|
readonly is: (a: number) => a is TtfImported_PositiveInt;
|
|
93
86
|
/**
|
|
94
|
-
* The
|
|
95
|
-
*
|
|
96
|
-
* @readonly
|
|
87
|
+
* The smallest value representable as `PositiveInt` (the lower saturation
|
|
88
|
+
* target of `fromNumber`).
|
|
97
89
|
*/
|
|
98
90
|
readonly MIN_VALUE: 1;
|
|
99
91
|
/**
|
|
100
|
-
* Returns the
|
|
101
|
-
*
|
|
102
|
-
* Since all inputs are guaranteed to be >= 1, the result is also guaranteed
|
|
103
|
-
* to be a positive integer.
|
|
92
|
+
* Returns the smallest of the given positive integers.
|
|
104
93
|
*
|
|
105
94
|
* @example
|
|
106
95
|
*
|
|
@@ -115,11 +104,11 @@ export declare const PositiveInt: {
|
|
|
115
104
|
* ```
|
|
116
105
|
*
|
|
117
106
|
* @param values - The positive integers to compare (at least one required)
|
|
118
|
-
* @returns The smallest value as a PositiveInt
|
|
107
|
+
* @returns The smallest value as a `PositiveInt`
|
|
119
108
|
*/
|
|
120
109
|
readonly min: (...values: readonly import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>[]) => TtfImported_PositiveInt;
|
|
121
110
|
/**
|
|
122
|
-
* Returns the
|
|
111
|
+
* Returns the largest of the given positive integers.
|
|
123
112
|
*
|
|
124
113
|
* @example
|
|
125
114
|
*
|
|
@@ -134,36 +123,37 @@ export declare const PositiveInt: {
|
|
|
134
123
|
* ```
|
|
135
124
|
*
|
|
136
125
|
* @param values - The positive integers to compare (at least one required)
|
|
137
|
-
* @returns The largest value as a PositiveInt
|
|
126
|
+
* @returns The largest value as a `PositiveInt`
|
|
138
127
|
*/
|
|
139
128
|
readonly max: (...values: readonly import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>[]) => TtfImported_PositiveInt;
|
|
140
129
|
/**
|
|
141
|
-
*
|
|
130
|
+
* Converts an arbitrary `number` into a `PositiveInt`, rounding to the
|
|
131
|
+
* nearest integer and saturating the result into the range `[MIN_VALUE,
|
|
132
|
+
* MAX_VALUE]`.
|
|
142
133
|
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
134
|
+
* Unlike `asPositiveInt`, this is total: out-of-range inputs are clamped to
|
|
135
|
+
* the nearest representable `PositiveInt` instead of throwing.
|
|
145
136
|
*
|
|
146
137
|
* @example
|
|
147
138
|
*
|
|
148
139
|
* ```ts
|
|
149
|
-
* const belowRange = PositiveInt.
|
|
140
|
+
* const belowRange = PositiveInt.fromNumber(0);
|
|
150
141
|
*
|
|
151
|
-
* const withinRange = PositiveInt.
|
|
142
|
+
* const withinRange = PositiveInt.fromNumber(10);
|
|
152
143
|
*
|
|
153
144
|
* assert.isTrue(belowRange === 1);
|
|
154
145
|
*
|
|
155
146
|
* assert.isTrue(withinRange === 10);
|
|
156
147
|
* ```
|
|
157
148
|
*
|
|
158
|
-
* @param value - The
|
|
159
|
-
* @returns The value
|
|
149
|
+
* @param value - The value to convert
|
|
150
|
+
* @returns The value as a `PositiveInt`
|
|
160
151
|
*/
|
|
161
|
-
readonly
|
|
152
|
+
readonly fromNumber: (x: number) => TtfImported_PositiveInt;
|
|
162
153
|
/**
|
|
163
|
-
* Generates a random
|
|
154
|
+
* Generates a random `PositiveInt` within the given range.
|
|
164
155
|
*
|
|
165
|
-
*
|
|
166
|
-
* If min > max, they are automatically swapped.
|
|
156
|
+
* The range is inclusive on both ends.
|
|
167
157
|
*
|
|
168
158
|
* @example
|
|
169
159
|
*
|
|
@@ -179,14 +169,14 @@ export declare const PositiveInt: {
|
|
|
179
169
|
* assert.isTrue(randomValue >= 3 && randomValue <= 6);
|
|
180
170
|
* ```
|
|
181
171
|
*
|
|
182
|
-
* @param min - The minimum value (inclusive
|
|
183
|
-
* @param max - The maximum value (inclusive
|
|
184
|
-
* @returns A random PositiveInt in
|
|
172
|
+
* @param min - The minimum value (inclusive)
|
|
173
|
+
* @param max - The maximum value (inclusive)
|
|
174
|
+
* @returns A random `PositiveInt` in `[min, max]`
|
|
185
175
|
*/
|
|
186
176
|
readonly random: (min?: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt> | undefined, max?: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt> | undefined) => TtfImported_PositiveInt;
|
|
187
177
|
/**
|
|
188
|
-
* Raises a
|
|
189
|
-
*
|
|
178
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `PositiveInt` (floored
|
|
179
|
+
* to an integer).
|
|
190
180
|
*
|
|
191
181
|
* @example
|
|
192
182
|
*
|
|
@@ -202,11 +192,11 @@ export declare const PositiveInt: {
|
|
|
202
192
|
*
|
|
203
193
|
* @param a - The base positive integer
|
|
204
194
|
* @param b - The exponent positive integer
|
|
205
|
-
* @returns `a ** b` as a PositiveInt
|
|
195
|
+
* @returns `a ** b` as a `PositiveInt`
|
|
206
196
|
*/
|
|
207
197
|
readonly pow: (x: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>) => TtfImported_PositiveInt;
|
|
208
198
|
/**
|
|
209
|
-
* Adds two positive integers,
|
|
199
|
+
* Adds two positive integers, returning `a + b` as a `PositiveInt`.
|
|
210
200
|
*
|
|
211
201
|
* @example
|
|
212
202
|
*
|
|
@@ -216,16 +206,13 @@ export declare const PositiveInt: {
|
|
|
216
206
|
* assert.isTrue(sum === 9);
|
|
217
207
|
* ```
|
|
218
208
|
*
|
|
219
|
-
* @param a -
|
|
220
|
-
* @param b -
|
|
221
|
-
* @returns `a
|
|
209
|
+
* @param a - The first positive integer
|
|
210
|
+
* @param b - The second positive integer
|
|
211
|
+
* @returns The sum of `a` and `b` as a `PositiveInt`
|
|
222
212
|
*/
|
|
223
213
|
readonly add: (x: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>) => TtfImported_PositiveInt;
|
|
224
214
|
/**
|
|
225
|
-
* Subtracts two positive integers,
|
|
226
|
-
*
|
|
227
|
-
* If the mathematical result would be <= 0, it is clamped to 1 to maintain
|
|
228
|
-
* the positive integer constraint.
|
|
215
|
+
* Subtracts two positive integers, returning `a - b` as a `PositiveInt`.
|
|
229
216
|
*
|
|
230
217
|
* @example
|
|
231
218
|
*
|
|
@@ -235,13 +222,13 @@ export declare const PositiveInt: {
|
|
|
235
222
|
* assert.isTrue(difference === 1);
|
|
236
223
|
* ```
|
|
237
224
|
*
|
|
238
|
-
* @param a - The
|
|
239
|
-
* @param b - The
|
|
240
|
-
* @returns `
|
|
225
|
+
* @param a - The first positive integer
|
|
226
|
+
* @param b - The second positive integer
|
|
227
|
+
* @returns The difference of `a` and `b` as a `PositiveInt`
|
|
241
228
|
*/
|
|
242
229
|
readonly sub: (x: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>) => TtfImported_PositiveInt;
|
|
243
230
|
/**
|
|
244
|
-
* Multiplies two positive integers,
|
|
231
|
+
* Multiplies two positive integers, returning `a * b` as a `PositiveInt`.
|
|
245
232
|
*
|
|
246
233
|
* @example
|
|
247
234
|
*
|
|
@@ -251,18 +238,14 @@ export declare const PositiveInt: {
|
|
|
251
238
|
* assert.isTrue(product === 21);
|
|
252
239
|
* ```
|
|
253
240
|
*
|
|
254
|
-
* @param a -
|
|
255
|
-
* @param b -
|
|
256
|
-
* @returns `a
|
|
241
|
+
* @param a - The first positive integer
|
|
242
|
+
* @param b - The second positive integer
|
|
243
|
+
* @returns The product of `a` and `b` as a `PositiveInt`
|
|
257
244
|
*/
|
|
258
245
|
readonly mul: (x: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>) => TtfImported_PositiveInt;
|
|
259
246
|
/**
|
|
260
|
-
* Divides two positive integers using floor division
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
* Performs mathematical floor division: `⌊a / b⌋`. If the result would be 0
|
|
264
|
-
* (when a < b), it is clamped to 1 to maintain the positive integer
|
|
265
|
-
* constraint.
|
|
247
|
+
* Divides two positive integers using floor division (`⌊a / b⌋`): the result
|
|
248
|
+
* is `a / b` rounded toward negative infinity, as a `PositiveInt`.
|
|
266
249
|
*
|
|
267
250
|
* @example
|
|
268
251
|
*
|
|
@@ -276,9 +259,9 @@ export declare const PositiveInt: {
|
|
|
276
259
|
* assert.isTrue(clamped === 1);
|
|
277
260
|
* ```
|
|
278
261
|
*
|
|
279
|
-
* @param a - The dividend
|
|
280
|
-
* @param b - The divisor (
|
|
281
|
-
* @returns
|
|
262
|
+
* @param a - The dividend
|
|
263
|
+
* @param b - The divisor (must be non-zero)
|
|
264
|
+
* @returns The floored quotient as a `PositiveInt`
|
|
282
265
|
*/
|
|
283
266
|
readonly div: (x: import("ts-type-forge").WithSmallInt<TtfImported_PositiveInt>, y: 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 | import("ts-type-forge").NormalizeBrandUnion<number & {
|
|
284
267
|
readonly "!=0": true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"positive-int.d.mts","sourceRoot":"","sources":["../../../src/number/branded-types/positive-int.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,IAAI,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAI5E,MAAM,MAAM,WAAW,GAAG,uBAAuB,CAAC;AA8BlD;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,aAAa,6CAAK,CAAC;AAEhC
|
|
1
|
+
{"version":3,"file":"positive-int.d.mts","sourceRoot":"","sources":["../../../src/number/branded-types/positive-int.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,IAAI,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAI5E,MAAM,MAAM,WAAW,GAAG,uBAAuB,CAAC;AA8BlD;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,aAAa,6CAAK,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;;;;MAAW,CAAC;AAEtC;;;;;;;;;GASG;AACH,eAAO,MAAM,WAAW;IACtB;;;;;;;;;;;;;;;;OAgBG;;IAGH;;;OAGG;;IAGH;;;;;;;;;;;;;;;;;OAiBG;;IAGH;;;;;;;;;;;;;;;;;OAiBG;;IAGH;;;;;;;;;;;;;;;;;;;;;;OAsBG;;IAGH;;;;;;;;;;;;;;;;;;;;;;OAsBG;;IAGH;;;;;;;;;;;;;;;;;;;OAmBG;;IAGH;;;;;;;;;;;;;;OAcG;;IAGH;;;;;;;;;;;;;;OAcG;;IAGH;;;;;;;;;;;;;;OAcG;;IAGH;;;;;;;;;;;;;;;;;;;OAmBG;;;;;;;;;;;;;;;CAEK,CAAC"}
|
|
@@ -5,8 +5,8 @@ const {
|
|
|
5
5
|
MIN_VALUE,
|
|
6
6
|
add,
|
|
7
7
|
castType,
|
|
8
|
-
clamp,
|
|
9
8
|
div,
|
|
9
|
+
fromNumber,
|
|
10
10
|
is,
|
|
11
11
|
max: max_,
|
|
12
12
|
min: min_,
|
|
@@ -42,16 +42,12 @@ const PositiveInt = {
|
|
|
42
42
|
*/
|
|
43
43
|
is,
|
|
44
44
|
/**
|
|
45
|
-
* The
|
|
46
|
-
*
|
|
47
|
-
* @readonly
|
|
45
|
+
* The smallest value representable as `PositiveInt` (the lower saturation
|
|
46
|
+
* target of `fromNumber`).
|
|
48
47
|
*/
|
|
49
48
|
MIN_VALUE,
|
|
50
49
|
/**
|
|
51
|
-
* Returns the
|
|
52
|
-
*
|
|
53
|
-
* Since all inputs are guaranteed to be >= 1, the result is also guaranteed
|
|
54
|
-
* to be a positive integer.
|
|
50
|
+
* Returns the smallest of the given positive integers.
|
|
55
51
|
*
|
|
56
52
|
* @example
|
|
57
53
|
*
|
|
@@ -66,11 +62,11 @@ const PositiveInt = {
|
|
|
66
62
|
* ```
|
|
67
63
|
*
|
|
68
64
|
* @param values - The positive integers to compare (at least one required)
|
|
69
|
-
* @returns The smallest value as a PositiveInt
|
|
65
|
+
* @returns The smallest value as a `PositiveInt`
|
|
70
66
|
*/
|
|
71
67
|
min: min_,
|
|
72
68
|
/**
|
|
73
|
-
* Returns the
|
|
69
|
+
* Returns the largest of the given positive integers.
|
|
74
70
|
*
|
|
75
71
|
* @example
|
|
76
72
|
*
|
|
@@ -85,36 +81,37 @@ const PositiveInt = {
|
|
|
85
81
|
* ```
|
|
86
82
|
*
|
|
87
83
|
* @param values - The positive integers to compare (at least one required)
|
|
88
|
-
* @returns The largest value as a PositiveInt
|
|
84
|
+
* @returns The largest value as a `PositiveInt`
|
|
89
85
|
*/
|
|
90
86
|
max: max_,
|
|
91
87
|
/**
|
|
92
|
-
*
|
|
88
|
+
* Converts an arbitrary `number` into a `PositiveInt`, rounding to the
|
|
89
|
+
* nearest integer and saturating the result into the range `[MIN_VALUE,
|
|
90
|
+
* MAX_VALUE]`.
|
|
93
91
|
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
92
|
+
* Unlike `asPositiveInt`, this is total: out-of-range inputs are clamped to
|
|
93
|
+
* the nearest representable `PositiveInt` instead of throwing.
|
|
96
94
|
*
|
|
97
95
|
* @example
|
|
98
96
|
*
|
|
99
97
|
* ```ts
|
|
100
|
-
* const belowRange = PositiveInt.
|
|
98
|
+
* const belowRange = PositiveInt.fromNumber(0);
|
|
101
99
|
*
|
|
102
|
-
* const withinRange = PositiveInt.
|
|
100
|
+
* const withinRange = PositiveInt.fromNumber(10);
|
|
103
101
|
*
|
|
104
102
|
* assert.isTrue(belowRange === 1);
|
|
105
103
|
*
|
|
106
104
|
* assert.isTrue(withinRange === 10);
|
|
107
105
|
* ```
|
|
108
106
|
*
|
|
109
|
-
* @param value - The
|
|
110
|
-
* @returns The value
|
|
107
|
+
* @param value - The value to convert
|
|
108
|
+
* @returns The value as a `PositiveInt`
|
|
111
109
|
*/
|
|
112
|
-
|
|
110
|
+
fromNumber,
|
|
113
111
|
/**
|
|
114
|
-
* Generates a random
|
|
112
|
+
* Generates a random `PositiveInt` within the given range.
|
|
115
113
|
*
|
|
116
|
-
*
|
|
117
|
-
* If min > max, they are automatically swapped.
|
|
114
|
+
* The range is inclusive on both ends.
|
|
118
115
|
*
|
|
119
116
|
* @example
|
|
120
117
|
*
|
|
@@ -130,14 +127,14 @@ const PositiveInt = {
|
|
|
130
127
|
* assert.isTrue(randomValue >= 3 && randomValue <= 6);
|
|
131
128
|
* ```
|
|
132
129
|
*
|
|
133
|
-
* @param min - The minimum value (inclusive
|
|
134
|
-
* @param max - The maximum value (inclusive
|
|
135
|
-
* @returns A random PositiveInt in
|
|
130
|
+
* @param min - The minimum value (inclusive)
|
|
131
|
+
* @param max - The maximum value (inclusive)
|
|
132
|
+
* @returns A random `PositiveInt` in `[min, max]`
|
|
136
133
|
*/
|
|
137
134
|
random,
|
|
138
135
|
/**
|
|
139
|
-
* Raises a
|
|
140
|
-
*
|
|
136
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `PositiveInt` (floored
|
|
137
|
+
* to an integer).
|
|
141
138
|
*
|
|
142
139
|
* @example
|
|
143
140
|
*
|
|
@@ -153,11 +150,11 @@ const PositiveInt = {
|
|
|
153
150
|
*
|
|
154
151
|
* @param a - The base positive integer
|
|
155
152
|
* @param b - The exponent positive integer
|
|
156
|
-
* @returns `a ** b` as a PositiveInt
|
|
153
|
+
* @returns `a ** b` as a `PositiveInt`
|
|
157
154
|
*/
|
|
158
155
|
pow,
|
|
159
156
|
/**
|
|
160
|
-
* Adds two positive integers,
|
|
157
|
+
* Adds two positive integers, returning `a + b` as a `PositiveInt`.
|
|
161
158
|
*
|
|
162
159
|
* @example
|
|
163
160
|
*
|
|
@@ -167,16 +164,13 @@ const PositiveInt = {
|
|
|
167
164
|
* assert.isTrue(sum === 9);
|
|
168
165
|
* ```
|
|
169
166
|
*
|
|
170
|
-
* @param a -
|
|
171
|
-
* @param b -
|
|
172
|
-
* @returns `a
|
|
167
|
+
* @param a - The first positive integer
|
|
168
|
+
* @param b - The second positive integer
|
|
169
|
+
* @returns The sum of `a` and `b` as a `PositiveInt`
|
|
173
170
|
*/
|
|
174
171
|
add,
|
|
175
172
|
/**
|
|
176
|
-
* Subtracts two positive integers,
|
|
177
|
-
*
|
|
178
|
-
* If the mathematical result would be <= 0, it is clamped to 1 to maintain
|
|
179
|
-
* the positive integer constraint.
|
|
173
|
+
* Subtracts two positive integers, returning `a - b` as a `PositiveInt`.
|
|
180
174
|
*
|
|
181
175
|
* @example
|
|
182
176
|
*
|
|
@@ -186,13 +180,13 @@ const PositiveInt = {
|
|
|
186
180
|
* assert.isTrue(difference === 1);
|
|
187
181
|
* ```
|
|
188
182
|
*
|
|
189
|
-
* @param a - The
|
|
190
|
-
* @param b - The
|
|
191
|
-
* @returns `
|
|
183
|
+
* @param a - The first positive integer
|
|
184
|
+
* @param b - The second positive integer
|
|
185
|
+
* @returns The difference of `a` and `b` as a `PositiveInt`
|
|
192
186
|
*/
|
|
193
187
|
sub,
|
|
194
188
|
/**
|
|
195
|
-
* Multiplies two positive integers,
|
|
189
|
+
* Multiplies two positive integers, returning `a * b` as a `PositiveInt`.
|
|
196
190
|
*
|
|
197
191
|
* @example
|
|
198
192
|
*
|
|
@@ -202,18 +196,14 @@ const PositiveInt = {
|
|
|
202
196
|
* assert.isTrue(product === 21);
|
|
203
197
|
* ```
|
|
204
198
|
*
|
|
205
|
-
* @param a -
|
|
206
|
-
* @param b -
|
|
207
|
-
* @returns `a
|
|
199
|
+
* @param a - The first positive integer
|
|
200
|
+
* @param b - The second positive integer
|
|
201
|
+
* @returns The product of `a` and `b` as a `PositiveInt`
|
|
208
202
|
*/
|
|
209
203
|
mul,
|
|
210
204
|
/**
|
|
211
|
-
* Divides two positive integers using floor division
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
* Performs mathematical floor division: `⌊a / b⌋`. If the result would be 0
|
|
215
|
-
* (when a < b), it is clamped to 1 to maintain the positive integer
|
|
216
|
-
* constraint.
|
|
205
|
+
* Divides two positive integers using floor division (`⌊a / b⌋`): the result
|
|
206
|
+
* is `a / b` rounded toward negative infinity, as a `PositiveInt`.
|
|
217
207
|
*
|
|
218
208
|
* @example
|
|
219
209
|
*
|
|
@@ -227,9 +217,9 @@ const PositiveInt = {
|
|
|
227
217
|
* assert.isTrue(clamped === 1);
|
|
228
218
|
* ```
|
|
229
219
|
*
|
|
230
|
-
* @param a - The dividend
|
|
231
|
-
* @param b - The divisor (
|
|
232
|
-
* @returns
|
|
220
|
+
* @param a - The dividend
|
|
221
|
+
* @param b - The divisor (must be non-zero)
|
|
222
|
+
* @returns The floored quotient as a `PositiveInt`
|
|
233
223
|
*/
|
|
234
224
|
div
|
|
235
225
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"positive-int.mjs","sources":["../../../src/number/branded-types/positive-int.mts"],"sourcesContent":["import { type PositiveInt as TtfImported_PositiveInt } from 'ts-type-forge';\nimport { expectType } from '../../expect-type.mjs';\nimport { TsDataForgeInternals } from '../refined-number-utils.mjs';\n\nexport type PositiveInt = TtfImported_PositiveInt;\n\ntype ElementType = PositiveInt;\n\nconst typeNameInMessage = 'a positive integer';\n\nconst {\n MIN_VALUE,\n add,\n castType,\n clamp,\n div,\n is,\n max: max_,\n min: min_,\n mul,\n pow,\n random,\n sub,\n} = TsDataForgeInternals.RefinedNumberUtils.operatorsForInteger<\n ElementType,\n 1,\n number\n>({\n integerOrSafeInteger: 'Integer',\n MIN_VALUE: 1,\n MAX_VALUE: Number.MAX_VALUE,\n typeNameInMessage,\n} as const);\n\n/**\n * Type guard that checks if a value is a positive integer.\n *\n * A positive integer is any integer greater than zero (>= 1). This excludes\n * zero, negative numbers, and non-integers.\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isPositiveInt(5));\n *\n * assert.isFalse(isPositiveInt(0));\n *\n * assert.isTrue(PositiveInt.is(10));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a positive integer, `false` otherwise\n */\nexport const isPositiveInt = is;\n\n/**\n * Casts a number to a PositiveInt branded type.\n *\n * This function validates that the input is a positive integer (>= 1) and\n * returns it with the PositiveInt brand. This ensures type safety for\n * operations that require strictly positive integer values.\n *\n * @example\n *\n * ```ts\n * const branded = asPositiveInt(7);\n *\n * assert.isTrue(branded === 7);\n *\n * assert.isTrue(PositiveInt.is(branded));\n * ```\n *\n * @param value - The value to cast\n * @returns The value as a PositiveInt branded type\n * @throws {TypeError} If the value is not a positive integer\n */\nexport const asPositiveInt = castType;\n\n/**\n * Namespace providing type-safe operations for PositiveInt branded types.\n *\n * PositiveInt represents integers that are strictly greater than zero (>= 1).\n * All operations automatically clamp results to maintain the positive\n * constraint, ensuring that arithmetic operations never produce zero or\n * negative values.\n *\n * This type is essential for:\n *\n * - Array lengths and sizes (length >= 1)\n * - Counts and quantities that must be positive\n * - Denominators in division operations\n * - Loop counters and iteration counts\n * - Database primary keys and IDs\n */\nexport const PositiveInt = {\n /**\n * Type guard that checks if a value is a positive integer.\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isPositiveInt(5));\n *\n * assert.isFalse(isPositiveInt(0));\n *\n * assert.isTrue(PositiveInt.is(10));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a positive integer, `false` otherwise\n * @see {@link isPositiveInt} for usage examples\n */\n is,\n\n /**\n * The minimum value for a PositiveInt.\n *\n * @readonly\n */\n MIN_VALUE,\n\n /**\n * Returns the minimum value from a list of positive integers.\n *\n * Since all inputs are guaranteed to be >= 1, the result is also guaranteed\n * to be a positive integer.\n *\n * @example\n *\n * ```ts\n * const smallest = PositiveInt.min(\n * asPositiveInt(9),\n * asPositiveInt(3),\n * asPositiveInt(12),\n * );\n *\n * assert.isTrue(smallest === 3);\n * ```\n *\n * @param values - The positive integers to compare (at least one required)\n * @returns The smallest value as a PositiveInt\n */\n min: min_,\n\n /**\n * Returns the maximum value from a list of positive integers.\n *\n * @example\n *\n * ```ts\n * const largest = PositiveInt.max(\n * asPositiveInt(9),\n * asPositiveInt(3),\n * asPositiveInt(12),\n * );\n *\n * assert.isTrue(largest === 12);\n * ```\n *\n * @param values - The positive integers to compare (at least one required)\n * @returns The largest value as a PositiveInt\n */\n max: max_,\n\n /**\n * Clamps a number to the positive integer range.\n *\n * Since PositiveInt has a minimum value of 1, this function ensures that any\n * input less than 1 is clamped to 1.\n *\n * @example\n *\n * ```ts\n * const belowRange = PositiveInt.clamp(0);\n *\n * const withinRange = PositiveInt.clamp(10);\n *\n * assert.isTrue(belowRange === 1);\n *\n * assert.isTrue(withinRange === 10);\n * ```\n *\n * @param value - The number to clamp\n * @returns The value clamped to >= 1 as a PositiveInt\n */\n clamp,\n\n /**\n * Generates a random positive integer within the specified range (inclusive).\n *\n * Both bounds are inclusive, and both min and max must be positive integers.\n * If min > max, they are automatically swapped.\n *\n * @example\n *\n * ```ts\n * const min = asPositiveInt(3);\n *\n * const max = asPositiveInt(6);\n *\n * const randomValue = PositiveInt.random(min, max);\n *\n * assert.isTrue(PositiveInt.is(randomValue));\n *\n * assert.isTrue(randomValue >= 3 && randomValue <= 6);\n * ```\n *\n * @param min - The minimum value (inclusive, must be >= 1)\n * @param max - The maximum value (inclusive, must be >= min)\n * @returns A random PositiveInt in the range [min, max]\n */\n random,\n\n /**\n * Raises a positive integer to a power, ensuring the result is never less\n * than 1.\n *\n * @example\n *\n * ```ts\n * const base = asPositiveInt(2);\n *\n * const exponent = asPositiveInt(4);\n *\n * const power = PositiveInt.pow(base, exponent);\n *\n * assert.isTrue(power === 16);\n * ```\n *\n * @param a - The base positive integer\n * @param b - The exponent positive integer\n * @returns `a ** b` as a PositiveInt, but never less than 1\n */\n pow,\n\n /**\n * Adds two positive integers, ensuring the result is never less than 1.\n *\n * @example\n *\n * ```ts\n * const sum = PositiveInt.add(asPositiveInt(4), asPositiveInt(5));\n *\n * assert.isTrue(sum === 9);\n * ```\n *\n * @param a - First positive integer\n * @param b - Second positive integer\n * @returns `a + b` as a PositiveInt, but never less than 1\n */\n add,\n\n /**\n * Subtracts two positive integers, clamping the result to remain positive.\n *\n * If the mathematical result would be <= 0, it is clamped to 1 to maintain\n * the positive integer constraint.\n *\n * @example\n *\n * ```ts\n * const difference = PositiveInt.sub(asPositiveInt(5), asPositiveInt(7));\n *\n * assert.isTrue(difference === 1);\n * ```\n *\n * @param a - The minuend (positive integer)\n * @param b - The subtrahend (positive integer)\n * @returns `max(1, a - b)` as a PositiveInt\n */\n sub,\n\n /**\n * Multiplies two positive integers, ensuring the result is never less than 1.\n *\n * @example\n *\n * ```ts\n * const product = PositiveInt.mul(asPositiveInt(3), asPositiveInt(7));\n *\n * assert.isTrue(product === 21);\n * ```\n *\n * @param a - First positive integer\n * @param b - Second positive integer\n * @returns `a * b` as a PositiveInt, but never less than 1\n */\n mul,\n\n /**\n * Divides two positive integers using floor division, clamping to remain\n * positive.\n *\n * Performs mathematical floor division: `⌊a / b⌋`. If the result would be 0\n * (when a < b), it is clamped to 1 to maintain the positive integer\n * constraint.\n *\n * @example\n *\n * ```ts\n * const quotient = PositiveInt.div(asPositiveInt(9), asPositiveInt(2));\n *\n * const clamped = PositiveInt.div(asPositiveInt(3), asPositiveInt(10));\n *\n * assert.isTrue(quotient === 4);\n *\n * assert.isTrue(clamped === 1);\n * ```\n *\n * @param a - The dividend (positive integer)\n * @param b - The divisor (positive integer, guaranteed non-zero)\n * @returns `max(1, ⌊a / b⌋)` as a PositiveInt\n */\n div,\n} as const;\n\nexpectType<\n keyof typeof PositiveInt,\n keyof TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'positive'\n >\n>('=');\n\nexpectType<\n typeof PositiveInt,\n TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'positive'\n >\n>('<=');\n"],"names":[],"mappings":";;AAQA,MAAM,iBAAA,GAAoB,oBAAA;AAE1B,MAAM;AAAA,EACJ,SAAA;AAAA,EACA,GAAA;AAAA,EACA,QAAA;AAAA,EACA,KAAA;AAAA,EACA,GAAA;AAAA,EACA,EAAA;AAAA,EACA,GAAA,EAAK,IAAA;AAAA,EACL,GAAA,EAAK,IAAA;AAAA,EACL,GAAA;AAAA,EACA,GAAA;AAAA,EACA,MAAA;AAAA,EACA;AACF,CAAA,GAAI,oBAAA,CAAqB,mBAAmB,mBAAA,CAI1C;AAAA,EACA,oBAAA,EAAsB,SAAA;AAAA,EACtB,SAAA,EAAW,CAAA;AAAA,EACX,WAAW,MAAA,CAAO,SAAA;AAAA,EAClB;AACF,CAAU,CAAA;AAqBH,MAAM,aAAA,GAAgB;AAuBtB,MAAM,aAAA,GAAgB;AAkBtB,MAAM,WAAA,GAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBzB,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,SAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBA,GAAA,EAAK,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBL,GAAA,EAAK,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBL,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA0BA,MAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA0BA;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"positive-int.mjs","sources":["../../../src/number/branded-types/positive-int.mts"],"sourcesContent":["import { type PositiveInt as TtfImported_PositiveInt } from 'ts-type-forge';\nimport { expectType } from '../../expect-type.mjs';\nimport { TsDataForgeInternals } from '../refined-number-utils.mjs';\n\nexport type PositiveInt = TtfImported_PositiveInt;\n\ntype ElementType = PositiveInt;\n\nconst typeNameInMessage = 'a positive integer';\n\nconst {\n MIN_VALUE,\n add,\n castType,\n div,\n fromNumber,\n is,\n max: max_,\n min: min_,\n mul,\n pow,\n random,\n sub,\n} = TsDataForgeInternals.RefinedNumberUtils.operatorsForInteger<\n ElementType,\n 1,\n number\n>({\n integerOrSafeInteger: 'Integer',\n MIN_VALUE: 1,\n MAX_VALUE: Number.MAX_VALUE,\n typeNameInMessage,\n} as const);\n\n/**\n * Type guard that checks if a value is a positive integer.\n *\n * Returns `true` for a positive integer — a value with no fractional component,\n * including values outside the safe integer range (unlike `SafeInt`).\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isPositiveInt(5));\n *\n * assert.isFalse(isPositiveInt(0));\n *\n * assert.isTrue(PositiveInt.is(10));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a positive integer, `false` otherwise\n */\nexport const isPositiveInt = is;\n\n/**\n * Casts a `number` to the `PositiveInt` branded type.\n *\n * Validates that the value is a positive integer and returns it with the\n * `PositiveInt` brand. Throws a `TypeError` otherwise.\n *\n * @example\n *\n * ```ts\n * const branded = asPositiveInt(7);\n *\n * assert.isTrue(branded === 7);\n *\n * assert.isTrue(PositiveInt.is(branded));\n * ```\n *\n * @param value - The value to cast\n * @returns The value as a `PositiveInt`\n * @throws {TypeError} If the value is not a positive integer\n */\nexport const asPositiveInt = castType;\n\n/**\n * Namespace providing type-safe operations for the `PositiveInt` branded type.\n *\n * The `PositiveInt` type represents a positive integer. Division (`div`) uses\n * floor division.\n *\n * Unlike `SafeInt`, `PositiveInt` allows values outside the safe integer range\n * (±2^53 − 1), so very large magnitudes may lose precision in JavaScript's\n * `number` type.\n */\nexport const PositiveInt = {\n /**\n * Type guard that checks if a value is a positive integer.\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isPositiveInt(5));\n *\n * assert.isFalse(isPositiveInt(0));\n *\n * assert.isTrue(PositiveInt.is(10));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a positive integer, `false` otherwise\n * @see {@link isPositiveInt} for usage examples\n */\n is,\n\n /**\n * The smallest value representable as `PositiveInt` (the lower saturation\n * target of `fromNumber`).\n */\n MIN_VALUE,\n\n /**\n * Returns the smallest of the given positive integers.\n *\n * @example\n *\n * ```ts\n * const smallest = PositiveInt.min(\n * asPositiveInt(9),\n * asPositiveInt(3),\n * asPositiveInt(12),\n * );\n *\n * assert.isTrue(smallest === 3);\n * ```\n *\n * @param values - The positive integers to compare (at least one required)\n * @returns The smallest value as a `PositiveInt`\n */\n min: min_,\n\n /**\n * Returns the largest of the given positive integers.\n *\n * @example\n *\n * ```ts\n * const largest = PositiveInt.max(\n * asPositiveInt(9),\n * asPositiveInt(3),\n * asPositiveInt(12),\n * );\n *\n * assert.isTrue(largest === 12);\n * ```\n *\n * @param values - The positive integers to compare (at least one required)\n * @returns The largest value as a `PositiveInt`\n */\n max: max_,\n\n /**\n * Converts an arbitrary `number` into a `PositiveInt`, rounding to the\n * nearest integer and saturating the result into the range `[MIN_VALUE,\n * MAX_VALUE]`.\n *\n * Unlike `asPositiveInt`, this is total: out-of-range inputs are clamped to\n * the nearest representable `PositiveInt` instead of throwing.\n *\n * @example\n *\n * ```ts\n * const belowRange = PositiveInt.fromNumber(0);\n *\n * const withinRange = PositiveInt.fromNumber(10);\n *\n * assert.isTrue(belowRange === 1);\n *\n * assert.isTrue(withinRange === 10);\n * ```\n *\n * @param value - The value to convert\n * @returns The value as a `PositiveInt`\n */\n fromNumber,\n\n /**\n * Generates a random `PositiveInt` within the given range.\n *\n * The range is inclusive on both ends.\n *\n * @example\n *\n * ```ts\n * const min = asPositiveInt(3);\n *\n * const max = asPositiveInt(6);\n *\n * const randomValue = PositiveInt.random(min, max);\n *\n * assert.isTrue(PositiveInt.is(randomValue));\n *\n * assert.isTrue(randomValue >= 3 && randomValue <= 6);\n * ```\n *\n * @param min - The minimum value (inclusive)\n * @param max - The maximum value (inclusive)\n * @returns A random `PositiveInt` in `[min, max]`\n */\n random,\n\n /**\n * Raises `a` to the power `b`, returning `a ** b` as a `PositiveInt` (floored\n * to an integer).\n *\n * @example\n *\n * ```ts\n * const base = asPositiveInt(2);\n *\n * const exponent = asPositiveInt(4);\n *\n * const power = PositiveInt.pow(base, exponent);\n *\n * assert.isTrue(power === 16);\n * ```\n *\n * @param a - The base positive integer\n * @param b - The exponent positive integer\n * @returns `a ** b` as a `PositiveInt`\n */\n pow,\n\n /**\n * Adds two positive integers, returning `a + b` as a `PositiveInt`.\n *\n * @example\n *\n * ```ts\n * const sum = PositiveInt.add(asPositiveInt(4), asPositiveInt(5));\n *\n * assert.isTrue(sum === 9);\n * ```\n *\n * @param a - The first positive integer\n * @param b - The second positive integer\n * @returns The sum of `a` and `b` as a `PositiveInt`\n */\n add,\n\n /**\n * Subtracts two positive integers, returning `a - b` as a `PositiveInt`.\n *\n * @example\n *\n * ```ts\n * const difference = PositiveInt.sub(asPositiveInt(5), asPositiveInt(7));\n *\n * assert.isTrue(difference === 1);\n * ```\n *\n * @param a - The first positive integer\n * @param b - The second positive integer\n * @returns The difference of `a` and `b` as a `PositiveInt`\n */\n sub,\n\n /**\n * Multiplies two positive integers, returning `a * b` as a `PositiveInt`.\n *\n * @example\n *\n * ```ts\n * const product = PositiveInt.mul(asPositiveInt(3), asPositiveInt(7));\n *\n * assert.isTrue(product === 21);\n * ```\n *\n * @param a - The first positive integer\n * @param b - The second positive integer\n * @returns The product of `a` and `b` as a `PositiveInt`\n */\n mul,\n\n /**\n * Divides two positive integers using floor division (`⌊a / b⌋`): the result\n * is `a / b` rounded toward negative infinity, as a `PositiveInt`.\n *\n * @example\n *\n * ```ts\n * const quotient = PositiveInt.div(asPositiveInt(9), asPositiveInt(2));\n *\n * const clamped = PositiveInt.div(asPositiveInt(3), asPositiveInt(10));\n *\n * assert.isTrue(quotient === 4);\n *\n * assert.isTrue(clamped === 1);\n * ```\n *\n * @param a - The dividend\n * @param b - The divisor (must be non-zero)\n * @returns The floored quotient as a `PositiveInt`\n */\n div,\n} as const;\n\nexpectType<\n keyof typeof PositiveInt,\n keyof TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'positive'\n >\n>('=');\n\nexpectType<\n typeof PositiveInt,\n TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'positive'\n >\n>('<=');\n"],"names":[],"mappings":";;AAQA,MAAM,iBAAA,GAAoB,oBAAA;AAE1B,MAAM;AAAA,EACJ,SAAA;AAAA,EACA,GAAA;AAAA,EACA,QAAA;AAAA,EACA,GAAA;AAAA,EACA,UAAA;AAAA,EACA,EAAA;AAAA,EACA,GAAA,EAAK,IAAA;AAAA,EACL,GAAA,EAAK,IAAA;AAAA,EACL,GAAA;AAAA,EACA,GAAA;AAAA,EACA,MAAA;AAAA,EACA;AACF,CAAA,GAAI,oBAAA,CAAqB,mBAAmB,mBAAA,CAI1C;AAAA,EACA,oBAAA,EAAsB,SAAA;AAAA,EACtB,SAAA,EAAW,CAAA;AAAA,EACX,WAAW,MAAA,CAAO,SAAA;AAAA,EAClB;AACF,CAAU,CAAA;AAqBH,MAAM,aAAA,GAAgB;AAsBtB,MAAM,aAAA,GAAgB;AAYtB,MAAM,WAAA,GAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBzB,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA,GAAA,EAAK,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBL,GAAA,EAAK,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAyBL,UAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAyBA,MAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBA;AACF;;;;"}
|