ts-data-forge 10.0.0 → 11.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array/impl/array-utils-creation.d.mts +3 -3
- package/dist/array/impl/array-utils-creation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-creation.mjs.map +1 -1
- package/dist/array/impl/array-utils-element-access.d.mts +4 -4
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
- package/dist/{guard/as-length-bounded-array.d.mts → array/impl/array-utils-length-bounded-array-cast.d.mts} +9 -9
- package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts.map +1 -0
- package/dist/{guard/as-length-bounded-array.mjs → array/impl/array-utils-length-bounded-array-cast.mjs} +2 -2
- package/dist/array/impl/array-utils-length-bounded-array-cast.mjs.map +1 -0
- package/dist/{guard/is-length-bounded-array.d.mts → array/impl/array-utils-length-bounded-array-guard.d.mts} +13 -13
- package/dist/array/impl/array-utils-length-bounded-array-guard.d.mts.map +1 -0
- package/dist/{guard/is-length-bounded-array.mjs → array/impl/array-utils-length-bounded-array-guard.mjs} +1 -1
- package/dist/array/impl/array-utils-length-bounded-array-guard.mjs.map +1 -0
- package/dist/array/impl/array-utils-modification.d.mts +9 -9
- package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.d.mts +9 -9
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-size.d.mts +3 -3
- package/dist/array/impl/array-utils-size.mjs.map +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts +9 -9
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts +16 -10
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/index.d.mts +2 -0
- package/dist/array/impl/index.d.mts.map +1 -1
- package/dist/array/impl/index.mjs +2 -0
- package/dist/array/impl/index.mjs.map +1 -1
- package/dist/entry-point.mjs +1 -3
- package/dist/entry-point.mjs.map +1 -1
- package/dist/guard/index.d.mts +0 -3
- package/dist/guard/index.d.mts.map +1 -1
- package/dist/guard/index.mjs +0 -3
- package/dist/guard/index.mjs.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -3
- package/dist/index.mjs.map +1 -1
- 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 +65 -58
- package/dist/number/branded-types/int16.d.mts.map +1 -1
- package/dist/number/branded-types/int16.mjs +49 -45
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.d.mts +65 -58
- package/dist/number/branded-types/int32.d.mts.map +1 -1
- package/dist/number/branded-types/int32.mjs +49 -45
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/negative-finite-number.d.mts +68 -59
- package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/negative-finite-number.mjs +52 -46
- package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/negative-int.d.mts +60 -48
- package/dist/number/branded-types/negative-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-int.mjs +40 -36
- package/dist/number/branded-types/negative-int.mjs.map +1 -1
- package/dist/number/branded-types/negative-safe-int.d.mts +57 -56
- package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-safe-int.mjs +40 -41
- package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +74 -64
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.mjs +58 -51
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +64 -57
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int16.mjs +47 -43
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +64 -58
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int32.mjs +47 -44
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.d.mts +64 -54
- package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.mjs +48 -41
- package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-int.d.mts +41 -146
- package/dist/number/branded-types/non-positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-int.mjs +27 -107
- package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.d.mts +54 -53
- package/dist/number/branded-types/non-positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.mjs +37 -39
- 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 +52 -53
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int16.mjs +36 -39
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.d.mts +52 -54
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int32.mjs +36 -40
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.d.mts +53 -54
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs +36 -40
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.d.mts +52 -53
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.mjs +35 -39
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.d.mts +52 -53
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.mjs +35 -39
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +74 -63
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/positive-finite-number.mjs +58 -50
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +46 -68
- package/dist/number/branded-types/positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int.mjs +34 -49
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +61 -57
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int16.mjs +44 -43
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +61 -57
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int32.mjs +44 -43
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +62 -57
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs +45 -43
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +61 -57
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint16.mjs +44 -43
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +61 -58
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint32.mjs +44 -44
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +43 -62
- package/dist/number/branded-types/safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/safe-int.mjs +35 -42
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +62 -57
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
- package/dist/number/branded-types/safe-uint.mjs +46 -44
- package/dist/number/branded-types/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.d.mts +63 -50
- package/dist/number/branded-types/uint.d.mts.map +1 -1
- package/dist/number/branded-types/uint.mjs +42 -39
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.d.mts +60 -58
- package/dist/number/branded-types/uint16.d.mts.map +1 -1
- package/dist/number/branded-types/uint16.mjs +44 -43
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.d.mts +59 -55
- package/dist/number/branded-types/uint32.d.mts.map +1 -1
- package/dist/number/branded-types/uint32.mjs +42 -42
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.d.mts +56 -65
- package/dist/number/enum/int8.d.mts.map +1 -1
- package/dist/number/enum/int8.mjs +44 -43
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.d.mts +60 -58
- package/dist/number/enum/uint8.d.mts.map +1 -1
- package/dist/number/enum/uint8.mjs +44 -43
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/string/index.d.mts +2 -0
- package/dist/string/index.d.mts.map +1 -0
- package/dist/string/index.mjs +2 -0
- package/dist/string/index.mjs.map +1 -0
- package/dist/string/str.d.mts +273 -0
- package/dist/string/str.d.mts.map +1 -0
- package/dist/string/str.mjs +42 -0
- package/dist/string/str.mjs.map +1 -0
- package/package.json +31 -92
- package/src/array/impl/array-utils-creation.mts +2 -1
- package/src/array/impl/array-utils-element-access.mts +3 -3
- package/src/array/impl/array-utils-element-access.test.mts +3 -3
- package/src/{guard/as-length-bounded-array.mts → array/impl/array-utils-length-bounded-array-cast.mts} +9 -9
- package/src/{guard/as-length-bounded-array.test.mts → array/impl/array-utils-length-bounded-array-cast.test.mts} +2 -2
- package/src/{guard/is-length-bounded-array.mts → array/impl/array-utils-length-bounded-array-guard.mts} +12 -12
- package/src/{guard/is-length-bounded-array.test.mts → array/impl/array-utils-length-bounded-array-guard.test.mts} +2 -2
- package/src/array/impl/array-utils-modification.mts +9 -8
- package/src/array/impl/array-utils-modification.test.mts +6 -1
- package/src/array/impl/array-utils-reducing-value.mts +9 -9
- package/src/array/impl/array-utils-size.mts +2 -2
- package/src/array/impl/array-utils-slicing.mts +24 -13
- package/src/array/impl/array-utils-transformation.mts +18 -12
- package/src/array/impl/array-utils-transformation.test.mts +12 -12
- package/src/array/impl/index.mts +2 -0
- package/src/guard/index.mts +0 -3
- package/src/index.mts +1 -0
- 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 +65 -58
- package/src/number/branded-types/int32.mts +65 -58
- package/src/number/branded-types/negative-finite-number.mts +68 -68
- package/src/number/branded-types/negative-int.mts +60 -57
- package/src/number/branded-types/negative-safe-int.mts +57 -65
- package/src/number/branded-types/non-negative-finite-number.mts +74 -64
- package/src/number/branded-types/non-negative-int16.mts +64 -57
- package/src/number/branded-types/non-negative-int32.mts +64 -58
- package/src/number/branded-types/non-positive-finite-number.mts +64 -54
- package/src/number/branded-types/non-positive-int.mts +41 -146
- package/src/number/branded-types/non-positive-safe-int.mts +54 -53
- 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 +52 -53
- package/src/number/branded-types/non-zero-int32.mts +52 -54
- package/src/number/branded-types/non-zero-safe-int.mts +53 -54
- package/src/number/branded-types/non-zero-uint16.mts +52 -53
- package/src/number/branded-types/non-zero-uint32.mts +52 -53
- package/src/number/branded-types/positive-finite-number.mts +74 -63
- package/src/number/branded-types/positive-int.mts +46 -68
- package/src/number/branded-types/positive-int16.mts +61 -57
- package/src/number/branded-types/positive-int32.mts +61 -57
- package/src/number/branded-types/positive-safe-int.mts +62 -57
- package/src/number/branded-types/positive-uint16.mts +61 -57
- package/src/number/branded-types/positive-uint32.mts +61 -58
- package/src/number/branded-types/safe-int.mts +43 -62
- package/src/number/branded-types/safe-uint.mts +62 -57
- package/src/number/branded-types/uint.mts +63 -50
- package/src/number/branded-types/uint16.mts +60 -58
- package/src/number/branded-types/uint32.mts +59 -55
- package/src/number/enum/int8.mts +56 -65
- package/src/number/enum/uint8.mts +60 -58
- package/src/string/index.mts +1 -0
- package/src/string/str.mts +371 -0
- package/src/string/str.test.mts +215 -0
- package/dist/guard/as-length-bounded-array.d.mts.map +0 -1
- package/dist/guard/as-length-bounded-array.mjs.map +0 -1
- package/dist/guard/is-length-bounded-array.d.mts.map +0 -1
- package/dist/guard/is-length-bounded-array.mjs.map +0 -1
- package/dist/guard/is-length-bounded-string.d.mts +0 -163
- package/dist/guard/is-length-bounded-string.d.mts.map +0 -1
- package/dist/guard/is-length-bounded-string.mjs +0 -7
- package/dist/guard/is-length-bounded-string.mjs.map +0 -1
- package/src/guard/is-length-bounded-string.mts +0 -199
- package/src/guard/is-length-bounded-string.test.mts +0 -170
|
@@ -42,8 +42,7 @@ const {
|
|
|
42
42
|
/**
|
|
43
43
|
* Type guard that checks if a value is a safe integer.
|
|
44
44
|
*
|
|
45
|
-
*
|
|
46
|
-
* without precision loss. The range is [±(2^53 - 1)].
|
|
45
|
+
* Returns `true` for a safe integer — a value with no fractional component.
|
|
47
46
|
*
|
|
48
47
|
* @example
|
|
49
48
|
*
|
|
@@ -61,11 +60,10 @@ const {
|
|
|
61
60
|
export const isSafeInt = is;
|
|
62
61
|
|
|
63
62
|
/**
|
|
64
|
-
* Casts a number to
|
|
63
|
+
* Casts a `number` to the `SafeInt` branded type.
|
|
65
64
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* operations that require precise integer arithmetic.
|
|
65
|
+
* Validates that the value is a safe integer and returns it with the `SafeInt`
|
|
66
|
+
* brand. Throws a `TypeError` otherwise.
|
|
69
67
|
*
|
|
70
68
|
* @example
|
|
71
69
|
*
|
|
@@ -78,26 +76,16 @@ export const isSafeInt = is;
|
|
|
78
76
|
* ```
|
|
79
77
|
*
|
|
80
78
|
* @param value - The value to cast
|
|
81
|
-
* @returns The value as a SafeInt
|
|
79
|
+
* @returns The value as a `SafeInt`
|
|
82
80
|
* @throws {TypeError} If the value is not a safe integer
|
|
83
81
|
*/
|
|
84
82
|
export const asSafeInt = castType;
|
|
85
83
|
|
|
86
84
|
/**
|
|
87
|
-
* Namespace providing type-safe operations for SafeInt branded
|
|
85
|
+
* Namespace providing type-safe operations for the `SafeInt` branded type.
|
|
88
86
|
*
|
|
89
|
-
* SafeInt represents
|
|
90
|
-
*
|
|
91
|
-
* approximately ±9 quadrillion.
|
|
92
|
-
*
|
|
93
|
-
* All operations automatically clamp results to stay within the safe range,
|
|
94
|
-
* preventing precision loss that occurs with larger integers. This makes
|
|
95
|
-
* SafeInt ideal for:
|
|
96
|
-
*
|
|
97
|
-
* - Financial calculations requiring exact cents
|
|
98
|
-
* - Database IDs and counters
|
|
99
|
-
* - Array indices and sizes
|
|
100
|
-
* - Any integer arithmetic requiring precision guarantees
|
|
87
|
+
* The `SafeInt` type represents a safe integer. Division (`div`) uses floor
|
|
88
|
+
* division.
|
|
101
89
|
*/
|
|
102
90
|
export const SafeInt = {
|
|
103
91
|
/**
|
|
@@ -120,24 +108,19 @@ export const SafeInt = {
|
|
|
120
108
|
is,
|
|
121
109
|
|
|
122
110
|
/**
|
|
123
|
-
* The
|
|
124
|
-
*
|
|
125
|
-
* @readonly
|
|
111
|
+
* The smallest value representable as `SafeInt`.
|
|
126
112
|
*/
|
|
127
113
|
MIN_VALUE,
|
|
128
114
|
|
|
129
115
|
/**
|
|
130
|
-
* The
|
|
131
|
-
*
|
|
132
|
-
* @readonly
|
|
116
|
+
* The largest value representable as `SafeInt`.
|
|
133
117
|
*/
|
|
134
118
|
MAX_VALUE,
|
|
135
119
|
|
|
136
120
|
/**
|
|
137
121
|
* Returns the absolute value of a safe integer.
|
|
138
122
|
*
|
|
139
|
-
*
|
|
140
|
-
* function clamps the result to maintain the safe integer guarantee.
|
|
123
|
+
* The result is non-negative and keeps the `SafeInt` brand.
|
|
141
124
|
*
|
|
142
125
|
* @example
|
|
143
126
|
*
|
|
@@ -152,12 +135,12 @@ export const SafeInt = {
|
|
|
152
135
|
* ```
|
|
153
136
|
*
|
|
154
137
|
* @param a - The safe integer value
|
|
155
|
-
* @returns The absolute value as a SafeInt
|
|
138
|
+
* @returns The absolute value as a non-negative `SafeInt`
|
|
156
139
|
*/
|
|
157
140
|
abs,
|
|
158
141
|
|
|
159
142
|
/**
|
|
160
|
-
* Returns the
|
|
143
|
+
* Returns the smallest of the given safe integers.
|
|
161
144
|
*
|
|
162
145
|
* @example
|
|
163
146
|
*
|
|
@@ -168,12 +151,12 @@ export const SafeInt = {
|
|
|
168
151
|
* ```
|
|
169
152
|
*
|
|
170
153
|
* @param values - The safe integers to compare (at least one required)
|
|
171
|
-
* @returns The smallest value as a SafeInt
|
|
154
|
+
* @returns The smallest value as a `SafeInt`
|
|
172
155
|
*/
|
|
173
156
|
min: min_,
|
|
174
157
|
|
|
175
158
|
/**
|
|
176
|
-
* Returns the
|
|
159
|
+
* Returns the largest of the given safe integers.
|
|
177
160
|
*
|
|
178
161
|
* @example
|
|
179
162
|
*
|
|
@@ -184,12 +167,13 @@ export const SafeInt = {
|
|
|
184
167
|
* ```
|
|
185
168
|
*
|
|
186
169
|
* @param values - The safe integers to compare (at least one required)
|
|
187
|
-
* @returns The largest value as a SafeInt
|
|
170
|
+
* @returns The largest value as a `SafeInt`
|
|
188
171
|
*/
|
|
189
172
|
max: max_,
|
|
190
173
|
|
|
191
174
|
/**
|
|
192
|
-
* Clamps a number to the
|
|
175
|
+
* Clamps a `number` into the `SafeInt` range, rounding to the nearest integer
|
|
176
|
+
* and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
|
|
193
177
|
*
|
|
194
178
|
* @example
|
|
195
179
|
*
|
|
@@ -207,17 +191,15 @@ export const SafeInt = {
|
|
|
207
191
|
* assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);
|
|
208
192
|
* ```
|
|
209
193
|
*
|
|
210
|
-
* @param value The
|
|
211
|
-
* @returns The
|
|
212
|
-
* SafeInt.
|
|
194
|
+
* @param value - The value to clamp
|
|
195
|
+
* @returns The clamped value as a `SafeInt`
|
|
213
196
|
*/
|
|
214
197
|
clamp,
|
|
215
198
|
|
|
216
199
|
/**
|
|
217
|
-
* Generates a random
|
|
200
|
+
* Generates a random `SafeInt` within the given range.
|
|
218
201
|
*
|
|
219
|
-
* The range is inclusive on both ends.
|
|
220
|
-
* swapped.
|
|
202
|
+
* The range is inclusive on both ends.
|
|
221
203
|
*
|
|
222
204
|
* @example
|
|
223
205
|
*
|
|
@@ -235,12 +217,13 @@ export const SafeInt = {
|
|
|
235
217
|
*
|
|
236
218
|
* @param min - The minimum value (inclusive)
|
|
237
219
|
* @param max - The maximum value (inclusive)
|
|
238
|
-
* @returns A random SafeInt in
|
|
220
|
+
* @returns A random `SafeInt` in `[min, max]`
|
|
239
221
|
*/
|
|
240
222
|
random,
|
|
241
223
|
|
|
242
224
|
/**
|
|
243
|
-
* Raises a
|
|
225
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `SafeInt` (floored to
|
|
226
|
+
* an integer).
|
|
244
227
|
*
|
|
245
228
|
* @example
|
|
246
229
|
*
|
|
@@ -256,14 +239,14 @@ export const SafeInt = {
|
|
|
256
239
|
* assert.isTrue(SafeInt.is(power));
|
|
257
240
|
* ```
|
|
258
241
|
*
|
|
259
|
-
* @param a The base
|
|
260
|
-
* @param b The exponent
|
|
261
|
-
* @returns `a ** b`
|
|
242
|
+
* @param a - The base safe integer
|
|
243
|
+
* @param b - The exponent safe integer
|
|
244
|
+
* @returns `a ** b` as a `SafeInt`
|
|
262
245
|
*/
|
|
263
246
|
pow,
|
|
264
247
|
|
|
265
248
|
/**
|
|
266
|
-
* Adds two SafeInt
|
|
249
|
+
* Adds two safe integers, returning `a + b` as a `SafeInt`.
|
|
267
250
|
*
|
|
268
251
|
* @example
|
|
269
252
|
*
|
|
@@ -275,14 +258,14 @@ export const SafeInt = {
|
|
|
275
258
|
* assert.isTrue(SafeInt.is(sum));
|
|
276
259
|
* ```
|
|
277
260
|
*
|
|
278
|
-
* @param a The first
|
|
279
|
-
* @param b The second
|
|
280
|
-
* @returns `a
|
|
261
|
+
* @param a - The first safe integer
|
|
262
|
+
* @param b - The second safe integer
|
|
263
|
+
* @returns The sum of `a` and `b` as a `SafeInt`
|
|
281
264
|
*/
|
|
282
265
|
add,
|
|
283
266
|
|
|
284
267
|
/**
|
|
285
|
-
* Subtracts
|
|
268
|
+
* Subtracts two safe integers, returning `a - b` as a `SafeInt`.
|
|
286
269
|
*
|
|
287
270
|
* @example
|
|
288
271
|
*
|
|
@@ -294,14 +277,14 @@ export const SafeInt = {
|
|
|
294
277
|
* assert.isTrue(SafeInt.is(difference));
|
|
295
278
|
* ```
|
|
296
279
|
*
|
|
297
|
-
* @param a The
|
|
298
|
-
* @param b The
|
|
299
|
-
* @returns `a
|
|
280
|
+
* @param a - The first safe integer
|
|
281
|
+
* @param b - The second safe integer
|
|
282
|
+
* @returns The difference of `a` and `b` as a `SafeInt`
|
|
300
283
|
*/
|
|
301
284
|
sub,
|
|
302
285
|
|
|
303
286
|
/**
|
|
304
|
-
* Multiplies two SafeInt
|
|
287
|
+
* Multiplies two safe integers, returning `a * b` as a `SafeInt`.
|
|
305
288
|
*
|
|
306
289
|
* @example
|
|
307
290
|
*
|
|
@@ -313,17 +296,15 @@ export const SafeInt = {
|
|
|
313
296
|
* assert.isTrue(SafeInt.is(product));
|
|
314
297
|
* ```
|
|
315
298
|
*
|
|
316
|
-
* @param a The first
|
|
317
|
-
* @param b The second
|
|
318
|
-
* @returns `a
|
|
299
|
+
* @param a - The first safe integer
|
|
300
|
+
* @param b - The second safe integer
|
|
301
|
+
* @returns The product of `a` and `b` as a `SafeInt`
|
|
319
302
|
*/
|
|
320
303
|
mul,
|
|
321
304
|
|
|
322
305
|
/**
|
|
323
|
-
* Divides
|
|
324
|
-
*
|
|
325
|
-
* Performs mathematical floor division: `⌊a / b⌋`. The divisor must be
|
|
326
|
-
* non-zero (enforced by type constraints).
|
|
306
|
+
* Divides two safe integers using floor division (`⌊a / b⌋`): the result is
|
|
307
|
+
* `a / b` rounded toward negative infinity, as a `SafeInt`.
|
|
327
308
|
*
|
|
328
309
|
* @example
|
|
329
310
|
*
|
|
@@ -337,7 +318,7 @@ export const SafeInt = {
|
|
|
337
318
|
*
|
|
338
319
|
* @param a - The dividend
|
|
339
320
|
* @param b - The divisor (must be non-zero)
|
|
340
|
-
* @returns The
|
|
321
|
+
* @returns The floored quotient as a `SafeInt`
|
|
341
322
|
*/
|
|
342
323
|
div,
|
|
343
324
|
} as const;
|
|
@@ -35,131 +35,136 @@ const {
|
|
|
35
35
|
} as const);
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
38
|
+
* Type guard that checks if a value is a non-negative safe integer.
|
|
39
39
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
40
|
+
* Returns `true` for a non-negative safe integer — a value with no fractional
|
|
41
|
+
* component.
|
|
42
|
+
*
|
|
43
|
+
* @param value - The value to check
|
|
44
|
+
* @returns `true` if the value is a non-negative safe integer, `false` otherwise
|
|
42
45
|
*/
|
|
43
46
|
export const isSafeUint = is;
|
|
44
47
|
|
|
45
48
|
/**
|
|
46
|
-
* Casts a number to
|
|
49
|
+
* Casts a `number` to the `SafeUint` branded type.
|
|
50
|
+
*
|
|
51
|
+
* Validates that the value is a non-negative safe integer and returns it with
|
|
52
|
+
* the `SafeUint` brand. Throws a `TypeError` otherwise.
|
|
47
53
|
*
|
|
48
|
-
* @param value The value to cast
|
|
49
|
-
* @returns The value as a SafeUint
|
|
50
|
-
* @throws {TypeError} If the value is not a non-negative safe integer
|
|
54
|
+
* @param value - The value to cast
|
|
55
|
+
* @returns The value as a `SafeUint`
|
|
56
|
+
* @throws {TypeError} If the value is not a non-negative safe integer
|
|
51
57
|
*/
|
|
52
58
|
export const asSafeUint = castType;
|
|
53
59
|
|
|
54
60
|
/**
|
|
55
|
-
* Namespace providing type-safe
|
|
56
|
-
* integers.
|
|
61
|
+
* Namespace providing type-safe operations for the `SafeUint` branded type.
|
|
57
62
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* non-negative constraint and IEEE 754 precision guarantees, preventing
|
|
61
|
-
* precision loss while ensuring results are never negative.
|
|
63
|
+
* The `SafeUint` type represents a non-negative safe integer. Division (`div`)
|
|
64
|
+
* uses floor division.
|
|
62
65
|
*/
|
|
63
66
|
export const SafeUint = {
|
|
64
67
|
/**
|
|
65
|
-
* Type guard
|
|
68
|
+
* Type guard that checks if a value is a non-negative safe integer.
|
|
66
69
|
*
|
|
67
|
-
* @param value The value to check
|
|
68
|
-
* @returns `true` if the value is a non-negative safe integer, `false`
|
|
69
|
-
*
|
|
70
|
+
* @param value - The value to check
|
|
71
|
+
* @returns `true` if the value is a non-negative safe integer, `false` otherwise
|
|
72
|
+
* @see {@link isSafeUint} for usage examples
|
|
70
73
|
*/
|
|
71
74
|
is,
|
|
72
75
|
|
|
73
76
|
/**
|
|
74
|
-
* The
|
|
75
|
-
*
|
|
76
|
-
* @readonly
|
|
77
|
+
* The smallest value representable as `SafeUint`.
|
|
77
78
|
*/
|
|
78
79
|
MIN_VALUE,
|
|
79
80
|
|
|
80
81
|
/**
|
|
81
|
-
* The
|
|
82
|
-
*
|
|
83
|
-
* @readonly
|
|
82
|
+
* The largest value representable as `SafeUint`.
|
|
84
83
|
*/
|
|
85
84
|
MAX_VALUE,
|
|
86
85
|
|
|
87
86
|
/**
|
|
88
|
-
* Returns the
|
|
87
|
+
* Returns the smallest of the given non-negative safe integers.
|
|
89
88
|
*
|
|
90
|
-
* @param
|
|
91
|
-
* @
|
|
92
|
-
* @returns The minimum value as a SafeUint.
|
|
89
|
+
* @param values - The non-negative safe integers to compare (at least one required)
|
|
90
|
+
* @returns The smallest value as a `SafeUint`
|
|
93
91
|
*/
|
|
94
92
|
min: min_,
|
|
95
93
|
|
|
96
94
|
/**
|
|
97
|
-
* Returns the
|
|
95
|
+
* Returns the largest of the given non-negative safe integers.
|
|
98
96
|
*
|
|
99
|
-
* @param
|
|
100
|
-
* @
|
|
101
|
-
* @returns The maximum value as a SafeUint.
|
|
97
|
+
* @param values - The non-negative safe integers to compare (at least one required)
|
|
98
|
+
* @returns The largest value as a `SafeUint`
|
|
102
99
|
*/
|
|
103
100
|
max: max_,
|
|
104
101
|
|
|
105
102
|
/**
|
|
106
|
-
* Clamps a number
|
|
103
|
+
* Clamps a `number` into the `SafeUint` range, rounding to the nearest
|
|
104
|
+
* integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
|
|
107
105
|
*
|
|
108
|
-
* @param value The
|
|
109
|
-
* @returns The
|
|
106
|
+
* @param value - The value to clamp
|
|
107
|
+
* @returns The clamped value as a `SafeUint`
|
|
110
108
|
*/
|
|
111
109
|
clamp,
|
|
112
110
|
|
|
113
111
|
/**
|
|
114
|
-
* Generates a random SafeUint
|
|
112
|
+
* Generates a random `SafeUint` within the given range.
|
|
113
|
+
*
|
|
114
|
+
* The range is inclusive on both ends.
|
|
115
115
|
*
|
|
116
|
-
* @
|
|
116
|
+
* @param min - The minimum value (inclusive)
|
|
117
|
+
* @param max - The maximum value (inclusive)
|
|
118
|
+
* @returns A random `SafeUint` in `[min, max]`
|
|
117
119
|
*/
|
|
118
120
|
random,
|
|
119
121
|
|
|
120
122
|
/**
|
|
121
|
-
* Raises a
|
|
123
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `SafeUint` (floored to
|
|
124
|
+
* an integer).
|
|
122
125
|
*
|
|
123
|
-
* @param a The base
|
|
124
|
-
* @param b The exponent
|
|
125
|
-
* @returns `a ** b`
|
|
126
|
+
* @param a - The base non-negative safe integer
|
|
127
|
+
* @param b - The exponent non-negative safe integer
|
|
128
|
+
* @returns `a ** b` as a `SafeUint`
|
|
126
129
|
*/
|
|
127
130
|
pow,
|
|
128
131
|
|
|
129
132
|
/**
|
|
130
|
-
* Adds two SafeUint
|
|
133
|
+
* Adds two non-negative safe integers, returning `a + b` as a `SafeUint`.
|
|
131
134
|
*
|
|
132
|
-
* @param a The first
|
|
133
|
-
* @param b The second
|
|
134
|
-
* @returns `a
|
|
135
|
+
* @param a - The first non-negative safe integer
|
|
136
|
+
* @param b - The second non-negative safe integer
|
|
137
|
+
* @returns The sum of `a` and `b` as a `SafeUint`
|
|
135
138
|
*/
|
|
136
139
|
add,
|
|
137
140
|
|
|
138
141
|
/**
|
|
139
|
-
* Subtracts
|
|
142
|
+
* Subtracts two non-negative safe integers, returning `a - b` as a
|
|
143
|
+
* `SafeUint`.
|
|
140
144
|
*
|
|
141
|
-
* @param a The
|
|
142
|
-
* @param b The
|
|
143
|
-
* @returns `a
|
|
144
|
-
* 0).
|
|
145
|
+
* @param a - The first non-negative safe integer
|
|
146
|
+
* @param b - The second non-negative safe integer
|
|
147
|
+
* @returns The difference of `a` and `b` as a `SafeUint`
|
|
145
148
|
*/
|
|
146
149
|
sub,
|
|
147
150
|
|
|
148
151
|
/**
|
|
149
|
-
* Multiplies two
|
|
152
|
+
* Multiplies two non-negative safe integers, returning `a * b` as a
|
|
153
|
+
* `SafeUint`.
|
|
150
154
|
*
|
|
151
|
-
* @param a The first
|
|
152
|
-
* @param b The second
|
|
153
|
-
* @returns `a
|
|
155
|
+
* @param a - The first non-negative safe integer
|
|
156
|
+
* @param b - The second non-negative safe integer
|
|
157
|
+
* @returns The product of `a` and `b` as a `SafeUint`
|
|
154
158
|
*/
|
|
155
159
|
mul,
|
|
156
160
|
|
|
157
161
|
/**
|
|
158
|
-
* Divides
|
|
162
|
+
* Divides two non-negative safe integers using floor division (`⌊a / b⌋`):
|
|
163
|
+
* the result is `a / b` rounded toward negative infinity, as a `SafeUint`.
|
|
159
164
|
*
|
|
160
|
-
* @param a The dividend
|
|
161
|
-
* @param b The divisor
|
|
162
|
-
* @returns
|
|
165
|
+
* @param a - The dividend
|
|
166
|
+
* @param b - The divisor (must be non-zero)
|
|
167
|
+
* @returns The floored quotient as a `SafeUint`
|
|
163
168
|
*/
|
|
164
169
|
div,
|
|
165
170
|
} as const;
|