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.
Files changed (185) hide show
  1. package/README.md +2 -2
  2. package/dist/number/branded-types/finite-number.d.mts +47 -61
  3. package/dist/number/branded-types/finite-number.d.mts.map +1 -1
  4. package/dist/number/branded-types/finite-number.mjs +37 -41
  5. package/dist/number/branded-types/finite-number.mjs.map +1 -1
  6. package/dist/number/branded-types/int.d.mts +40 -46
  7. package/dist/number/branded-types/int.d.mts.map +1 -1
  8. package/dist/number/branded-types/int.mjs +29 -31
  9. package/dist/number/branded-types/int.mjs.map +1 -1
  10. package/dist/number/branded-types/int16.d.mts +71 -59
  11. package/dist/number/branded-types/int16.d.mts.map +1 -1
  12. package/dist/number/branded-types/int16.mjs +56 -47
  13. package/dist/number/branded-types/int16.mjs.map +1 -1
  14. package/dist/number/branded-types/int32.d.mts +71 -59
  15. package/dist/number/branded-types/int32.d.mts.map +1 -1
  16. package/dist/number/branded-types/int32.mjs +56 -47
  17. package/dist/number/branded-types/int32.mjs.map +1 -1
  18. package/dist/number/branded-types/negative-finite-number.d.mts +76 -60
  19. package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -1
  20. package/dist/number/branded-types/negative-finite-number.mjs +62 -49
  21. package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
  22. package/dist/number/branded-types/negative-int.d.mts +66 -49
  23. package/dist/number/branded-types/negative-int.d.mts.map +1 -1
  24. package/dist/number/branded-types/negative-int.mjs +48 -39
  25. package/dist/number/branded-types/negative-int.mjs.map +1 -1
  26. package/dist/number/branded-types/negative-safe-int.d.mts +64 -57
  27. package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -1
  28. package/dist/number/branded-types/negative-safe-int.mjs +49 -44
  29. package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
  30. package/dist/number/branded-types/non-negative-finite-number.d.mts +80 -65
  31. package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
  32. package/dist/number/branded-types/non-negative-finite-number.mjs +65 -53
  33. package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
  34. package/dist/number/branded-types/non-negative-int16.d.mts +71 -58
  35. package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
  36. package/dist/number/branded-types/non-negative-int16.mjs +55 -45
  37. package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
  38. package/dist/number/branded-types/non-negative-int32.d.mts +71 -59
  39. package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
  40. package/dist/number/branded-types/non-negative-int32.mjs +55 -46
  41. package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
  42. package/dist/number/branded-types/non-positive-finite-number.d.mts +70 -55
  43. package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -1
  44. package/dist/number/branded-types/non-positive-finite-number.mjs +55 -43
  45. package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
  46. package/dist/number/branded-types/non-positive-int.d.mts +46 -146
  47. package/dist/number/branded-types/non-positive-int.d.mts.map +1 -1
  48. package/dist/number/branded-types/non-positive-int.mjs +33 -108
  49. package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
  50. package/dist/number/branded-types/non-positive-safe-int.d.mts +62 -54
  51. package/dist/number/branded-types/non-positive-safe-int.d.mts.map +1 -1
  52. package/dist/number/branded-types/non-positive-safe-int.mjs +46 -41
  53. package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -1
  54. package/dist/number/branded-types/non-zero-finite-number.d.mts +62 -49
  55. package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
  56. package/dist/number/branded-types/non-zero-finite-number.mjs +46 -37
  57. package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
  58. package/dist/number/branded-types/non-zero-int.d.mts +51 -38
  59. package/dist/number/branded-types/non-zero-int.d.mts.map +1 -1
  60. package/dist/number/branded-types/non-zero-int.mjs +31 -27
  61. package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
  62. package/dist/number/branded-types/non-zero-int16.d.mts +59 -54
  63. package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
  64. package/dist/number/branded-types/non-zero-int16.mjs +44 -41
  65. package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
  66. package/dist/number/branded-types/non-zero-int32.d.mts +59 -55
  67. package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
  68. package/dist/number/branded-types/non-zero-int32.mjs +44 -42
  69. package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
  70. package/dist/number/branded-types/non-zero-safe-int.d.mts +60 -55
  71. package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
  72. package/dist/number/branded-types/non-zero-safe-int.mjs +44 -42
  73. package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
  74. package/dist/number/branded-types/non-zero-uint16.d.mts +59 -54
  75. package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
  76. package/dist/number/branded-types/non-zero-uint16.mjs +43 -41
  77. package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
  78. package/dist/number/branded-types/non-zero-uint32.d.mts +59 -54
  79. package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
  80. package/dist/number/branded-types/non-zero-uint32.mjs +43 -41
  81. package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
  82. package/dist/number/branded-types/positive-finite-number.d.mts +82 -64
  83. package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
  84. package/dist/number/branded-types/positive-finite-number.mjs +67 -52
  85. package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
  86. package/dist/number/branded-types/positive-int.d.mts +53 -70
  87. package/dist/number/branded-types/positive-int.d.mts.map +1 -1
  88. package/dist/number/branded-types/positive-int.mjs +42 -52
  89. package/dist/number/branded-types/positive-int.mjs.map +1 -1
  90. package/dist/number/branded-types/positive-int16.d.mts +68 -58
  91. package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
  92. package/dist/number/branded-types/positive-int16.mjs +52 -45
  93. package/dist/number/branded-types/positive-int16.mjs.map +1 -1
  94. package/dist/number/branded-types/positive-int32.d.mts +68 -58
  95. package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
  96. package/dist/number/branded-types/positive-int32.mjs +52 -45
  97. package/dist/number/branded-types/positive-int32.mjs.map +1 -1
  98. package/dist/number/branded-types/positive-safe-int.d.mts +72 -61
  99. package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
  100. package/dist/number/branded-types/positive-safe-int.mjs +56 -48
  101. package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
  102. package/dist/number/branded-types/positive-uint16.d.mts +68 -58
  103. package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
  104. package/dist/number/branded-types/positive-uint16.mjs +52 -45
  105. package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
  106. package/dist/number/branded-types/positive-uint32.d.mts +68 -59
  107. package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
  108. package/dist/number/branded-types/positive-uint32.mjs +52 -46
  109. package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
  110. package/dist/number/branded-types/safe-int.d.mts +52 -66
  111. package/dist/number/branded-types/safe-int.d.mts.map +1 -1
  112. package/dist/number/branded-types/safe-int.mjs +45 -47
  113. package/dist/number/branded-types/safe-int.mjs.map +1 -1
  114. package/dist/number/branded-types/safe-uint.d.mts +68 -58
  115. package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
  116. package/dist/number/branded-types/safe-uint.mjs +53 -46
  117. package/dist/number/branded-types/safe-uint.mjs.map +1 -1
  118. package/dist/number/branded-types/uint.d.mts +72 -55
  119. package/dist/number/branded-types/uint.d.mts.map +1 -1
  120. package/dist/number/branded-types/uint.mjs +52 -45
  121. package/dist/number/branded-types/uint.mjs.map +1 -1
  122. package/dist/number/branded-types/uint16.d.mts +66 -59
  123. package/dist/number/branded-types/uint16.d.mts.map +1 -1
  124. package/dist/number/branded-types/uint16.mjs +51 -45
  125. package/dist/number/branded-types/uint16.mjs.map +1 -1
  126. package/dist/number/branded-types/uint32.d.mts +66 -57
  127. package/dist/number/branded-types/uint32.d.mts.map +1 -1
  128. package/dist/number/branded-types/uint32.mjs +50 -45
  129. package/dist/number/branded-types/uint32.mjs.map +1 -1
  130. package/dist/number/enum/int8.d.mts +62 -66
  131. package/dist/number/enum/int8.d.mts.map +1 -1
  132. package/dist/number/enum/int8.mjs +57 -51
  133. package/dist/number/enum/int8.mjs.map +1 -1
  134. package/dist/number/enum/uint8.d.mts +66 -59
  135. package/dist/number/enum/uint8.d.mts.map +1 -1
  136. package/dist/number/enum/uint8.mjs +57 -51
  137. package/dist/number/enum/uint8.mjs.map +1 -1
  138. package/dist/number/refined-number-utils.d.mts +9 -9
  139. package/dist/number/refined-number-utils.d.mts.map +1 -1
  140. package/dist/number/refined-number-utils.mjs +7 -7
  141. package/dist/number/refined-number-utils.mjs.map +1 -1
  142. package/package.json +2 -1
  143. package/src/number/branded-types/finite-number.mts +47 -61
  144. package/src/number/branded-types/int.mts +40 -46
  145. package/src/number/branded-types/int16.mts +72 -60
  146. package/src/number/branded-types/int32.mts +72 -60
  147. package/src/number/branded-types/negative-finite-number.mts +78 -71
  148. package/src/number/branded-types/negative-finite-number.test.mts +4 -4
  149. package/src/number/branded-types/negative-int.mts +68 -60
  150. package/src/number/branded-types/negative-int.test.mts +5 -5
  151. package/src/number/branded-types/negative-safe-int.mts +66 -68
  152. package/src/number/branded-types/negative-safe-int.test.mts +4 -4
  153. package/src/number/branded-types/non-negative-finite-number.mts +81 -66
  154. package/src/number/branded-types/non-negative-int16.mts +72 -59
  155. package/src/number/branded-types/non-negative-int32.mts +72 -60
  156. package/src/number/branded-types/non-positive-finite-number.mts +71 -56
  157. package/src/number/branded-types/non-positive-finite-number.test.mts +7 -7
  158. package/src/number/branded-types/non-positive-int.mts +47 -147
  159. package/src/number/branded-types/non-positive-int.test.mts +7 -7
  160. package/src/number/branded-types/non-positive-safe-int.mts +63 -55
  161. package/src/number/branded-types/non-positive-safe-int.test.mts +7 -7
  162. package/src/number/branded-types/non-zero-finite-number.mts +62 -51
  163. package/src/number/branded-types/non-zero-int.mts +51 -38
  164. package/src/number/branded-types/non-zero-int16.mts +60 -55
  165. package/src/number/branded-types/non-zero-int32.mts +60 -56
  166. package/src/number/branded-types/non-zero-safe-int.mts +61 -56
  167. package/src/number/branded-types/non-zero-uint16.mts +60 -55
  168. package/src/number/branded-types/non-zero-uint32.mts +60 -55
  169. package/src/number/branded-types/positive-finite-number.mts +83 -65
  170. package/src/number/branded-types/positive-int.mts +54 -71
  171. package/src/number/branded-types/positive-int16.mts +69 -59
  172. package/src/number/branded-types/positive-int32.mts +69 -59
  173. package/src/number/branded-types/positive-safe-int.mts +73 -62
  174. package/src/number/branded-types/positive-uint16.mts +69 -59
  175. package/src/number/branded-types/positive-uint32.mts +69 -60
  176. package/src/number/branded-types/safe-int.mts +53 -67
  177. package/src/number/branded-types/safe-uint.mts +69 -59
  178. package/src/number/branded-types/uint.mts +73 -56
  179. package/src/number/branded-types/uint16.mts +67 -60
  180. package/src/number/branded-types/uint32.mts +67 -58
  181. package/src/number/enum/int8.mts +70 -73
  182. package/src/number/enum/int8.test.mts +7 -4
  183. package/src/number/enum/uint8.mts +74 -66
  184. package/src/number/enum/uint8.test.mts +7 -4
  185. package/src/number/refined-number-utils.mts +22 -19
@@ -12,8 +12,8 @@ const {
12
12
  MIN_VALUE,
13
13
  add,
14
14
  castType,
15
- clamp,
16
15
  div,
16
+ fromNumber,
17
17
  is,
18
18
  max: max_,
19
19
  min: min_,
@@ -33,7 +33,11 @@ const {
33
33
  } as const);
34
34
 
35
35
  /**
36
- * Checks if a number is a Uint.
36
+ * Type guard that checks if a value is a non-negative integer.
37
+ *
38
+ * Returns `true` for a non-negative integer — a value with no fractional
39
+ * component, including values outside the safe integer range (unlike
40
+ * `SafeInt`).
37
41
  *
38
42
  * @example
39
43
  *
@@ -45,13 +49,16 @@ const {
45
49
  * assert.isTrue(Uint.is(0));
46
50
  * ```
47
51
  *
48
- * @param value The value to check.
49
- * @returns `true` if the value is a Uint, `false` otherwise.
52
+ * @param value - The value to check
53
+ * @returns `true` if the value is a non-negative integer, `false` otherwise
50
54
  */
51
55
  export const isUint = is;
52
56
 
53
57
  /**
54
- * Casts a number to a Uint type.
58
+ * Casts a `number` to the `Uint` branded type.
59
+ *
60
+ * Validates that the value is a non-negative integer and returns it with the
61
+ * `Uint` brand. Throws a `TypeError` otherwise.
55
62
  *
56
63
  * @example
57
64
  *
@@ -63,22 +70,25 @@ export const isUint = is;
63
70
  * assert.isTrue(Uint.is(branded));
64
71
  * ```
65
72
  *
66
- * @param value The value to cast.
67
- * @returns The value as a Uint type.
68
- * @throws {TypeError} If the value is not a non-negative integer.
73
+ * @param value - The value to cast
74
+ * @returns The value as an `Uint`
75
+ * @throws {TypeError} If the value is not a non-negative integer
69
76
  */
70
77
  export const asUint = castType;
71
78
 
72
79
  /**
73
- * Namespace providing type-safe arithmetic operations for unsigned integers.
80
+ * Namespace providing type-safe operations for the `Uint` branded type.
81
+ *
82
+ * The `Uint` type represents a non-negative integer. Division (`div`) uses
83
+ * floor division.
74
84
  *
75
- * All operations maintain the non-negative constraint by clamping negative
76
- * results to 0. This ensures that all arithmetic preserves the unsigned integer
77
- * property.
85
+ * Unlike `SafeInt`, `Uint` allows values outside the safe integer range (±2^53
86
+ * − 1), so very large magnitudes may lose precision in JavaScript's `number`
87
+ * type.
78
88
  */
79
89
  export const Uint = {
80
90
  /**
81
- * Type guard to check if a value is a Uint.
91
+ * Type guard that checks if a value is a non-negative integer.
82
92
  *
83
93
  * @example
84
94
  *
@@ -90,21 +100,20 @@ export const Uint = {
90
100
  * assert.isTrue(Uint.is(0));
91
101
  * ```
92
102
  *
93
- * @param value The value to check.
94
- * @returns `true` if the value is a non-negative integer, `false` otherwise.
103
+ * @param value - The value to check
104
+ * @returns `true` if the value is a non-negative integer, `false` otherwise
95
105
  * @see {@link isUint} for usage examples
96
106
  */
97
107
  is,
98
108
 
99
109
  /**
100
- * The minimum value for an unsigned integer.
101
- *
102
- * @readonly
110
+ * The smallest value representable as `Uint` (the lower saturation target of
111
+ * `fromNumber`).
103
112
  */
104
113
  MIN_VALUE,
105
114
 
106
115
  /**
107
- * Returns the smaller of two Uint values.
116
+ * Returns the smallest of the given non-negative integers.
108
117
  *
109
118
  * @example
110
119
  *
@@ -114,14 +123,13 @@ export const Uint = {
114
123
  * assert.isTrue(smallest === 3);
115
124
  * ```
116
125
  *
117
- * @param a The first Uint.
118
- * @param b The second Uint.
119
- * @returns The minimum value as a Uint.
126
+ * @param values - The non-negative integers to compare (at least one required)
127
+ * @returns The smallest value as an `Uint`
120
128
  */
121
129
  min: min_,
122
130
 
123
131
  /**
124
- * Returns the larger of two Uint values.
132
+ * Returns the largest of the given non-negative integers.
125
133
  *
126
134
  * @example
127
135
  *
@@ -131,34 +139,39 @@ export const Uint = {
131
139
  * assert.isTrue(largest === 7);
132
140
  * ```
133
141
  *
134
- * @param a The first Uint.
135
- * @param b The second Uint.
136
- * @returns The maximum value as a Uint.
142
+ * @param values - The non-negative integers to compare (at least one required)
143
+ * @returns The largest value as an `Uint`
137
144
  */
138
145
  max: max_,
139
146
 
140
147
  /**
141
- * Clamps a number to the Uint range (non-negative).
148
+ * Converts an arbitrary `number` into an `Uint`, rounding to the nearest
149
+ * integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
150
+ *
151
+ * Unlike `asUint`, this is total: out-of-range inputs are clamped to the
152
+ * nearest representable `Uint` instead of throwing.
142
153
  *
143
154
  * @example
144
155
  *
145
156
  * ```ts
146
- * const clampedNegative = Uint.clamp(-5);
157
+ * const fromNegative = Uint.fromNumber(-5);
147
158
  *
148
- * const clampedPositive = Uint.clamp(42);
159
+ * const fromPositive = Uint.fromNumber(42);
149
160
  *
150
- * assert.isTrue(clampedNegative === 0);
161
+ * assert.isTrue(fromNegative === 0);
151
162
  *
152
- * assert.isTrue(clampedPositive === 42);
163
+ * assert.isTrue(fromPositive === 42);
153
164
  * ```
154
165
  *
155
- * @param value The number to clamp.
156
- * @returns The value clamped to [0, +∞) as a Uint.
166
+ * @param value - The value to convert
167
+ * @returns The value as an `Uint`
157
168
  */
158
- clamp,
169
+ fromNumber,
159
170
 
160
171
  /**
161
- * Generates a random Uint value.
172
+ * Generates a random `Uint` within the given range.
173
+ *
174
+ * The range is inclusive on both ends.
162
175
  *
163
176
  * @example
164
177
  *
@@ -174,12 +187,15 @@ export const Uint = {
174
187
  * assert.isTrue(randomValue >= 0 && randomValue <= 3);
175
188
  * ```
176
189
  *
177
- * @returns A random non-negative integer as a Uint.
190
+ * @param min - The minimum value (inclusive)
191
+ * @param max - The maximum value (inclusive)
192
+ * @returns A random `Uint` in `[min, max]`
178
193
  */
179
194
  random,
180
195
 
181
196
  /**
182
- * Raises a Uint to the power of another Uint.
197
+ * Raises `a` to the power `b`, returning `a ** b` as an `Uint` (floored to an
198
+ * integer).
183
199
  *
184
200
  * @example
185
201
  *
@@ -193,14 +209,14 @@ export const Uint = {
193
209
  * assert.isTrue(power === 32);
194
210
  * ```
195
211
  *
196
- * @param a The base Uint.
197
- * @param b The exponent Uint.
198
- * @returns `a ** b` clamped to [0, +∞) as a Uint.
212
+ * @param a - The base non-negative integer
213
+ * @param b - The exponent non-negative integer
214
+ * @returns `a ** b` as an `Uint`
199
215
  */
200
216
  pow,
201
217
 
202
218
  /**
203
- * Adds two Uint values.
219
+ * Adds two non-negative integers, returning `a + b` as an `Uint`.
204
220
  *
205
221
  * @example
206
222
  *
@@ -210,14 +226,14 @@ export const Uint = {
210
226
  * assert.isTrue(sum === 13);
211
227
  * ```
212
228
  *
213
- * @param a The first Uint.
214
- * @param b The second Uint.
215
- * @returns `a + b` clamped to [0, +∞) as a Uint.
229
+ * @param a - The first non-negative integer
230
+ * @param b - The second non-negative integer
231
+ * @returns The sum of `a` and `b` as an `Uint`
216
232
  */
217
233
  add,
218
234
 
219
235
  /**
220
- * Subtracts one Uint from another.
236
+ * Subtracts two non-negative integers, returning `a - b` as an `Uint`.
221
237
  *
222
238
  * @example
223
239
  *
@@ -227,14 +243,14 @@ export const Uint = {
227
243
  * assert.isTrue(difference === 0);
228
244
  * ```
229
245
  *
230
- * @param a The minuend Uint.
231
- * @param b The subtrahend Uint.
232
- * @returns `a - b` clamped to [0, +∞) as a Uint (minimum 0).
246
+ * @param a - The first non-negative integer
247
+ * @param b - The second non-negative integer
248
+ * @returns The difference of `a` and `b` as an `Uint`
233
249
  */
234
250
  sub,
235
251
 
236
252
  /**
237
- * Multiplies two Uint values.
253
+ * Multiplies two non-negative integers, returning `a * b` as an `Uint`.
238
254
  *
239
255
  * @example
240
256
  *
@@ -244,14 +260,15 @@ export const Uint = {
244
260
  * assert.isTrue(product === 42);
245
261
  * ```
246
262
  *
247
- * @param a The first Uint.
248
- * @param b The second Uint.
249
- * @returns `a * b` clamped to [0, +∞) as a Uint.
263
+ * @param a - The first non-negative integer
264
+ * @param b - The second non-negative integer
265
+ * @returns The product of `a` and `b` as an `Uint`
250
266
  */
251
267
  mul,
252
268
 
253
269
  /**
254
- * Divides one Uint by another using floor division.
270
+ * Divides two non-negative integers using floor division (`⌊a / b⌋`): the
271
+ * result is `a / b` rounded toward negative infinity, as an `Uint`.
255
272
  *
256
273
  * @example
257
274
  *
@@ -261,9 +278,9 @@ export const Uint = {
261
278
  * assert.isTrue(quotient === 2);
262
279
  * ```
263
280
  *
264
- * @param a The dividend Uint.
265
- * @param b The divisor Uint.
266
- * @returns `⌊a / b⌋` clamped to [0, +∞) as a Uint.
281
+ * @param a - The dividend
282
+ * @param b - The divisor (must be non-zero)
283
+ * @returns The floored quotient as an `Uint`
267
284
  */
268
285
  div,
269
286
  } as const;
@@ -13,8 +13,8 @@ const {
13
13
  MIN_VALUE,
14
14
  add,
15
15
  castType,
16
- clamp,
17
16
  div,
17
+ fromNumber,
18
18
  is,
19
19
  max: max_,
20
20
  min: min_,
@@ -34,132 +34,139 @@ const {
34
34
  } as const);
35
35
 
36
36
  /**
37
- * Checks if a number is a Uint16 (16-bit unsigned integer in the range [0,
38
- * 2^16)).
37
+ * Type guard that checks if a value is a non-negative integer less than 2^16.
39
38
  *
40
- * @param value The value to check.
41
- * @returns `true` if the value is a Uint16, `false` otherwise.
39
+ * Returns `true` for a non-negative integer less than 2^16 — a value with no
40
+ * fractional component.
41
+ *
42
+ * @param value - The value to check
43
+ * @returns `true` if the value is a non-negative integer less than 2^16, `false` otherwise
42
44
  */
43
45
  export const isUint16 = is;
44
46
 
45
47
  /**
46
- * Casts a number to a Uint16 type.
48
+ * Casts a `number` to the `Uint16` branded type.
49
+ *
50
+ * Validates that the value is a non-negative integer less than 2^16 and returns
51
+ * it with the `Uint16` brand. Throws a `TypeError` otherwise.
47
52
  *
48
- * @param value The value to cast.
49
- * @returns The value as a Uint16 type.
50
- * @throws {TypeError} If the value is not a non-negative integer less than
51
- * 2^16.
53
+ * @param value - The value to cast
54
+ * @returns The value as an `Uint16`
55
+ * @throws {TypeError} If the value is not a non-negative integer less than 2^16
52
56
  */
53
57
  export const asUint16 = castType;
54
58
 
55
59
  /**
56
- * Namespace providing type-safe arithmetic operations for 16-bit unsigned
57
- * integers.
60
+ * Namespace providing type-safe operations for the `Uint16` branded type.
58
61
  *
59
- * All operations automatically clamp results to the valid Uint16 range [0,
60
- * 65535]. This ensures that all arithmetic maintains the 16-bit unsigned
61
- * integer constraint, with negative results clamped to 0 and overflow results
62
- * clamped to MAX_VALUE.
62
+ * The `Uint16` type represents a non-negative integer less than 2^16. Division
63
+ * (`div`) uses floor division.
63
64
  */
64
65
  export const Uint16 = {
65
66
  /**
66
- * Type guard to check if a value is a Uint16.
67
+ * Type guard that checks if a value is a non-negative integer less than 2^16.
67
68
  *
68
- * @param value The value to check.
69
- * @returns `true` if the value is a 16-bit unsigned integer, `false`
70
- * otherwise.
69
+ * @param value - The value to check
70
+ * @returns `true` if the value is a non-negative integer less than 2^16, `false` otherwise
71
+ * @see {@link isUint16} for usage examples
71
72
  */
72
73
  is,
73
74
 
74
75
  /**
75
- * The minimum value for a 16-bit unsigned integer.
76
- *
77
- * @readonly
76
+ * The smallest value representable as `Uint16` (the lower saturation target
77
+ * of `fromNumber`).
78
78
  */
79
79
  MIN_VALUE,
80
80
 
81
81
  /**
82
- * The maximum value for a 16-bit unsigned integer.
83
- *
84
- * @readonly
82
+ * The largest value representable as `Uint16` (the upper saturation target of
83
+ * `fromNumber`).
85
84
  */
86
85
  MAX_VALUE,
87
86
 
88
87
  /**
89
- * Returns the smaller of two Uint16 values.
88
+ * Returns the smallest of the given non-negative integers.
90
89
  *
91
- * @param a The first Uint16.
92
- * @param b The second Uint16.
93
- * @returns The minimum value as a Uint16.
90
+ * @param values - The non-negative integers to compare (at least one required)
91
+ * @returns The smallest value as an `Uint16`
94
92
  */
95
93
  min: min_,
96
94
 
97
95
  /**
98
- * Returns the larger of two Uint16 values.
96
+ * Returns the largest of the given non-negative integers.
99
97
  *
100
- * @param a The first Uint16.
101
- * @param b The second Uint16.
102
- * @returns The maximum value as a Uint16.
98
+ * @param values - The non-negative integers to compare (at least one required)
99
+ * @returns The largest value as an `Uint16`
103
100
  */
104
101
  max: max_,
105
102
 
106
103
  /**
107
- * Clamps a number to the Uint16 range.
104
+ * Converts an arbitrary `number` into an `Uint16`, rounding to the nearest
105
+ * integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
106
+ *
107
+ * Unlike `asUint16`, this is total: out-of-range inputs are clamped to the
108
+ * nearest representable `Uint16` instead of throwing.
108
109
  *
109
- * @param value The number to clamp.
110
- * @returns The value clamped to [0, 65535] as a Uint16.
110
+ * @param value - The value to convert
111
+ * @returns The value as an `Uint16`
111
112
  */
112
- clamp,
113
+ fromNumber,
113
114
 
114
115
  /**
115
- * Generates a random Uint16 value within the valid range.
116
+ * Generates a random `Uint16` within the given range.
117
+ *
118
+ * The range is inclusive on both ends.
116
119
  *
117
- * @returns A random Uint16 between 0 and 65535.
120
+ * @param min - The minimum value (inclusive)
121
+ * @param max - The maximum value (inclusive)
122
+ * @returns A random `Uint16` in `[min, max]`
118
123
  */
119
124
  random,
120
125
 
121
126
  /**
122
- * Raises a Uint16 to the power of another Uint16.
127
+ * Raises `a` to the power `b`, returning `a ** b` as an `Uint16` (floored to
128
+ * an integer).
123
129
  *
124
- * @param a The base Uint16.
125
- * @param b The exponent Uint16.
126
- * @returns `a ** b` clamped to [0, 65535] as a Uint16.
130
+ * @param a - The base non-negative integer
131
+ * @param b - The exponent non-negative integer
132
+ * @returns `a ** b` as an `Uint16`
127
133
  */
128
134
  pow,
129
135
 
130
136
  /**
131
- * Adds two Uint16 values.
137
+ * Adds two non-negative integers, returning `a + b` as an `Uint16`.
132
138
  *
133
- * @param a The first Uint16.
134
- * @param b The second Uint16.
135
- * @returns `a + b` clamped to [0, 65535] as a Uint16.
139
+ * @param a - The first non-negative integer
140
+ * @param b - The second non-negative integer
141
+ * @returns The sum of `a` and `b` as an `Uint16`
136
142
  */
137
143
  add,
138
144
 
139
145
  /**
140
- * Subtracts one Uint16 from another.
146
+ * Subtracts two non-negative integers, returning `a - b` as an `Uint16`.
141
147
  *
142
- * @param a The minuend Uint16.
143
- * @param b The subtrahend Uint16.
144
- * @returns `a - b` clamped to [0, 65535] as a Uint16 (minimum 0).
148
+ * @param a - The first non-negative integer
149
+ * @param b - The second non-negative integer
150
+ * @returns The difference of `a` and `b` as an `Uint16`
145
151
  */
146
152
  sub,
147
153
 
148
154
  /**
149
- * Multiplies two Uint16 values.
155
+ * Multiplies two non-negative integers, returning `a * b` as an `Uint16`.
150
156
  *
151
- * @param a The first Uint16.
152
- * @param b The second Uint16.
153
- * @returns `a * b` clamped to [0, 65535] as a Uint16.
157
+ * @param a - The first non-negative integer
158
+ * @param b - The second non-negative integer
159
+ * @returns The product of `a` and `b` as an `Uint16`
154
160
  */
155
161
  mul,
156
162
 
157
163
  /**
158
- * Divides one Uint16 by another using floor division.
164
+ * Divides two non-negative integers using floor division (`⌊a / b⌋`): the
165
+ * result is `a / b` rounded toward negative infinity, as an `Uint16`.
159
166
  *
160
- * @param a The dividend Uint16.
161
- * @param b The divisor Uint16.
162
- * @returns `⌊a / b⌋` clamped to [0, 65535] as a Uint16.
167
+ * @param a - The dividend
168
+ * @param b - The divisor (must be non-zero)
169
+ * @returns The floored quotient as an `Uint16`
163
170
  */
164
171
  div,
165
172
  } as const;