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,7 +12,7 @@ const {
12
12
  MAX_VALUE,
13
13
  add,
14
14
  castType,
15
- clamp,
15
+ fromNumber,
16
16
  is,
17
17
  max: max_,
18
18
  min: min_,
@@ -30,17 +30,8 @@ const {
30
30
  typeNameInMessage,
31
31
  } as const);
32
32
 
33
- /**
34
- * Returns the absolute value of a negative finite number.
35
- *
36
- * The absolute value of a negative finite number is always a positive finite
37
- * number, so the result is typed as a {@link PositiveFiniteNumber}.
38
- *
39
- * @param x - The negative finite number.
40
- * @returns `|x|` as a PositiveFiniteNumber.
41
- */
42
33
  const abs = (x: ElementType): PositiveFiniteNumber =>
43
- PositiveFiniteNumber.clamp(Math.abs(x));
34
+ PositiveFiniteNumber.fromNumber(Math.abs(x));
44
35
 
45
36
  const floor = (
46
37
  x: ElementType,
@@ -71,140 +62,156 @@ const round = (
71
62
  >;
72
63
 
73
64
  /**
74
- * Checks if a number is a NegativeFiniteNumber (a finite number `< 0`).
65
+ * Type guard that checks if a value is a negative finite number.
66
+ *
67
+ * Returns `true` only for a finite value — never `NaN`, `Infinity`, or
68
+ * `-Infinity`.
75
69
  *
76
- * @param value The value to check.
77
- * @returns `true` if the value is a NegativeFiniteNumber, `false` otherwise.
70
+ * @param value - The value to check
71
+ * @returns `true` if the value is a negative finite number, `false` otherwise
78
72
  */
79
73
  export const isNegativeFiniteNumber = is;
80
74
 
81
75
  /**
82
- * Casts a number to a NegativeFiniteNumber type.
76
+ * Casts a `number` to the `NegativeFiniteNumber` branded type.
77
+ *
78
+ * Validates that the value is a negative finite number and returns it with the
79
+ * `NegativeFiniteNumber` brand. Throws a `TypeError` otherwise.
83
80
  *
84
- * @param value The value to cast.
85
- * @returns The value as a NegativeFiniteNumber type.
86
- * @throws {TypeError} If the value is not a negative finite number.
81
+ * @param value - The value to cast
82
+ * @returns The value as a `NegativeFiniteNumber`
83
+ * @throws {TypeError} If the value is not a negative finite number
87
84
  */
88
85
  export const asNegativeFiniteNumber = castType;
89
86
 
90
87
  /**
91
- * Namespace providing type-safe arithmetic operations for negative finite
92
- * numbers.
88
+ * Namespace providing type-safe operations for the `NegativeFiniteNumber`
89
+ * branded type.
93
90
  *
94
- * All operations keep results finite (excluding NaN and Infinity). Operations
95
- * that stay within the negative finite numbers clamp their results to the valid
96
- * range, while operations whose result leaves the set (`mul`, `div`, `abs`) are
97
- * typed to reflect the actual sign of the result.
91
+ * The `NegativeFiniteNumber` type represents a negative finite number.
98
92
  */
99
93
  export const NegativeFiniteNumber = {
100
94
  /**
101
- * Type guard to check if a value is a NegativeFiniteNumber.
95
+ * Type guard that checks if a value is a negative finite number.
102
96
  *
103
- * @param value The value to check.
104
- * @returns `true` if the value is a negative finite number, `false`
105
- * otherwise.
97
+ * @param value - The value to check
98
+ * @returns `true` if the value is a negative finite number, `false` otherwise
99
+ * @see {@link isNegativeFiniteNumber} for usage examples
106
100
  */
107
101
  is,
108
102
 
109
103
  /**
110
- * The maximum value for a negative finite number.
111
- *
112
- * @readonly
104
+ * The largest value representable as `NegativeFiniteNumber` (the upper
105
+ * saturation target of `fromNumber`). The domain is open at `0` (excluded),
106
+ * so this is `-Number.MIN_VALUE` — the negative double closest to `0` —
107
+ * rather than `0`.
113
108
  */
114
109
  MAX_VALUE,
115
110
 
116
111
  /**
117
112
  * Returns the absolute value of a negative finite number.
118
113
  *
119
- * @param a The negative finite number.
120
- * @returns The absolute value as a PositiveFiniteNumber.
114
+ * The result is non-negative and keeps the `NegativeFiniteNumber` brand.
115
+ *
116
+ * @param a - The negative finite number value
117
+ * @returns The absolute value as a non-negative `NegativeFiniteNumber`
121
118
  */
122
119
  abs,
123
120
 
124
121
  /**
125
- * Returns the smaller of two NegativeFiniteNumber values.
122
+ * Returns the smallest of the given negative finite numbers.
126
123
  *
127
- * @param a The first NegativeFiniteNumber.
128
- * @param b The second NegativeFiniteNumber.
129
- * @returns The minimum value as a NegativeFiniteNumber.
124
+ * @param values - The negative finite numbers to compare (at least one required)
125
+ * @returns The smallest value as a `NegativeFiniteNumber`
130
126
  */
131
127
  min: min_,
132
128
 
133
129
  /**
134
- * Returns the larger of two NegativeFiniteNumber values.
130
+ * Returns the largest of the given negative finite numbers.
135
131
  *
136
- * @param a The first NegativeFiniteNumber.
137
- * @param b The second NegativeFiniteNumber.
138
- * @returns The maximum value as a NegativeFiniteNumber.
132
+ * @param values - The negative finite numbers to compare (at least one required)
133
+ * @returns The largest value as a `NegativeFiniteNumber`
139
134
  */
140
135
  max: max_,
141
136
 
142
137
  /**
143
- * Clamps a number to the negative finite range.
138
+ * Converts an arbitrary `number` into a `NegativeFiniteNumber`, saturating it
139
+ * into the range `[MIN_VALUE, MAX_VALUE]`.
140
+ *
141
+ * Unlike `asNegativeFiniteNumber`, this is total: out-of-range inputs are
142
+ * clamped to the nearest representable `NegativeFiniteNumber` instead of
143
+ * throwing.
144
144
  *
145
- * @param value The number to clamp.
146
- * @returns The value clamped to `(-∞, 0)` as a NegativeFiniteNumber.
145
+ * @param value - The value to convert
146
+ * @returns The value as a `NegativeFiniteNumber`
147
147
  */
148
- clamp,
148
+ fromNumber,
149
149
 
150
150
  /**
151
- * Rounds down a NegativeFiniteNumber to the nearest integer.
151
+ * Returns the greatest integer less than or equal to a negative finite
152
+ * number.
152
153
  *
153
- * @param x The NegativeFiniteNumber to round down.
154
- * @returns The floor value as a NegativeInt (always `<= -1`).
154
+ * @param a - The negative finite number value
155
+ * @returns The floored value as an integer
155
156
  */
156
157
  floor,
157
158
 
158
159
  /**
159
- * Rounds up a NegativeFiniteNumber to the nearest integer.
160
+ * Returns the smallest integer greater than or equal to a negative finite
161
+ * number.
160
162
  *
161
- * @param x The NegativeFiniteNumber to round up.
162
- * @returns The ceiling value as a NonPositiveInt (can be 0).
163
+ * @param a - The negative finite number value
164
+ * @returns The ceiled value as an integer
163
165
  */
164
166
  ceil,
165
167
 
166
168
  /**
167
- * Rounds a NegativeFiniteNumber to the nearest integer.
169
+ * Returns the value of a negative finite number rounded to the nearest
170
+ * integer.
168
171
  *
169
- * @param x The NegativeFiniteNumber to round.
170
- * @returns The rounded value as a NonPositiveInt (can be 0 if x > -0.5).
172
+ * @param a - The negative finite number value
173
+ * @returns The rounded value as an integer
171
174
  */
172
175
  round,
173
176
 
174
177
  /**
175
- * Generates a random NegativeFiniteNumber value.
178
+ * Generates a random `NegativeFiniteNumber` within the given range.
176
179
  *
177
- * @returns A random negative finite number.
180
+ * The range is inclusive on both ends.
181
+ *
182
+ * @param min - The minimum value (inclusive)
183
+ * @param max - The maximum value (inclusive)
184
+ * @returns A random `NegativeFiniteNumber` in `[min, max]`
178
185
  */
179
186
  random,
180
187
 
181
188
  /**
182
- * Raises a NegativeFiniteNumber to the power of another NegativeFiniteNumber.
189
+ * Raises `a` to the power `b`, returning `a ** b` as a
190
+ * `NegativeFiniteNumber`.
183
191
  *
184
- * @param a The base NegativeFiniteNumber.
185
- * @param b The exponent NegativeFiniteNumber.
186
- * @returns `a ** b` clamped to `(-∞, 0)` as a NegativeFiniteNumber.
192
+ * @param a - The base negative finite number
193
+ * @param b - The exponent negative finite number
194
+ * @returns `a ** b` as a `NegativeFiniteNumber`
187
195
  */
188
196
  pow,
189
197
 
190
198
  /**
191
- * Adds two NegativeFiniteNumber values.
199
+ * Adds two negative finite numbers, returning `a + b` as a
200
+ * `NegativeFiniteNumber`.
192
201
  *
193
- * @param a The first NegativeFiniteNumber.
194
- * @param b The second NegativeFiniteNumber.
195
- * @returns `a + b` as a NegativeFiniteNumber.
202
+ * @param a - The first negative finite number
203
+ * @param b - The second negative finite number
204
+ * @returns The sum of `a` and `b` as a `NegativeFiniteNumber`
196
205
  */
197
206
  add,
198
207
 
199
208
  /**
200
- * Subtracts one NegativeFiniteNumber from another.
201
- *
202
- * The mathematical result can be positive, so it is clamped to the negative
203
- * finite range (maximum `-Number.MIN_VALUE`).
209
+ * Subtracts two negative finite numbers, returning `a - b` as a
210
+ * `NegativeFiniteNumber`.
204
211
  *
205
- * @param a The minuend NegativeFiniteNumber.
206
- * @param b The subtrahend NegativeFiniteNumber.
207
- * @returns `a - b` clamped to `(-∞, 0)` as a NegativeFiniteNumber.
212
+ * @param a - The first negative finite number
213
+ * @param b - The second negative finite number
214
+ * @returns The difference of `a` and `b` as a `NegativeFiniteNumber`
208
215
  */
209
216
  sub,
210
217
  } as const;
@@ -93,13 +93,13 @@ describe('NegativeFiniteNumber test', () => {
93
93
  });
94
94
  });
95
95
 
96
- describe('NegativeFiniteNumber.clamp', () => {
96
+ describe('NegativeFiniteNumber.fromNumber', () => {
97
97
  test('clamps non-negative values into range', () => {
98
- expect(NegativeFiniteNumber.clamp(-5.5)).toBeCloseTo(-5.5, 5);
98
+ expect(NegativeFiniteNumber.fromNumber(-5.5)).toBeCloseTo(-5.5, 5);
99
99
 
100
- expect(NegativeFiniteNumber.clamp(0)).toBeLessThan(0);
100
+ expect(NegativeFiniteNumber.fromNumber(0)).toBeLessThan(0);
101
101
 
102
- expect(NegativeFiniteNumber.clamp(5)).toBeLessThan(0);
102
+ expect(NegativeFiniteNumber.fromNumber(5)).toBeLessThan(0);
103
103
  });
104
104
  });
105
105
 
@@ -15,7 +15,7 @@ const {
15
15
  MAX_VALUE,
16
16
  add,
17
17
  castType,
18
- clamp,
18
+ fromNumber,
19
19
  is,
20
20
  max: max_,
21
21
  min: min_,
@@ -34,127 +34,135 @@ const {
34
34
  typeNameInMessage,
35
35
  } as const);
36
36
 
37
- /**
38
- * Returns the absolute value of a negative integer.
39
- *
40
- * The absolute value of a negative integer is always a positive integer
41
- * (`>= 1`), so the result is typed as a {@link PositiveInt}.
42
- *
43
- * @param x - The negative integer.
44
- * @returns `|x|` as a PositiveInt.
45
- */
46
37
  const abs = (x: WithSmallInt<ElementType>): PositiveInt =>
47
- PositiveInt.clamp(Math.abs(x));
38
+ PositiveInt.fromNumber(Math.abs(x));
48
39
 
49
40
  /**
50
- * Checks if a number is a NegativeInt (a negative integer `<= -1`).
41
+ * Type guard that checks if a value is a negative integer.
42
+ *
43
+ * Returns `true` for a negative integer — a value with no fractional component,
44
+ * including values outside the safe integer range (unlike `SafeInt`).
51
45
  *
52
- * @param value The value to check.
53
- * @returns `true` if the value is a NegativeInt, `false` otherwise.
46
+ * @param value - The value to check
47
+ * @returns `true` if the value is a negative integer, `false` otherwise
54
48
  */
55
49
  export const isNegativeInt = is;
56
50
 
57
51
  /**
58
- * Casts a number to a NegativeInt type.
52
+ * Casts a `number` to the `NegativeInt` branded type.
53
+ *
54
+ * Validates that the value is a negative integer and returns it with the
55
+ * `NegativeInt` brand. Throws a `TypeError` otherwise.
59
56
  *
60
- * @param value The value to cast.
61
- * @returns The value as a NegativeInt type.
62
- * @throws {TypeError} If the value is not a negative integer.
57
+ * @param value - The value to cast
58
+ * @returns The value as a `NegativeInt`
59
+ * @throws {TypeError} If the value is not a negative integer
63
60
  */
64
61
  export const asNegativeInt = castType;
65
62
 
66
63
  /**
67
- * Namespace providing type-safe arithmetic operations for negative integers.
64
+ * Namespace providing type-safe operations for the `NegativeInt` branded type.
68
65
  *
69
- * NegativeInt represents integers that are strictly less than zero (`<= -1`).
70
- * Operations that stay within the negative integers (such as `add`) clamp their
71
- * results to the valid range, while operations whose result leaves the set
72
- * (`mul`, `div`, `abs`) are typed to reflect the actual sign of the result.
66
+ * The `NegativeInt` type represents a negative integer. Division (`div`) uses
67
+ * floor division.
68
+ *
69
+ * Unlike `SafeInt`, `NegativeInt` allows values outside the safe integer range
70
+ * (±2^53 − 1), so very large magnitudes may lose precision in JavaScript's
71
+ * `number` type.
73
72
  */
74
73
  export const NegativeInt = {
75
74
  /**
76
- * Type guard to check if a value is a NegativeInt.
75
+ * Type guard that checks if a value is a negative integer.
77
76
  *
78
- * @param value The value to check.
79
- * @returns `true` if the value is a negative integer, `false` otherwise.
77
+ * @param value - The value to check
78
+ * @returns `true` if the value is a negative integer, `false` otherwise
79
+ * @see {@link isNegativeInt} for usage examples
80
80
  */
81
81
  is,
82
82
 
83
83
  /**
84
- * The maximum value for a negative integer.
85
- *
86
- * @readonly
84
+ * The largest value representable as `NegativeInt` (the upper saturation
85
+ * target of `fromNumber`).
87
86
  */
88
87
  MAX_VALUE,
89
88
 
90
89
  /**
91
90
  * Returns the absolute value of a negative integer.
92
91
  *
93
- * @param a The negative integer.
94
- * @returns The absolute value as a PositiveInt.
92
+ * The result is non-negative and keeps the `NegativeInt` brand. Note that
93
+ * `Math.abs(Number.MIN_SAFE_INTEGER)` exceeds `Number.MAX_SAFE_INTEGER`, so
94
+ * use `SafeInt` for guaranteed precision.
95
+ *
96
+ * @param a - The negative integer value
97
+ * @returns The absolute value as a non-negative `NegativeInt`
95
98
  */
96
99
  abs,
97
100
 
98
101
  /**
99
- * Returns the smaller of two negative integers.
102
+ * Returns the smallest of the given negative integers.
100
103
  *
101
- * @param a The first negative integer.
102
- * @param b The second negative integer.
103
- * @returns The minimum value as a NegativeInt.
104
+ * @param values - The negative integers to compare (at least one required)
105
+ * @returns The smallest value as a `NegativeInt`
104
106
  */
105
107
  min: min_,
106
108
 
107
109
  /**
108
- * Returns the larger of two negative integers.
110
+ * Returns the largest of the given negative integers.
109
111
  *
110
- * @param a The first negative integer.
111
- * @param b The second negative integer.
112
- * @returns The maximum value as a NegativeInt.
112
+ * @param values - The negative integers to compare (at least one required)
113
+ * @returns The largest value as a `NegativeInt`
113
114
  */
114
115
  max: max_,
115
116
 
116
117
  /**
117
- * Clamps a number to the negative integer range.
118
+ * Converts an arbitrary `number` into a `NegativeInt`, rounding to the
119
+ * nearest integer and saturating the result into the range `[MIN_VALUE,
120
+ * MAX_VALUE]`.
118
121
  *
119
- * @param value The number to clamp.
120
- * @returns The value clamped to `[-∞, -1]` as a NegativeInt.
122
+ * Unlike `asNegativeInt`, this is total: out-of-range inputs are clamped to
123
+ * the nearest representable `NegativeInt` instead of throwing.
124
+ *
125
+ * @param value - The value to convert
126
+ * @returns The value as a `NegativeInt`
121
127
  */
122
- clamp,
128
+ fromNumber,
123
129
 
124
130
  /**
125
- * Generates a random NegativeInt value within the valid range.
131
+ * Generates a random `NegativeInt` within the given range.
132
+ *
133
+ * The range is inclusive on both ends.
126
134
  *
127
- * @returns A random negative integer.
135
+ * @param min - The minimum value (inclusive)
136
+ * @param max - The maximum value (inclusive)
137
+ * @returns A random `NegativeInt` in `[min, max]`
128
138
  */
129
139
  random,
130
140
 
131
141
  /**
132
- * Raises a negative integer to the power of another negative integer.
142
+ * Raises `a` to the power `b`, returning `a ** b` as a `NegativeInt` (floored
143
+ * to an integer).
133
144
  *
134
- * @param a The base negative integer.
135
- * @param b The exponent negative integer.
136
- * @returns `a ** b` clamped to the negative integer range.
145
+ * @param a - The base negative integer
146
+ * @param b - The exponent negative integer
147
+ * @returns `a ** b` as a `NegativeInt`
137
148
  */
138
149
  pow,
139
150
 
140
151
  /**
141
- * Adds two negative integers.
152
+ * Adds two negative integers, returning `a + b` as a `NegativeInt`.
142
153
  *
143
- * @param a The first negative integer.
144
- * @param b The second negative integer.
145
- * @returns `a + b` as a NegativeInt.
154
+ * @param a - The first negative integer
155
+ * @param b - The second negative integer
156
+ * @returns The sum of `a` and `b` as a `NegativeInt`
146
157
  */
147
158
  add,
148
159
 
149
160
  /**
150
- * Subtracts one negative integer from another.
151
- *
152
- * The mathematical result can be positive, so it is clamped to the negative
153
- * integer range (maximum `-1`).
161
+ * Subtracts two negative integers, returning `a - b` as a `NegativeInt`.
154
162
  *
155
- * @param a The minuend negative integer.
156
- * @param b The subtrahend negative integer.
157
- * @returns `a - b` clamped to the negative integer range.
163
+ * @param a - The first negative integer
164
+ * @param b - The second negative integer
165
+ * @returns The difference of `a` and `b` as a `NegativeInt`
158
166
  */
159
167
  sub,
160
168
  } as const;
@@ -96,17 +96,17 @@ describe('NegativeInt test', () => {
96
96
  });
97
97
  });
98
98
 
99
- describe('NegativeInt.clamp', () => {
99
+ describe('NegativeInt.fromNumber', () => {
100
100
  test('returns the same value if within range', () => {
101
- expect(NegativeInt.clamp(-5)).toBe(-5);
101
+ expect(NegativeInt.fromNumber(-5)).toBe(-5);
102
102
 
103
- expect(NegativeInt.clamp(-1000)).toBe(-1000);
103
+ expect(NegativeInt.fromNumber(-1000)).toBe(-1000);
104
104
  });
105
105
 
106
106
  test('clamps non-negative values to MAX_VALUE (-1)', () => {
107
- expect(NegativeInt.clamp(0)).toBe(-1);
107
+ expect(NegativeInt.fromNumber(0)).toBe(-1);
108
108
 
109
- expect(NegativeInt.clamp(5)).toBe(-1);
109
+ expect(NegativeInt.fromNumber(5)).toBe(-1);
110
110
  });
111
111
  });
112
112