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,7 @@ export type SafeInt = TtfImported_SafeInt;
|
|
|
3
3
|
/**
|
|
4
4
|
* Type guard that checks if a value is a safe integer.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
* without precision loss. The range is [±(2^53 - 1)].
|
|
6
|
+
* Returns `true` for a safe integer — a value with no fractional component.
|
|
8
7
|
*
|
|
9
8
|
* @example
|
|
10
9
|
*
|
|
@@ -21,11 +20,10 @@ export type SafeInt = TtfImported_SafeInt;
|
|
|
21
20
|
*/
|
|
22
21
|
export declare const isSafeInt: (a: number) => a is TtfImported_SafeInt;
|
|
23
22
|
/**
|
|
24
|
-
* Casts a number to
|
|
23
|
+
* Casts a `number` to the `SafeInt` branded type.
|
|
25
24
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* operations that require precise integer arithmetic.
|
|
25
|
+
* Validates that the value is a safe integer and returns it with the `SafeInt`
|
|
26
|
+
* brand. Throws a `TypeError` otherwise.
|
|
29
27
|
*
|
|
30
28
|
* @example
|
|
31
29
|
*
|
|
@@ -38,7 +36,7 @@ export declare const isSafeInt: (a: number) => a is TtfImported_SafeInt;
|
|
|
38
36
|
* ```
|
|
39
37
|
*
|
|
40
38
|
* @param value - The value to cast
|
|
41
|
-
* @returns The value as a SafeInt
|
|
39
|
+
* @returns The value as a `SafeInt`
|
|
42
40
|
* @throws {TypeError} If the value is not a safe integer
|
|
43
41
|
*/
|
|
44
42
|
export declare const asSafeInt: <N extends number>(x: N) => number & {
|
|
@@ -50,20 +48,10 @@ export declare const asSafeInt: <N extends number>(x: N) => number & {
|
|
|
50
48
|
'TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3': unknown;
|
|
51
49
|
}> & N;
|
|
52
50
|
/**
|
|
53
|
-
* Namespace providing type-safe operations for SafeInt branded
|
|
51
|
+
* Namespace providing type-safe operations for the `SafeInt` branded type.
|
|
54
52
|
*
|
|
55
|
-
* SafeInt represents
|
|
56
|
-
*
|
|
57
|
-
* approximately ±9 quadrillion.
|
|
58
|
-
*
|
|
59
|
-
* All operations automatically clamp results to stay within the safe range,
|
|
60
|
-
* preventing precision loss that occurs with larger integers. This makes
|
|
61
|
-
* SafeInt ideal for:
|
|
62
|
-
*
|
|
63
|
-
* - Financial calculations requiring exact cents
|
|
64
|
-
* - Database IDs and counters
|
|
65
|
-
* - Array indices and sizes
|
|
66
|
-
* - Any integer arithmetic requiring precision guarantees
|
|
53
|
+
* The `SafeInt` type represents a safe integer. Division (`div`) uses floor
|
|
54
|
+
* division.
|
|
67
55
|
*/
|
|
68
56
|
export declare const SafeInt: {
|
|
69
57
|
/**
|
|
@@ -85,22 +73,19 @@ export declare const SafeInt: {
|
|
|
85
73
|
*/
|
|
86
74
|
readonly is: (a: number) => a is TtfImported_SafeInt;
|
|
87
75
|
/**
|
|
88
|
-
* The
|
|
89
|
-
*
|
|
90
|
-
* @readonly
|
|
76
|
+
* The smallest value representable as `SafeInt` (the lower saturation target
|
|
77
|
+
* of `fromNumber`).
|
|
91
78
|
*/
|
|
92
79
|
readonly MIN_VALUE: TtfImported_SafeInt;
|
|
93
80
|
/**
|
|
94
|
-
* The
|
|
95
|
-
*
|
|
96
|
-
* @readonly
|
|
81
|
+
* The largest value representable as `SafeInt` (the upper saturation target
|
|
82
|
+
* of `fromNumber`).
|
|
97
83
|
*/
|
|
98
84
|
readonly MAX_VALUE: SafeUint;
|
|
99
85
|
/**
|
|
100
86
|
* Returns the absolute value of a safe integer.
|
|
101
87
|
*
|
|
102
|
-
*
|
|
103
|
-
* function clamps the result to maintain the safe integer guarantee.
|
|
88
|
+
* The result is non-negative and keeps the `SafeInt` brand.
|
|
104
89
|
*
|
|
105
90
|
* @example
|
|
106
91
|
*
|
|
@@ -115,11 +100,11 @@ export declare const SafeInt: {
|
|
|
115
100
|
* ```
|
|
116
101
|
*
|
|
117
102
|
* @param a - The safe integer value
|
|
118
|
-
* @returns The absolute value as a SafeInt
|
|
103
|
+
* @returns The absolute value as a non-negative `SafeInt`
|
|
119
104
|
*/
|
|
120
105
|
readonly abs: (x: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>) => import("ts-type-forge").IntersectBrand<TtfImported_SafeInt, import("ts-type-forge").NonNegativeNumber>;
|
|
121
106
|
/**
|
|
122
|
-
* Returns the
|
|
107
|
+
* Returns the smallest of the given safe integers.
|
|
123
108
|
*
|
|
124
109
|
* @example
|
|
125
110
|
*
|
|
@@ -130,11 +115,11 @@ export declare const SafeInt: {
|
|
|
130
115
|
* ```
|
|
131
116
|
*
|
|
132
117
|
* @param values - The safe integers to compare (at least one required)
|
|
133
|
-
* @returns The smallest value as a SafeInt
|
|
118
|
+
* @returns The smallest value as a `SafeInt`
|
|
134
119
|
*/
|
|
135
120
|
readonly min: (...values: readonly import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>[]) => TtfImported_SafeInt;
|
|
136
121
|
/**
|
|
137
|
-
* Returns the
|
|
122
|
+
* Returns the largest of the given safe integers.
|
|
138
123
|
*
|
|
139
124
|
* @example
|
|
140
125
|
*
|
|
@@ -145,20 +130,24 @@ export declare const SafeInt: {
|
|
|
145
130
|
* ```
|
|
146
131
|
*
|
|
147
132
|
* @param values - The safe integers to compare (at least one required)
|
|
148
|
-
* @returns The largest value as a SafeInt
|
|
133
|
+
* @returns The largest value as a `SafeInt`
|
|
149
134
|
*/
|
|
150
135
|
readonly max: (...values: readonly import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>[]) => TtfImported_SafeInt;
|
|
151
136
|
/**
|
|
152
|
-
*
|
|
137
|
+
* Converts an arbitrary `number` into a `SafeInt`, rounding to the nearest
|
|
138
|
+
* integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
|
|
139
|
+
*
|
|
140
|
+
* Unlike `asSafeInt`, this is total: out-of-range inputs are clamped to the
|
|
141
|
+
* nearest representable `SafeInt` instead of throwing.
|
|
153
142
|
*
|
|
154
143
|
* @example
|
|
155
144
|
*
|
|
156
145
|
* ```ts
|
|
157
|
-
* const aboveRange = SafeInt.
|
|
146
|
+
* const aboveRange = SafeInt.fromNumber(1e20);
|
|
158
147
|
*
|
|
159
|
-
* const withinRange = SafeInt.
|
|
148
|
+
* const withinRange = SafeInt.fromNumber(123);
|
|
160
149
|
*
|
|
161
|
-
* const belowRange = SafeInt.
|
|
150
|
+
* const belowRange = SafeInt.fromNumber(-1e20);
|
|
162
151
|
*
|
|
163
152
|
* assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);
|
|
164
153
|
*
|
|
@@ -167,16 +156,14 @@ export declare const SafeInt: {
|
|
|
167
156
|
* assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);
|
|
168
157
|
* ```
|
|
169
158
|
*
|
|
170
|
-
* @param value The
|
|
171
|
-
* @returns The value
|
|
172
|
-
* SafeInt.
|
|
159
|
+
* @param value - The value to convert
|
|
160
|
+
* @returns The value as a `SafeInt`
|
|
173
161
|
*/
|
|
174
|
-
readonly
|
|
162
|
+
readonly fromNumber: (x: number) => TtfImported_SafeInt;
|
|
175
163
|
/**
|
|
176
|
-
* Generates a random
|
|
164
|
+
* Generates a random `SafeInt` within the given range.
|
|
177
165
|
*
|
|
178
|
-
* The range is inclusive on both ends.
|
|
179
|
-
* swapped.
|
|
166
|
+
* The range is inclusive on both ends.
|
|
180
167
|
*
|
|
181
168
|
* @example
|
|
182
169
|
*
|
|
@@ -194,11 +181,12 @@ export declare const SafeInt: {
|
|
|
194
181
|
*
|
|
195
182
|
* @param min - The minimum value (inclusive)
|
|
196
183
|
* @param max - The maximum value (inclusive)
|
|
197
|
-
* @returns A random SafeInt in
|
|
184
|
+
* @returns A random `SafeInt` in `[min, max]`
|
|
198
185
|
*/
|
|
199
186
|
readonly random: (min?: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt> | undefined, max?: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt> | undefined) => TtfImported_SafeInt;
|
|
200
187
|
/**
|
|
201
|
-
* Raises a
|
|
188
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `SafeInt` (floored to
|
|
189
|
+
* an integer).
|
|
202
190
|
*
|
|
203
191
|
* @example
|
|
204
192
|
*
|
|
@@ -214,13 +202,13 @@ export declare const SafeInt: {
|
|
|
214
202
|
* assert.isTrue(SafeInt.is(power));
|
|
215
203
|
* ```
|
|
216
204
|
*
|
|
217
|
-
* @param a The base
|
|
218
|
-
* @param b The exponent
|
|
219
|
-
* @returns `a ** b`
|
|
205
|
+
* @param a - The base safe integer
|
|
206
|
+
* @param b - The exponent safe integer
|
|
207
|
+
* @returns `a ** b` as a `SafeInt`
|
|
220
208
|
*/
|
|
221
209
|
readonly pow: (x: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>) => TtfImported_SafeInt;
|
|
222
210
|
/**
|
|
223
|
-
* Adds two SafeInt
|
|
211
|
+
* Adds two safe integers, returning `a + b` as a `SafeInt`.
|
|
224
212
|
*
|
|
225
213
|
* @example
|
|
226
214
|
*
|
|
@@ -232,13 +220,13 @@ export declare const SafeInt: {
|
|
|
232
220
|
* assert.isTrue(SafeInt.is(sum));
|
|
233
221
|
* ```
|
|
234
222
|
*
|
|
235
|
-
* @param a The first
|
|
236
|
-
* @param b The second
|
|
237
|
-
* @returns `a
|
|
223
|
+
* @param a - The first safe integer
|
|
224
|
+
* @param b - The second safe integer
|
|
225
|
+
* @returns The sum of `a` and `b` as a `SafeInt`
|
|
238
226
|
*/
|
|
239
227
|
readonly add: (x: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>) => TtfImported_SafeInt;
|
|
240
228
|
/**
|
|
241
|
-
* Subtracts
|
|
229
|
+
* Subtracts two safe integers, returning `a - b` as a `SafeInt`.
|
|
242
230
|
*
|
|
243
231
|
* @example
|
|
244
232
|
*
|
|
@@ -250,13 +238,13 @@ export declare const SafeInt: {
|
|
|
250
238
|
* assert.isTrue(SafeInt.is(difference));
|
|
251
239
|
* ```
|
|
252
240
|
*
|
|
253
|
-
* @param a The
|
|
254
|
-
* @param b The
|
|
255
|
-
* @returns `a
|
|
241
|
+
* @param a - The first safe integer
|
|
242
|
+
* @param b - The second safe integer
|
|
243
|
+
* @returns The difference of `a` and `b` as a `SafeInt`
|
|
256
244
|
*/
|
|
257
245
|
readonly sub: (x: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>) => TtfImported_SafeInt;
|
|
258
246
|
/**
|
|
259
|
-
* Multiplies two SafeInt
|
|
247
|
+
* Multiplies two safe integers, returning `a * b` as a `SafeInt`.
|
|
260
248
|
*
|
|
261
249
|
* @example
|
|
262
250
|
*
|
|
@@ -268,16 +256,14 @@ export declare const SafeInt: {
|
|
|
268
256
|
* assert.isTrue(SafeInt.is(product));
|
|
269
257
|
* ```
|
|
270
258
|
*
|
|
271
|
-
* @param a The first
|
|
272
|
-
* @param b The second
|
|
273
|
-
* @returns `a
|
|
259
|
+
* @param a - The first safe integer
|
|
260
|
+
* @param b - The second safe integer
|
|
261
|
+
* @returns The product of `a` and `b` as a `SafeInt`
|
|
274
262
|
*/
|
|
275
263
|
readonly mul: (x: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>, y: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>) => TtfImported_SafeInt;
|
|
276
264
|
/**
|
|
277
|
-
* Divides
|
|
278
|
-
*
|
|
279
|
-
* Performs mathematical floor division: `⌊a / b⌋`. The divisor must be
|
|
280
|
-
* non-zero (enforced by type constraints).
|
|
265
|
+
* Divides two safe integers using floor division (`⌊a / b⌋`): the result is
|
|
266
|
+
* `a / b` rounded toward negative infinity, as a `SafeInt`.
|
|
281
267
|
*
|
|
282
268
|
* @example
|
|
283
269
|
*
|
|
@@ -291,7 +277,7 @@ export declare const SafeInt: {
|
|
|
291
277
|
*
|
|
292
278
|
* @param a - The dividend
|
|
293
279
|
* @param b - The divisor (must be non-zero)
|
|
294
|
-
* @returns The
|
|
280
|
+
* @returns The floored quotient as a `SafeInt`
|
|
295
281
|
*/
|
|
296
282
|
readonly div: (x: import("ts-type-forge").WithSmallInt<TtfImported_SafeInt>, y: -40 | -39 | -38 | -37 | -36 | -35 | -34 | -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 | 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 & {
|
|
297
283
|
readonly "!=0": true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"safe-int.d.mts","sourceRoot":"","sources":["../../../src/number/branded-types/safe-int.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,OAAO,IAAI,mBAAmB,EACpC,MAAM,eAAe,CAAC;AAIvB,MAAM,MAAM,OAAO,GAAG,mBAAmB,CAAC;AAkC1C
|
|
1
|
+
{"version":3,"file":"safe-int.d.mts","sourceRoot":"","sources":["../../../src/number/branded-types/safe-int.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,OAAO,IAAI,mBAAmB,EACpC,MAAM,eAAe,CAAC;AAIvB,MAAM,MAAM,OAAO,GAAG,mBAAmB,CAAC;AAkC1C;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,SAAS,yCAAK,CAAC;AAE5B;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,SAAS;;;;;;;MAAW,CAAC;AAElC;;;;;GAKG;AACH,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;;;;;;;OAgBG;;IAGH;;;OAGG;;IAGH;;;OAGG;;IAGH;;;;;;;;;;;;;;;;;;;OAmBG;;IAGH;;;;;;;;;;;;;OAaG;;IAGH;;;;;;;;;;;;;OAaG;;IAGH;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;;IAGH;;;;;;;;;;;;;;;;;;;;;;OAsBG;;IAGH;;;;;;;;;;;;;;;;;;;;;OAqBG;;IAGH;;;;;;;;;;;;;;;;OAgBG;;IAGH;;;;;;;;;;;;;;;;OAgBG;;IAGH;;;;;;;;;;;;;;;;OAgBG;;IAGH;;;;;;;;;;;;;;;;;OAiBG;;;;;;;;;;CAEK,CAAC"}
|
|
@@ -7,8 +7,8 @@ const {
|
|
|
7
7
|
abs,
|
|
8
8
|
add,
|
|
9
9
|
castType,
|
|
10
|
-
clamp,
|
|
11
10
|
div,
|
|
11
|
+
fromNumber,
|
|
12
12
|
is,
|
|
13
13
|
max: max_,
|
|
14
14
|
min: min_,
|
|
@@ -46,22 +46,19 @@ const SafeInt = {
|
|
|
46
46
|
*/
|
|
47
47
|
is,
|
|
48
48
|
/**
|
|
49
|
-
* The
|
|
50
|
-
*
|
|
51
|
-
* @readonly
|
|
49
|
+
* The smallest value representable as `SafeInt` (the lower saturation target
|
|
50
|
+
* of `fromNumber`).
|
|
52
51
|
*/
|
|
53
52
|
MIN_VALUE,
|
|
54
53
|
/**
|
|
55
|
-
* The
|
|
56
|
-
*
|
|
57
|
-
* @readonly
|
|
54
|
+
* The largest value representable as `SafeInt` (the upper saturation target
|
|
55
|
+
* of `fromNumber`).
|
|
58
56
|
*/
|
|
59
57
|
MAX_VALUE,
|
|
60
58
|
/**
|
|
61
59
|
* Returns the absolute value of a safe integer.
|
|
62
60
|
*
|
|
63
|
-
*
|
|
64
|
-
* function clamps the result to maintain the safe integer guarantee.
|
|
61
|
+
* The result is non-negative and keeps the `SafeInt` brand.
|
|
65
62
|
*
|
|
66
63
|
* @example
|
|
67
64
|
*
|
|
@@ -76,11 +73,11 @@ const SafeInt = {
|
|
|
76
73
|
* ```
|
|
77
74
|
*
|
|
78
75
|
* @param a - The safe integer value
|
|
79
|
-
* @returns The absolute value as a SafeInt
|
|
76
|
+
* @returns The absolute value as a non-negative `SafeInt`
|
|
80
77
|
*/
|
|
81
78
|
abs,
|
|
82
79
|
/**
|
|
83
|
-
* Returns the
|
|
80
|
+
* Returns the smallest of the given safe integers.
|
|
84
81
|
*
|
|
85
82
|
* @example
|
|
86
83
|
*
|
|
@@ -91,11 +88,11 @@ const SafeInt = {
|
|
|
91
88
|
* ```
|
|
92
89
|
*
|
|
93
90
|
* @param values - The safe integers to compare (at least one required)
|
|
94
|
-
* @returns The smallest value as a SafeInt
|
|
91
|
+
* @returns The smallest value as a `SafeInt`
|
|
95
92
|
*/
|
|
96
93
|
min: min_,
|
|
97
94
|
/**
|
|
98
|
-
* Returns the
|
|
95
|
+
* Returns the largest of the given safe integers.
|
|
99
96
|
*
|
|
100
97
|
* @example
|
|
101
98
|
*
|
|
@@ -106,20 +103,24 @@ const SafeInt = {
|
|
|
106
103
|
* ```
|
|
107
104
|
*
|
|
108
105
|
* @param values - The safe integers to compare (at least one required)
|
|
109
|
-
* @returns The largest value as a SafeInt
|
|
106
|
+
* @returns The largest value as a `SafeInt`
|
|
110
107
|
*/
|
|
111
108
|
max: max_,
|
|
112
109
|
/**
|
|
113
|
-
*
|
|
110
|
+
* Converts an arbitrary `number` into a `SafeInt`, rounding to the nearest
|
|
111
|
+
* integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
|
|
112
|
+
*
|
|
113
|
+
* Unlike `asSafeInt`, this is total: out-of-range inputs are clamped to the
|
|
114
|
+
* nearest representable `SafeInt` instead of throwing.
|
|
114
115
|
*
|
|
115
116
|
* @example
|
|
116
117
|
*
|
|
117
118
|
* ```ts
|
|
118
|
-
* const aboveRange = SafeInt.
|
|
119
|
+
* const aboveRange = SafeInt.fromNumber(1e20);
|
|
119
120
|
*
|
|
120
|
-
* const withinRange = SafeInt.
|
|
121
|
+
* const withinRange = SafeInt.fromNumber(123);
|
|
121
122
|
*
|
|
122
|
-
* const belowRange = SafeInt.
|
|
123
|
+
* const belowRange = SafeInt.fromNumber(-1e20);
|
|
123
124
|
*
|
|
124
125
|
* assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);
|
|
125
126
|
*
|
|
@@ -128,16 +129,14 @@ const SafeInt = {
|
|
|
128
129
|
* assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);
|
|
129
130
|
* ```
|
|
130
131
|
*
|
|
131
|
-
* @param value The
|
|
132
|
-
* @returns The value
|
|
133
|
-
* SafeInt.
|
|
132
|
+
* @param value - The value to convert
|
|
133
|
+
* @returns The value as a `SafeInt`
|
|
134
134
|
*/
|
|
135
|
-
|
|
135
|
+
fromNumber,
|
|
136
136
|
/**
|
|
137
|
-
* Generates a random
|
|
137
|
+
* Generates a random `SafeInt` within the given range.
|
|
138
138
|
*
|
|
139
|
-
* The range is inclusive on both ends.
|
|
140
|
-
* swapped.
|
|
139
|
+
* The range is inclusive on both ends.
|
|
141
140
|
*
|
|
142
141
|
* @example
|
|
143
142
|
*
|
|
@@ -155,11 +154,12 @@ const SafeInt = {
|
|
|
155
154
|
*
|
|
156
155
|
* @param min - The minimum value (inclusive)
|
|
157
156
|
* @param max - The maximum value (inclusive)
|
|
158
|
-
* @returns A random SafeInt in
|
|
157
|
+
* @returns A random `SafeInt` in `[min, max]`
|
|
159
158
|
*/
|
|
160
159
|
random,
|
|
161
160
|
/**
|
|
162
|
-
* Raises a
|
|
161
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `SafeInt` (floored to
|
|
162
|
+
* an integer).
|
|
163
163
|
*
|
|
164
164
|
* @example
|
|
165
165
|
*
|
|
@@ -175,13 +175,13 @@ const SafeInt = {
|
|
|
175
175
|
* assert.isTrue(SafeInt.is(power));
|
|
176
176
|
* ```
|
|
177
177
|
*
|
|
178
|
-
* @param a The base
|
|
179
|
-
* @param b The exponent
|
|
180
|
-
* @returns `a ** b`
|
|
178
|
+
* @param a - The base safe integer
|
|
179
|
+
* @param b - The exponent safe integer
|
|
180
|
+
* @returns `a ** b` as a `SafeInt`
|
|
181
181
|
*/
|
|
182
182
|
pow,
|
|
183
183
|
/**
|
|
184
|
-
* Adds two SafeInt
|
|
184
|
+
* Adds two safe integers, returning `a + b` as a `SafeInt`.
|
|
185
185
|
*
|
|
186
186
|
* @example
|
|
187
187
|
*
|
|
@@ -193,13 +193,13 @@ const SafeInt = {
|
|
|
193
193
|
* assert.isTrue(SafeInt.is(sum));
|
|
194
194
|
* ```
|
|
195
195
|
*
|
|
196
|
-
* @param a The first
|
|
197
|
-
* @param b The second
|
|
198
|
-
* @returns `a
|
|
196
|
+
* @param a - The first safe integer
|
|
197
|
+
* @param b - The second safe integer
|
|
198
|
+
* @returns The sum of `a` and `b` as a `SafeInt`
|
|
199
199
|
*/
|
|
200
200
|
add,
|
|
201
201
|
/**
|
|
202
|
-
* Subtracts
|
|
202
|
+
* Subtracts two safe integers, returning `a - b` as a `SafeInt`.
|
|
203
203
|
*
|
|
204
204
|
* @example
|
|
205
205
|
*
|
|
@@ -211,13 +211,13 @@ const SafeInt = {
|
|
|
211
211
|
* assert.isTrue(SafeInt.is(difference));
|
|
212
212
|
* ```
|
|
213
213
|
*
|
|
214
|
-
* @param a The
|
|
215
|
-
* @param b The
|
|
216
|
-
* @returns `a
|
|
214
|
+
* @param a - The first safe integer
|
|
215
|
+
* @param b - The second safe integer
|
|
216
|
+
* @returns The difference of `a` and `b` as a `SafeInt`
|
|
217
217
|
*/
|
|
218
218
|
sub,
|
|
219
219
|
/**
|
|
220
|
-
* Multiplies two SafeInt
|
|
220
|
+
* Multiplies two safe integers, returning `a * b` as a `SafeInt`.
|
|
221
221
|
*
|
|
222
222
|
* @example
|
|
223
223
|
*
|
|
@@ -229,16 +229,14 @@ const SafeInt = {
|
|
|
229
229
|
* assert.isTrue(SafeInt.is(product));
|
|
230
230
|
* ```
|
|
231
231
|
*
|
|
232
|
-
* @param a The first
|
|
233
|
-
* @param b The second
|
|
234
|
-
* @returns `a
|
|
232
|
+
* @param a - The first safe integer
|
|
233
|
+
* @param b - The second safe integer
|
|
234
|
+
* @returns The product of `a` and `b` as a `SafeInt`
|
|
235
235
|
*/
|
|
236
236
|
mul,
|
|
237
237
|
/**
|
|
238
|
-
* Divides
|
|
239
|
-
*
|
|
240
|
-
* Performs mathematical floor division: `⌊a / b⌋`. The divisor must be
|
|
241
|
-
* non-zero (enforced by type constraints).
|
|
238
|
+
* Divides two safe integers using floor division (`⌊a / b⌋`): the result is
|
|
239
|
+
* `a / b` rounded toward negative infinity, as a `SafeInt`.
|
|
242
240
|
*
|
|
243
241
|
* @example
|
|
244
242
|
*
|
|
@@ -252,7 +250,7 @@ const SafeInt = {
|
|
|
252
250
|
*
|
|
253
251
|
* @param a - The dividend
|
|
254
252
|
* @param b - The divisor (must be non-zero)
|
|
255
|
-
* @returns The
|
|
253
|
+
* @returns The floored quotient as a `SafeInt`
|
|
256
254
|
*/
|
|
257
255
|
div
|
|
258
256
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"safe-int.mjs","sources":["../../../src/number/branded-types/safe-int.mts"],"sourcesContent":["import {\n type SafeUint,\n type SafeInt as TtfImported_SafeInt,\n} from 'ts-type-forge';\nimport { expectType } from '../../expect-type.mjs';\nimport { TsDataForgeInternals } from '../refined-number-utils.mjs';\n\nexport type SafeInt = TtfImported_SafeInt;\n\ntype ElementType = SafeInt;\n\nconst typeNameInMessage = 'a safe integer';\n\nconst {\n MAX_VALUE,\n MIN_VALUE,\n abs,\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 SafeInt,\n SafeUint\n>({\n integerOrSafeInteger: 'SafeInteger',\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion, math/prefer-number-min-safe-integer\n MIN_VALUE: Number.MIN_SAFE_INTEGER as SafeInt,\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion, math/prefer-number-max-safe-integer\n MAX_VALUE: Number.MAX_SAFE_INTEGER as SafeUint,\n typeNameInMessage,\n} as const);\n\n/**\n * Type guard that checks if a value is a safe integer.\n *\n * A safe integer is an integer that can be exactly represented in JavaScript\n * without precision loss. The range is [±(2^53 - 1)].\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isSafeInt(Number.MAX_SAFE_INTEGER));\n *\n * assert.isFalse(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));\n *\n * assert.isTrue(SafeInt.is(Number.MIN_SAFE_INTEGER));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a safe integer, `false` otherwise\n */\nexport const isSafeInt = is;\n\n/**\n * Casts a number to a SafeInt branded type.\n *\n * This function validates that the input is a safe integer (within ±(2^53 - 1))\n * and returns it with the SafeInt brand. This ensures type safety for\n * operations that require precise integer arithmetic.\n *\n * @example\n *\n * ```ts\n * const branded = asSafeInt(123);\n *\n * assert.isTrue(branded === 123);\n *\n * assert.isTrue(SafeInt.is(branded));\n * ```\n *\n * @param value - The value to cast\n * @returns The value as a SafeInt branded type\n * @throws {TypeError} If the value is not a safe integer\n */\nexport const asSafeInt = castType;\n\n/**\n * Namespace providing type-safe operations for SafeInt branded types.\n *\n * SafeInt represents integers that can be exactly represented in JavaScript's\n * number type without precision loss. The range is [±(2^53 - 1)], which covers\n * approximately ±9 quadrillion.\n *\n * All operations automatically clamp results to stay within the safe range,\n * preventing precision loss that occurs with larger integers. This makes\n * SafeInt ideal for:\n *\n * - Financial calculations requiring exact cents\n * - Database IDs and counters\n * - Array indices and sizes\n * - Any integer arithmetic requiring precision guarantees\n */\nexport const SafeInt = {\n /**\n * Type guard that checks if a value is a safe integer.\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isSafeInt(Number.MAX_SAFE_INTEGER));\n *\n * assert.isFalse(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));\n *\n * assert.isTrue(SafeInt.is(Number.MIN_SAFE_INTEGER));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a safe integer, `false` otherwise\n * @see {@link isSafeInt} for usage examples\n */\n is,\n\n /**\n * The minimum safe integer value (-(2^53 - 1)).\n *\n * @readonly\n */\n MIN_VALUE,\n\n /**\n * The maximum safe integer value (2^53 - 1).\n *\n * @readonly\n */\n MAX_VALUE,\n\n /**\n * Returns the absolute value of a safe integer.\n *\n * Note: `Math.abs(MIN_SAFE_INTEGER)` would exceed `MAX_SAFE_INTEGER`, so this\n * function clamps the result to maintain the safe integer guarantee.\n *\n * @example\n *\n * ```ts\n * const negative = asSafeInt(-900);\n *\n * const absolute = SafeInt.abs(negative);\n *\n * assert.isTrue(absolute === 900);\n *\n * assert.isTrue(SafeInt.is(absolute));\n * ```\n *\n * @param a - The safe integer value\n * @returns The absolute value as a SafeInt, clamped if necessary\n */\n abs,\n\n /**\n * Returns the minimum value from a list of safe integers.\n *\n * @example\n *\n * ```ts\n * const smallest = SafeInt.min(asSafeInt(25), asSafeInt(-14), asSafeInt(99));\n *\n * assert.isTrue(smallest === -14);\n * ```\n *\n * @param values - The safe integers to compare (at least one required)\n * @returns The smallest value as a SafeInt\n */\n min: min_,\n\n /**\n * Returns the maximum value from a list of safe integers.\n *\n * @example\n *\n * ```ts\n * const largest = SafeInt.max(asSafeInt(25), asSafeInt(-14), asSafeInt(99));\n *\n * assert.isTrue(largest === 99);\n * ```\n *\n * @param values - The safe integers to compare (at least one required)\n * @returns The largest value as a SafeInt\n */\n max: max_,\n\n /**\n * Clamps a number to the safe integer range.\n *\n * @example\n *\n * ```ts\n * const aboveRange = SafeInt.clamp(1e20);\n *\n * const withinRange = SafeInt.clamp(123);\n *\n * const belowRange = SafeInt.clamp(-1e20);\n *\n * assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);\n *\n * assert.isTrue(withinRange === 123);\n *\n * assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);\n * ```\n *\n * @param value The number to clamp.\n * @returns The value clamped to [MIN_SAFE_INTEGER, MAX_SAFE_INTEGER] as a\n * SafeInt.\n */\n clamp,\n\n /**\n * Generates a random safe integer within the specified range (inclusive).\n *\n * The range is inclusive on both ends. If min > max, they are automatically\n * swapped.\n *\n * @example\n *\n * ```ts\n * const min = asSafeInt(-10);\n *\n * const max = asSafeInt(10);\n *\n * const randomValue = SafeInt.random(min, max);\n *\n * assert.isTrue(SafeInt.is(randomValue));\n *\n * assert.isTrue(randomValue >= -10 && randomValue <= 10);\n * ```\n *\n * @param min - The minimum value (inclusive)\n * @param max - The maximum value (inclusive)\n * @returns A random SafeInt in the range [min, max]\n */\n random,\n\n /**\n * Raises a SafeInt to the power of another SafeInt.\n *\n * @example\n *\n * ```ts\n * const base = asSafeInt(3);\n *\n * const exponent = asSafeInt(5);\n *\n * const power = SafeInt.pow(base, exponent);\n *\n * assert.isTrue(power === 243);\n *\n * assert.isTrue(SafeInt.is(power));\n * ```\n *\n * @param a The base SafeInt.\n * @param b The exponent SafeInt.\n * @returns `a ** b` clamped to safe integer range as a SafeInt.\n */\n pow,\n\n /**\n * Adds two SafeInt values.\n *\n * @example\n *\n * ```ts\n * const sum = SafeInt.add(asSafeInt(9), asSafeInt(4));\n *\n * assert.isTrue(sum === 13);\n *\n * assert.isTrue(SafeInt.is(sum));\n * ```\n *\n * @param a The first SafeInt.\n * @param b The second SafeInt.\n * @returns `a + b` clamped to safe integer range as a SafeInt.\n */\n add,\n\n /**\n * Subtracts one SafeInt from another.\n *\n * @example\n *\n * ```ts\n * const difference = SafeInt.sub(asSafeInt(9), asSafeInt(14));\n *\n * assert.isTrue(difference === -5);\n *\n * assert.isTrue(SafeInt.is(difference));\n * ```\n *\n * @param a The minuend SafeInt.\n * @param b The subtrahend SafeInt.\n * @returns `a - b` clamped to safe integer range as a SafeInt.\n */\n sub,\n\n /**\n * Multiplies two SafeInt values.\n *\n * @example\n *\n * ```ts\n * const product = SafeInt.mul(asSafeInt(-8), asSafeInt(7));\n *\n * assert.isTrue(product === -56);\n *\n * assert.isTrue(SafeInt.is(product));\n * ```\n *\n * @param a The first SafeInt.\n * @param b The second SafeInt.\n * @returns `a * b` clamped to safe integer range as a SafeInt.\n */\n mul,\n\n /**\n * Divides one SafeInt by another using floor division.\n *\n * Performs mathematical floor division: `⌊a / b⌋`. The divisor must be\n * non-zero (enforced by type constraints).\n *\n * @example\n *\n * ```ts\n * const quotient = SafeInt.div(asSafeInt(-17), asSafeInt(5));\n *\n * assert.isTrue(quotient === -4);\n *\n * assert.isTrue(SafeInt.is(quotient));\n * ```\n *\n * @param a - The dividend\n * @param b - The divisor (must be non-zero)\n * @returns The integer quotient as a SafeInt\n */\n div,\n} as const;\n\nexpectType<\n keyof typeof SafeInt,\n keyof TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'range'\n >\n>('=');\n\nexpectType<\n typeof SafeInt,\n TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'range'\n >\n>('<=');\n"],"names":[],"mappings":";;AAWA,MAAM,iBAAA,GAAoB,gBAAA;AAE1B,MAAM;AAAA,EACJ,SAAA;AAAA,EACA,SAAA;AAAA,EACA,GAAA;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,aAAA;AAAA;AAAA,EAEtB,WAAW,MAAA,CAAO,gBAAA;AAAA;AAAA,EAElB,WAAW,MAAA,CAAO,gBAAA;AAAA,EAClB;AACF,CAAU,CAAA;AAqBH,MAAM,SAAA,GAAY;AAuBlB,MAAM,SAAA,GAAY;AAkBlB,MAAM,OAAA,GAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBrB,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,SAAA;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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,GAAA,EAAK,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBL,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,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;AAAA,EAuBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBA;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"safe-int.mjs","sources":["../../../src/number/branded-types/safe-int.mts"],"sourcesContent":["import {\n type SafeUint,\n type SafeInt as TtfImported_SafeInt,\n} from 'ts-type-forge';\nimport { expectType } from '../../expect-type.mjs';\nimport { TsDataForgeInternals } from '../refined-number-utils.mjs';\n\nexport type SafeInt = TtfImported_SafeInt;\n\ntype ElementType = SafeInt;\n\nconst typeNameInMessage = 'a safe integer';\n\nconst {\n MAX_VALUE,\n MIN_VALUE,\n abs,\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 SafeInt,\n SafeUint\n>({\n integerOrSafeInteger: 'SafeInteger',\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion, math/prefer-number-min-safe-integer\n MIN_VALUE: Number.MIN_SAFE_INTEGER as SafeInt,\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion, math/prefer-number-max-safe-integer\n MAX_VALUE: Number.MAX_SAFE_INTEGER as SafeUint,\n typeNameInMessage,\n} as const);\n\n/**\n * Type guard that checks if a value is a safe integer.\n *\n * Returns `true` for a safe integer — a value with no fractional component.\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isSafeInt(Number.MAX_SAFE_INTEGER));\n *\n * assert.isFalse(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));\n *\n * assert.isTrue(SafeInt.is(Number.MIN_SAFE_INTEGER));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a safe integer, `false` otherwise\n */\nexport const isSafeInt = is;\n\n/**\n * Casts a `number` to the `SafeInt` branded type.\n *\n * Validates that the value is a safe integer and returns it with the `SafeInt`\n * brand. Throws a `TypeError` otherwise.\n *\n * @example\n *\n * ```ts\n * const branded = asSafeInt(123);\n *\n * assert.isTrue(branded === 123);\n *\n * assert.isTrue(SafeInt.is(branded));\n * ```\n *\n * @param value - The value to cast\n * @returns The value as a `SafeInt`\n * @throws {TypeError} If the value is not a safe integer\n */\nexport const asSafeInt = castType;\n\n/**\n * Namespace providing type-safe operations for the `SafeInt` branded type.\n *\n * The `SafeInt` type represents a safe integer. Division (`div`) uses floor\n * division.\n */\nexport const SafeInt = {\n /**\n * Type guard that checks if a value is a safe integer.\n *\n * @example\n *\n * ```ts\n * assert.isTrue(isSafeInt(Number.MAX_SAFE_INTEGER));\n *\n * assert.isFalse(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));\n *\n * assert.isTrue(SafeInt.is(Number.MIN_SAFE_INTEGER));\n * ```\n *\n * @param value - The value to check\n * @returns `true` if the value is a safe integer, `false` otherwise\n * @see {@link isSafeInt} for usage examples\n */\n is,\n\n /**\n * The smallest value representable as `SafeInt` (the lower saturation target\n * of `fromNumber`).\n */\n MIN_VALUE,\n\n /**\n * The largest value representable as `SafeInt` (the upper saturation target\n * of `fromNumber`).\n */\n MAX_VALUE,\n\n /**\n * Returns the absolute value of a safe integer.\n *\n * The result is non-negative and keeps the `SafeInt` brand.\n *\n * @example\n *\n * ```ts\n * const negative = asSafeInt(-900);\n *\n * const absolute = SafeInt.abs(negative);\n *\n * assert.isTrue(absolute === 900);\n *\n * assert.isTrue(SafeInt.is(absolute));\n * ```\n *\n * @param a - The safe integer value\n * @returns The absolute value as a non-negative `SafeInt`\n */\n abs,\n\n /**\n * Returns the smallest of the given safe integers.\n *\n * @example\n *\n * ```ts\n * const smallest = SafeInt.min(asSafeInt(25), asSafeInt(-14), asSafeInt(99));\n *\n * assert.isTrue(smallest === -14);\n * ```\n *\n * @param values - The safe integers to compare (at least one required)\n * @returns The smallest value as a `SafeInt`\n */\n min: min_,\n\n /**\n * Returns the largest of the given safe integers.\n *\n * @example\n *\n * ```ts\n * const largest = SafeInt.max(asSafeInt(25), asSafeInt(-14), asSafeInt(99));\n *\n * assert.isTrue(largest === 99);\n * ```\n *\n * @param values - The safe integers to compare (at least one required)\n * @returns The largest value as a `SafeInt`\n */\n max: max_,\n\n /**\n * Converts an arbitrary `number` into a `SafeInt`, rounding to the nearest\n * integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.\n *\n * Unlike `asSafeInt`, this is total: out-of-range inputs are clamped to the\n * nearest representable `SafeInt` instead of throwing.\n *\n * @example\n *\n * ```ts\n * const aboveRange = SafeInt.fromNumber(1e20);\n *\n * const withinRange = SafeInt.fromNumber(123);\n *\n * const belowRange = SafeInt.fromNumber(-1e20);\n *\n * assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);\n *\n * assert.isTrue(withinRange === 123);\n *\n * assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);\n * ```\n *\n * @param value - The value to convert\n * @returns The value as a `SafeInt`\n */\n fromNumber,\n\n /**\n * Generates a random `SafeInt` within the given range.\n *\n * The range is inclusive on both ends.\n *\n * @example\n *\n * ```ts\n * const min = asSafeInt(-10);\n *\n * const max = asSafeInt(10);\n *\n * const randomValue = SafeInt.random(min, max);\n *\n * assert.isTrue(SafeInt.is(randomValue));\n *\n * assert.isTrue(randomValue >= -10 && randomValue <= 10);\n * ```\n *\n * @param min - The minimum value (inclusive)\n * @param max - The maximum value (inclusive)\n * @returns A random `SafeInt` in `[min, max]`\n */\n random,\n\n /**\n * Raises `a` to the power `b`, returning `a ** b` as a `SafeInt` (floored to\n * an integer).\n *\n * @example\n *\n * ```ts\n * const base = asSafeInt(3);\n *\n * const exponent = asSafeInt(5);\n *\n * const power = SafeInt.pow(base, exponent);\n *\n * assert.isTrue(power === 243);\n *\n * assert.isTrue(SafeInt.is(power));\n * ```\n *\n * @param a - The base safe integer\n * @param b - The exponent safe integer\n * @returns `a ** b` as a `SafeInt`\n */\n pow,\n\n /**\n * Adds two safe integers, returning `a + b` as a `SafeInt`.\n *\n * @example\n *\n * ```ts\n * const sum = SafeInt.add(asSafeInt(9), asSafeInt(4));\n *\n * assert.isTrue(sum === 13);\n *\n * assert.isTrue(SafeInt.is(sum));\n * ```\n *\n * @param a - The first safe integer\n * @param b - The second safe integer\n * @returns The sum of `a` and `b` as a `SafeInt`\n */\n add,\n\n /**\n * Subtracts two safe integers, returning `a - b` as a `SafeInt`.\n *\n * @example\n *\n * ```ts\n * const difference = SafeInt.sub(asSafeInt(9), asSafeInt(14));\n *\n * assert.isTrue(difference === -5);\n *\n * assert.isTrue(SafeInt.is(difference));\n * ```\n *\n * @param a - The first safe integer\n * @param b - The second safe integer\n * @returns The difference of `a` and `b` as a `SafeInt`\n */\n sub,\n\n /**\n * Multiplies two safe integers, returning `a * b` as a `SafeInt`.\n *\n * @example\n *\n * ```ts\n * const product = SafeInt.mul(asSafeInt(-8), asSafeInt(7));\n *\n * assert.isTrue(product === -56);\n *\n * assert.isTrue(SafeInt.is(product));\n * ```\n *\n * @param a - The first safe integer\n * @param b - The second safe integer\n * @returns The product of `a` and `b` as a `SafeInt`\n */\n mul,\n\n /**\n * Divides two safe integers using floor division (`⌊a / b⌋`): the result is\n * `a / b` rounded toward negative infinity, as a `SafeInt`.\n *\n * @example\n *\n * ```ts\n * const quotient = SafeInt.div(asSafeInt(-17), asSafeInt(5));\n *\n * assert.isTrue(quotient === -4);\n *\n * assert.isTrue(SafeInt.is(quotient));\n * ```\n *\n * @param a - The dividend\n * @param b - The divisor (must be non-zero)\n * @returns The floored quotient as a `SafeInt`\n */\n div,\n} as const;\n\nexpectType<\n keyof typeof SafeInt,\n keyof TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'range'\n >\n>('=');\n\nexpectType<\n typeof SafeInt,\n TsDataForgeInternals.RefinedNumberUtils.NumberClass<\n ElementType,\n 'int' | 'range'\n >\n>('<=');\n"],"names":[],"mappings":";;AAWA,MAAM,iBAAA,GAAoB,gBAAA;AAE1B,MAAM;AAAA,EACJ,SAAA;AAAA,EACA,SAAA;AAAA,EACA,GAAA;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,aAAA;AAAA;AAAA,EAEtB,WAAW,MAAA,CAAO,gBAAA;AAAA;AAAA,EAElB,WAAW,MAAA,CAAO,gBAAA;AAAA,EAClB;AACF,CAAU,CAAA;AAoBH,MAAM,SAAA,GAAY;AAsBlB,MAAM,SAAA,GAAY;AAQlB,MAAM,OAAA,GAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBrB,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAA;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;AAAA;AAAA,EAsBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBA,GAAA,EAAK,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBL,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;AAAA;AAAA;AAAA,EA4BL,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;AAAA;AAAA,EAwBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,GAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBA;AACF;;;;"}
|