ts-data-forge 11.0.1 → 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 (165) hide show
  1. package/README.md +2 -2
  2. package/dist/number/branded-types/int16.d.mts +12 -7
  3. package/dist/number/branded-types/int16.d.mts.map +1 -1
  4. package/dist/number/branded-types/int16.mjs +13 -8
  5. package/dist/number/branded-types/int16.mjs.map +1 -1
  6. package/dist/number/branded-types/int32.d.mts +12 -7
  7. package/dist/number/branded-types/int32.d.mts.map +1 -1
  8. package/dist/number/branded-types/int32.mjs +13 -8
  9. package/dist/number/branded-types/int32.mjs.map +1 -1
  10. package/dist/number/branded-types/negative-finite-number.d.mts +13 -6
  11. package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -1
  12. package/dist/number/branded-types/negative-finite-number.mjs +15 -8
  13. package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
  14. package/dist/number/branded-types/negative-int.d.mts +11 -6
  15. package/dist/number/branded-types/negative-int.d.mts.map +1 -1
  16. package/dist/number/branded-types/negative-int.mjs +13 -8
  17. package/dist/number/branded-types/negative-int.mjs.map +1 -1
  18. package/dist/number/branded-types/negative-safe-int.d.mts +13 -7
  19. package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -1
  20. package/dist/number/branded-types/negative-safe-int.mjs +15 -9
  21. package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
  22. package/dist/number/branded-types/non-negative-finite-number.d.mts +11 -6
  23. package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
  24. package/dist/number/branded-types/non-negative-finite-number.mjs +12 -7
  25. package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
  26. package/dist/number/branded-types/non-negative-int16.d.mts +13 -7
  27. package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
  28. package/dist/number/branded-types/non-negative-int16.mjs +14 -8
  29. package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
  30. package/dist/number/branded-types/non-negative-int32.d.mts +13 -7
  31. package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
  32. package/dist/number/branded-types/non-negative-int32.mjs +14 -8
  33. package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
  34. package/dist/number/branded-types/non-positive-finite-number.d.mts +11 -6
  35. package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -1
  36. package/dist/number/branded-types/non-positive-finite-number.mjs +12 -7
  37. package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
  38. package/dist/number/branded-types/non-positive-int.d.mts +11 -6
  39. package/dist/number/branded-types/non-positive-int.d.mts.map +1 -1
  40. package/dist/number/branded-types/non-positive-int.mjs +12 -7
  41. package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
  42. package/dist/number/branded-types/non-positive-safe-int.d.mts +14 -7
  43. package/dist/number/branded-types/non-positive-safe-int.d.mts.map +1 -1
  44. package/dist/number/branded-types/non-positive-safe-int.mjs +15 -8
  45. package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -1
  46. package/dist/number/branded-types/non-zero-int16.d.mts +13 -7
  47. package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
  48. package/dist/number/branded-types/non-zero-int16.mjs +14 -8
  49. package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
  50. package/dist/number/branded-types/non-zero-int32.d.mts +13 -7
  51. package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
  52. package/dist/number/branded-types/non-zero-int32.mjs +14 -8
  53. package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
  54. package/dist/number/branded-types/non-zero-safe-int.d.mts +13 -7
  55. package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
  56. package/dist/number/branded-types/non-zero-safe-int.mjs +14 -8
  57. package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
  58. package/dist/number/branded-types/non-zero-uint16.d.mts +13 -7
  59. package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
  60. package/dist/number/branded-types/non-zero-uint16.mjs +14 -8
  61. package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
  62. package/dist/number/branded-types/non-zero-uint32.d.mts +13 -7
  63. package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
  64. package/dist/number/branded-types/non-zero-uint32.mjs +14 -8
  65. package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
  66. package/dist/number/branded-types/positive-finite-number.d.mts +13 -6
  67. package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
  68. package/dist/number/branded-types/positive-finite-number.mjs +14 -7
  69. package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
  70. package/dist/number/branded-types/positive-int.d.mts +13 -8
  71. package/dist/number/branded-types/positive-int.d.mts.map +1 -1
  72. package/dist/number/branded-types/positive-int.mjs +14 -9
  73. package/dist/number/branded-types/positive-int.mjs.map +1 -1
  74. package/dist/number/branded-types/positive-int16.d.mts +13 -7
  75. package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
  76. package/dist/number/branded-types/positive-int16.mjs +14 -8
  77. package/dist/number/branded-types/positive-int16.mjs.map +1 -1
  78. package/dist/number/branded-types/positive-int32.d.mts +13 -7
  79. package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
  80. package/dist/number/branded-types/positive-int32.mjs +14 -8
  81. package/dist/number/branded-types/positive-int32.mjs.map +1 -1
  82. package/dist/number/branded-types/positive-safe-int.d.mts +16 -10
  83. package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
  84. package/dist/number/branded-types/positive-safe-int.mjs +17 -11
  85. package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
  86. package/dist/number/branded-types/positive-uint16.d.mts +13 -7
  87. package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
  88. package/dist/number/branded-types/positive-uint16.mjs +14 -8
  89. package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
  90. package/dist/number/branded-types/positive-uint32.d.mts +13 -7
  91. package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
  92. package/dist/number/branded-types/positive-uint32.mjs +14 -8
  93. package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
  94. package/dist/number/branded-types/safe-int.d.mts +15 -10
  95. package/dist/number/branded-types/safe-int.d.mts.map +1 -1
  96. package/dist/number/branded-types/safe-int.mjs +16 -11
  97. package/dist/number/branded-types/safe-int.mjs.map +1 -1
  98. package/dist/number/branded-types/safe-uint.d.mts +12 -7
  99. package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
  100. package/dist/number/branded-types/safe-uint.mjs +13 -8
  101. package/dist/number/branded-types/safe-uint.mjs.map +1 -1
  102. package/dist/number/branded-types/uint.d.mts +14 -10
  103. package/dist/number/branded-types/uint.d.mts.map +1 -1
  104. package/dist/number/branded-types/uint.mjs +15 -11
  105. package/dist/number/branded-types/uint.mjs.map +1 -1
  106. package/dist/number/branded-types/uint16.d.mts +12 -7
  107. package/dist/number/branded-types/uint16.d.mts.map +1 -1
  108. package/dist/number/branded-types/uint16.mjs +13 -8
  109. package/dist/number/branded-types/uint16.mjs.map +1 -1
  110. package/dist/number/branded-types/uint32.d.mts +12 -7
  111. package/dist/number/branded-types/uint32.d.mts.map +1 -1
  112. package/dist/number/branded-types/uint32.mjs +13 -8
  113. package/dist/number/branded-types/uint32.mjs.map +1 -1
  114. package/dist/number/enum/int8.d.mts +12 -7
  115. package/dist/number/enum/int8.d.mts.map +1 -1
  116. package/dist/number/enum/int8.mjs +19 -14
  117. package/dist/number/enum/int8.mjs.map +1 -1
  118. package/dist/number/enum/uint8.d.mts +12 -7
  119. package/dist/number/enum/uint8.d.mts.map +1 -1
  120. package/dist/number/enum/uint8.mjs +19 -14
  121. package/dist/number/enum/uint8.mjs.map +1 -1
  122. package/dist/number/refined-number-utils.d.mts +9 -9
  123. package/dist/number/refined-number-utils.d.mts.map +1 -1
  124. package/dist/number/refined-number-utils.mjs +7 -7
  125. package/dist/number/refined-number-utils.mjs.map +1 -1
  126. package/package.json +1 -1
  127. package/src/number/branded-types/int16.mts +13 -8
  128. package/src/number/branded-types/int32.mts +13 -8
  129. package/src/number/branded-types/negative-finite-number.mts +15 -8
  130. package/src/number/branded-types/negative-finite-number.test.mts +4 -4
  131. package/src/number/branded-types/negative-int.mts +13 -8
  132. package/src/number/branded-types/negative-int.test.mts +5 -5
  133. package/src/number/branded-types/negative-safe-int.mts +15 -9
  134. package/src/number/branded-types/negative-safe-int.test.mts +4 -4
  135. package/src/number/branded-types/non-negative-finite-number.mts +12 -7
  136. package/src/number/branded-types/non-negative-int16.mts +14 -8
  137. package/src/number/branded-types/non-negative-int32.mts +14 -8
  138. package/src/number/branded-types/non-positive-finite-number.mts +12 -7
  139. package/src/number/branded-types/non-positive-finite-number.test.mts +7 -7
  140. package/src/number/branded-types/non-positive-int.mts +12 -7
  141. package/src/number/branded-types/non-positive-int.test.mts +7 -7
  142. package/src/number/branded-types/non-positive-safe-int.mts +15 -8
  143. package/src/number/branded-types/non-positive-safe-int.test.mts +7 -7
  144. package/src/number/branded-types/non-zero-int16.mts +14 -8
  145. package/src/number/branded-types/non-zero-int32.mts +14 -8
  146. package/src/number/branded-types/non-zero-safe-int.mts +14 -8
  147. package/src/number/branded-types/non-zero-uint16.mts +14 -8
  148. package/src/number/branded-types/non-zero-uint32.mts +14 -8
  149. package/src/number/branded-types/positive-finite-number.mts +14 -7
  150. package/src/number/branded-types/positive-int.mts +14 -9
  151. package/src/number/branded-types/positive-int16.mts +14 -8
  152. package/src/number/branded-types/positive-int32.mts +14 -8
  153. package/src/number/branded-types/positive-safe-int.mts +17 -11
  154. package/src/number/branded-types/positive-uint16.mts +14 -8
  155. package/src/number/branded-types/positive-uint32.mts +14 -8
  156. package/src/number/branded-types/safe-int.mts +16 -11
  157. package/src/number/branded-types/safe-uint.mts +13 -8
  158. package/src/number/branded-types/uint.mts +15 -11
  159. package/src/number/branded-types/uint16.mts +13 -8
  160. package/src/number/branded-types/uint32.mts +13 -8
  161. package/src/number/enum/int8.mts +20 -14
  162. package/src/number/enum/int8.test.mts +7 -4
  163. package/src/number/enum/uint8.mts +20 -14
  164. package/src/number/enum/uint8.test.mts +7 -4
  165. package/src/number/refined-number-utils.mts +22 -19
@@ -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_,
@@ -74,12 +74,14 @@ export const PositiveInt16 = {
74
74
  is,
75
75
 
76
76
  /**
77
- * The smallest value representable as `PositiveInt16`.
77
+ * The smallest value representable as `PositiveInt16` (the lower saturation
78
+ * target of `fromNumber`).
78
79
  */
79
80
  MIN_VALUE,
80
81
 
81
82
  /**
82
- * The largest value representable as `PositiveInt16`.
83
+ * The largest value representable as `PositiveInt16` (the upper saturation
84
+ * target of `fromNumber`).
83
85
  */
84
86
  MAX_VALUE,
85
87
 
@@ -100,13 +102,17 @@ export const PositiveInt16 = {
100
102
  max: max_,
101
103
 
102
104
  /**
103
- * Clamps a `number` into the `PositiveInt16` range, rounding to the nearest
104
- * integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
105
+ * Converts an arbitrary `number` into a `PositiveInt16`, rounding to the
106
+ * nearest integer and saturating the result into the range `[MIN_VALUE,
107
+ * MAX_VALUE]`.
105
108
  *
106
- * @param value - The value to clamp
107
- * @returns The clamped value as a `PositiveInt16`
109
+ * Unlike `asPositiveInt16`, this is total: out-of-range inputs are clamped to
110
+ * the nearest representable `PositiveInt16` instead of throwing.
111
+ *
112
+ * @param value - The value to convert
113
+ * @returns The value as a `PositiveInt16`
108
114
  */
109
- clamp,
115
+ fromNumber,
110
116
 
111
117
  /**
112
118
  * Generates a random `PositiveInt16` within the given range.
@@ -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_,
@@ -74,12 +74,14 @@ export const PositiveInt32 = {
74
74
  is,
75
75
 
76
76
  /**
77
- * The smallest value representable as `PositiveInt32`.
77
+ * The smallest value representable as `PositiveInt32` (the lower saturation
78
+ * target of `fromNumber`).
78
79
  */
79
80
  MIN_VALUE,
80
81
 
81
82
  /**
82
- * The largest value representable as `PositiveInt32`.
83
+ * The largest value representable as `PositiveInt32` (the upper saturation
84
+ * target of `fromNumber`).
83
85
  */
84
86
  MAX_VALUE,
85
87
 
@@ -100,13 +102,17 @@ export const PositiveInt32 = {
100
102
  max: max_,
101
103
 
102
104
  /**
103
- * Clamps a `number` into the `PositiveInt32` range, rounding to the nearest
104
- * integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
105
+ * Converts an arbitrary `number` into a `PositiveInt32`, rounding to the
106
+ * nearest integer and saturating the result into the range `[MIN_VALUE,
107
+ * MAX_VALUE]`.
105
108
  *
106
- * @param value - The value to clamp
107
- * @returns The clamped value as a `PositiveInt32`
109
+ * Unlike `asPositiveInt32`, this is total: out-of-range inputs are clamped to
110
+ * the nearest representable `PositiveInt32` instead of throwing.
111
+ *
112
+ * @param value - The value to convert
113
+ * @returns The value as a `PositiveInt32`
108
114
  */
109
- clamp,
115
+ fromNumber,
110
116
 
111
117
  /**
112
118
  * Generates a random `PositiveInt32` within the given range.
@@ -16,8 +16,8 @@ const {
16
16
  MIN_VALUE,
17
17
  add,
18
18
  castType,
19
- clamp,
20
19
  div,
20
+ fromNumber,
21
21
  is,
22
22
  max: max_,
23
23
  min: min_,
@@ -112,12 +112,14 @@ export const PositiveSafeInt = {
112
112
  is,
113
113
 
114
114
  /**
115
- * The smallest value representable as `PositiveSafeInt`.
115
+ * The smallest value representable as `PositiveSafeInt` (the lower saturation
116
+ * target of `fromNumber`).
116
117
  */
117
118
  MIN_VALUE,
118
119
 
119
120
  /**
120
- * The largest value representable as `PositiveSafeInt`.
121
+ * The largest value representable as `PositiveSafeInt` (the upper saturation
122
+ * target of `fromNumber`).
121
123
  */
122
124
  MAX_VALUE,
123
125
 
@@ -160,17 +162,21 @@ export const PositiveSafeInt = {
160
162
  max: max_,
161
163
 
162
164
  /**
163
- * Clamps a `number` into the `PositiveSafeInt` range, rounding to the nearest
164
- * integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
165
+ * Converts an arbitrary `number` into a `PositiveSafeInt`, rounding to the
166
+ * nearest integer and saturating the result into the range `[MIN_VALUE,
167
+ * MAX_VALUE]`.
168
+ *
169
+ * Unlike `asPositiveSafeInt`, this is total: out-of-range inputs are clamped
170
+ * to the nearest representable `PositiveSafeInt` instead of throwing.
165
171
  *
166
172
  * @example
167
173
  *
168
174
  * ```ts
169
- * const belowRange = PositiveSafeInt.clamp(0);
175
+ * const belowRange = PositiveSafeInt.fromNumber(0);
170
176
  *
171
- * const withinRange = PositiveSafeInt.clamp(123);
177
+ * const withinRange = PositiveSafeInt.fromNumber(123);
172
178
  *
173
- * const aboveRange = PositiveSafeInt.clamp(Number.MAX_SAFE_INTEGER + 10);
179
+ * const aboveRange = PositiveSafeInt.fromNumber(Number.MAX_SAFE_INTEGER + 10);
174
180
  *
175
181
  * assert.isTrue(belowRange === 1);
176
182
  *
@@ -179,10 +185,10 @@ export const PositiveSafeInt = {
179
185
  * assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);
180
186
  * ```
181
187
  *
182
- * @param value - The value to clamp
183
- * @returns The clamped value as a `PositiveSafeInt`
188
+ * @param value - The value to convert
189
+ * @returns The value as a `PositiveSafeInt`
184
190
  */
185
- clamp,
191
+ fromNumber,
186
192
 
187
193
  /**
188
194
  * Generates a random `PositiveSafeInt` within the given range.
@@ -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_,
@@ -74,12 +74,14 @@ export const PositiveUint16 = {
74
74
  is,
75
75
 
76
76
  /**
77
- * The smallest value representable as `PositiveUint16`.
77
+ * The smallest value representable as `PositiveUint16` (the lower saturation
78
+ * target of `fromNumber`).
78
79
  */
79
80
  MIN_VALUE,
80
81
 
81
82
  /**
82
- * The largest value representable as `PositiveUint16`.
83
+ * The largest value representable as `PositiveUint16` (the upper saturation
84
+ * target of `fromNumber`).
83
85
  */
84
86
  MAX_VALUE,
85
87
 
@@ -100,13 +102,17 @@ export const PositiveUint16 = {
100
102
  max: max_,
101
103
 
102
104
  /**
103
- * Clamps a `number` into the `PositiveUint16` range, rounding to the nearest
104
- * integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
105
+ * Converts an arbitrary `number` into a `PositiveUint16`, rounding to the
106
+ * nearest integer and saturating the result into the range `[MIN_VALUE,
107
+ * MAX_VALUE]`.
105
108
  *
106
- * @param value - The value to clamp
107
- * @returns The clamped value as a `PositiveUint16`
109
+ * Unlike `asPositiveUint16`, this is total: out-of-range inputs are clamped
110
+ * to the nearest representable `PositiveUint16` instead of throwing.
111
+ *
112
+ * @param value - The value to convert
113
+ * @returns The value as a `PositiveUint16`
108
114
  */
109
- clamp,
115
+ fromNumber,
110
116
 
111
117
  /**
112
118
  * Generates a random `PositiveUint16` within the given range.
@@ -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_,
@@ -74,12 +74,14 @@ export const PositiveUint32 = {
74
74
  is,
75
75
 
76
76
  /**
77
- * The smallest value representable as `PositiveUint32`.
77
+ * The smallest value representable as `PositiveUint32` (the lower saturation
78
+ * target of `fromNumber`).
78
79
  */
79
80
  MIN_VALUE,
80
81
 
81
82
  /**
82
- * The largest value representable as `PositiveUint32`.
83
+ * The largest value representable as `PositiveUint32` (the upper saturation
84
+ * target of `fromNumber`).
83
85
  */
84
86
  MAX_VALUE,
85
87
 
@@ -100,13 +102,17 @@ export const PositiveUint32 = {
100
102
  max: max_,
101
103
 
102
104
  /**
103
- * Clamps a `number` into the `PositiveUint32` range, rounding to the nearest
104
- * integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
105
+ * Converts an arbitrary `number` into a `PositiveUint32`, rounding to the
106
+ * nearest integer and saturating the result into the range `[MIN_VALUE,
107
+ * MAX_VALUE]`.
105
108
  *
106
- * @param value - The value to clamp
107
- * @returns The clamped value as a `PositiveUint32`
109
+ * Unlike `asPositiveUint32`, this is total: out-of-range inputs are clamped
110
+ * to the nearest representable `PositiveUint32` instead of throwing.
111
+ *
112
+ * @param value - The value to convert
113
+ * @returns The value as a `PositiveUint32`
108
114
  */
109
- clamp,
115
+ fromNumber,
110
116
 
111
117
  /**
112
118
  * Generates a random `PositiveUint32` within the given range.
@@ -17,8 +17,8 @@ const {
17
17
  abs,
18
18
  add,
19
19
  castType,
20
- clamp,
21
20
  div,
21
+ fromNumber,
22
22
  is,
23
23
  max: max_,
24
24
  min: min_,
@@ -108,12 +108,14 @@ export const SafeInt = {
108
108
  is,
109
109
 
110
110
  /**
111
- * The smallest value representable as `SafeInt`.
111
+ * The smallest value representable as `SafeInt` (the lower saturation target
112
+ * of `fromNumber`).
112
113
  */
113
114
  MIN_VALUE,
114
115
 
115
116
  /**
116
- * The largest value representable as `SafeInt`.
117
+ * The largest value representable as `SafeInt` (the upper saturation target
118
+ * of `fromNumber`).
117
119
  */
118
120
  MAX_VALUE,
119
121
 
@@ -172,17 +174,20 @@ export const SafeInt = {
172
174
  max: max_,
173
175
 
174
176
  /**
175
- * Clamps a `number` into the `SafeInt` range, rounding to the nearest integer
176
- * and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
177
+ * Converts an arbitrary `number` into a `SafeInt`, rounding to the nearest
178
+ * integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
179
+ *
180
+ * Unlike `asSafeInt`, this is total: out-of-range inputs are clamped to the
181
+ * nearest representable `SafeInt` instead of throwing.
177
182
  *
178
183
  * @example
179
184
  *
180
185
  * ```ts
181
- * const aboveRange = SafeInt.clamp(1e20);
186
+ * const aboveRange = SafeInt.fromNumber(1e20);
182
187
  *
183
- * const withinRange = SafeInt.clamp(123);
188
+ * const withinRange = SafeInt.fromNumber(123);
184
189
  *
185
- * const belowRange = SafeInt.clamp(-1e20);
190
+ * const belowRange = SafeInt.fromNumber(-1e20);
186
191
  *
187
192
  * assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);
188
193
  *
@@ -191,10 +196,10 @@ export const SafeInt = {
191
196
  * assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);
192
197
  * ```
193
198
  *
194
- * @param value - The value to clamp
195
- * @returns The clamped value as a `SafeInt`
199
+ * @param value - The value to convert
200
+ * @returns The value as a `SafeInt`
196
201
  */
197
- clamp,
202
+ fromNumber,
198
203
 
199
204
  /**
200
205
  * Generates a random `SafeInt` within the given range.
@@ -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_,
@@ -74,12 +74,14 @@ export const SafeUint = {
74
74
  is,
75
75
 
76
76
  /**
77
- * The smallest value representable as `SafeUint`.
77
+ * The smallest value representable as `SafeUint` (the lower saturation target
78
+ * of `fromNumber`).
78
79
  */
79
80
  MIN_VALUE,
80
81
 
81
82
  /**
82
- * The largest value representable as `SafeUint`.
83
+ * The largest value representable as `SafeUint` (the upper saturation target
84
+ * of `fromNumber`).
83
85
  */
84
86
  MAX_VALUE,
85
87
 
@@ -100,13 +102,16 @@ export const SafeUint = {
100
102
  max: max_,
101
103
 
102
104
  /**
103
- * Clamps a `number` into the `SafeUint` range, rounding to the nearest
104
- * integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
105
+ * Converts an arbitrary `number` into a `SafeUint`, rounding to the nearest
106
+ * integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
105
107
  *
106
- * @param value - The value to clamp
107
- * @returns The clamped value as a `SafeUint`
108
+ * Unlike `asSafeUint`, this is total: out-of-range inputs are clamped to the
109
+ * nearest representable `SafeUint` instead of throwing.
110
+ *
111
+ * @param value - The value to convert
112
+ * @returns The value as a `SafeUint`
108
113
  */
109
- clamp,
114
+ fromNumber,
110
115
 
111
116
  /**
112
117
  * Generates a random `SafeUint` within the given range.
@@ -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_,
@@ -107,7 +107,8 @@ export const Uint = {
107
107
  is,
108
108
 
109
109
  /**
110
- * The smallest value representable as `Uint`.
110
+ * The smallest value representable as `Uint` (the lower saturation target of
111
+ * `fromNumber`).
111
112
  */
112
113
  MIN_VALUE,
113
114
 
@@ -144,25 +145,28 @@ export const Uint = {
144
145
  max: max_,
145
146
 
146
147
  /**
147
- * Clamps a `number` into the `Uint` range, rounding to the nearest integer
148
- * and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
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.
149
153
  *
150
154
  * @example
151
155
  *
152
156
  * ```ts
153
- * const clampedNegative = Uint.clamp(-5);
157
+ * const fromNegative = Uint.fromNumber(-5);
154
158
  *
155
- * const clampedPositive = Uint.clamp(42);
159
+ * const fromPositive = Uint.fromNumber(42);
156
160
  *
157
- * assert.isTrue(clampedNegative === 0);
161
+ * assert.isTrue(fromNegative === 0);
158
162
  *
159
- * assert.isTrue(clampedPositive === 42);
163
+ * assert.isTrue(fromPositive === 42);
160
164
  * ```
161
165
  *
162
- * @param value - The value to clamp
163
- * @returns The clamped value as an `Uint`
166
+ * @param value - The value to convert
167
+ * @returns The value as an `Uint`
164
168
  */
165
- clamp,
169
+ fromNumber,
166
170
 
167
171
  /**
168
172
  * Generates a random `Uint` within the given range.
@@ -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_,
@@ -73,12 +73,14 @@ export const Uint16 = {
73
73
  is,
74
74
 
75
75
  /**
76
- * The smallest value representable as `Uint16`.
76
+ * The smallest value representable as `Uint16` (the lower saturation target
77
+ * of `fromNumber`).
77
78
  */
78
79
  MIN_VALUE,
79
80
 
80
81
  /**
81
- * The largest value representable as `Uint16`.
82
+ * The largest value representable as `Uint16` (the upper saturation target of
83
+ * `fromNumber`).
82
84
  */
83
85
  MAX_VALUE,
84
86
 
@@ -99,13 +101,16 @@ export const Uint16 = {
99
101
  max: max_,
100
102
 
101
103
  /**
102
- * Clamps a `number` into the `Uint16` range, rounding to the nearest integer
103
- * and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
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]`.
104
106
  *
105
- * @param value - The value to clamp
106
- * @returns The clamped value as an `Uint16`
107
+ * Unlike `asUint16`, this is total: out-of-range inputs are clamped to the
108
+ * nearest representable `Uint16` instead of throwing.
109
+ *
110
+ * @param value - The value to convert
111
+ * @returns The value as an `Uint16`
107
112
  */
108
- clamp,
113
+ fromNumber,
109
114
 
110
115
  /**
111
116
  * Generates a random `Uint16` within the given range.
@@ -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_,
@@ -73,12 +73,14 @@ export const Uint32 = {
73
73
  is,
74
74
 
75
75
  /**
76
- * The smallest value representable as `Uint32`.
76
+ * The smallest value representable as `Uint32` (the lower saturation target
77
+ * of `fromNumber`).
77
78
  */
78
79
  MIN_VALUE,
79
80
 
80
81
  /**
81
- * The largest value representable as `Uint32`.
82
+ * The largest value representable as `Uint32` (the upper saturation target of
83
+ * `fromNumber`).
82
84
  */
83
85
  MAX_VALUE,
84
86
 
@@ -99,13 +101,16 @@ export const Uint32 = {
99
101
  max: max_,
100
102
 
101
103
  /**
102
- * Clamps a `number` into the `Uint32` range, rounding to the nearest integer
103
- * and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
104
+ * Converts an arbitrary `number` into an `Uint32`, rounding to the nearest
105
+ * integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
104
106
  *
105
- * @param value - The value to clamp
106
- * @returns The clamped value as an `Uint32`
107
+ * Unlike `asUint32`, this is total: out-of-range inputs are clamped to the
108
+ * nearest representable `Uint32` instead of throwing.
109
+ *
110
+ * @param value - The value to convert
111
+ * @returns The value as an `Uint32`
107
112
  */
108
- clamp,
113
+ fromNumber,
109
114
 
110
115
  /**
111
116
  * Generates a random `Uint32` within the given range.
@@ -14,7 +14,7 @@ const {
14
14
  MAX_VALUE,
15
15
  MIN_VALUE,
16
16
  castType: castTypeImpl,
17
- clamp: clampImpl,
17
+ fromNumber: fromNumberImpl,
18
18
  is: isImpl,
19
19
  random: randomImpl,
20
20
  } = TsDataForgeInternals.RefinedNumberUtils.operatorsForInteger<
@@ -34,7 +34,7 @@ const castType = (x: number): Int8 =>
34
34
  // eslint-disable-next-line total-functions/no-unsafe-type-assertion
35
35
  castTypeImpl(x) as Int8;
36
36
 
37
- const clamp = (a: number): Int8 => castType(clampImpl(a));
37
+ const fromNumber = (a: number): Int8 => castType(fromNumberImpl(a));
38
38
 
39
39
  const abs = <N extends Int8>(x: N): AbsoluteValue<N> =>
40
40
  // eslint-disable-next-line total-functions/no-unsafe-type-assertion
@@ -46,15 +46,16 @@ const min_ = (...values: readonly Int8[]): Int8 =>
46
46
  const max_ = (...values: readonly Int8[]): Int8 =>
47
47
  castType(Math.max(...values));
48
48
 
49
- const pow = (x: Int8, y: Int8): Int8 => clamp(x ** y);
49
+ const pow = (x: Int8, y: Int8): Int8 => fromNumber(x ** y);
50
50
 
51
- const add = (x: Int8, y: Int8): Int8 => clamp(x + y);
51
+ const add = (x: Int8, y: Int8): Int8 => fromNumber(x + y);
52
52
 
53
- const sub = (x: Int8, y: Int8): Int8 => clamp(x - y);
53
+ const sub = (x: Int8, y: Int8): Int8 => fromNumber(x - y);
54
54
 
55
- const mul = (x: Int8, y: Int8): Int8 => clamp(x * y);
55
+ const mul = (x: Int8, y: Int8): Int8 => fromNumber(x * y);
56
56
 
57
- const div = (x: Int8, y: Exclude<Int8, 0>): Int8 => clamp(Math.floor(x / y));
57
+ const div = (x: Int8, y: Exclude<Int8, 0>): Int8 =>
58
+ fromNumber(Math.floor(x / y));
58
59
 
59
60
  const random = (min: Int8, max: Int8): Int8 =>
60
61
  castType(randomImpl(castTypeImpl(min), castTypeImpl(max)));
@@ -99,12 +100,14 @@ export const Int8 = {
99
100
  is,
100
101
 
101
102
  /**
102
- * The smallest value representable as `Int8`.
103
+ * The smallest value representable as `Int8` (the lower saturation target of
104
+ * `fromNumber`).
103
105
  */
104
106
  MIN_VALUE,
105
107
 
106
108
  /**
107
- * The largest value representable as `Int8`.
109
+ * The largest value representable as `Int8` (the upper saturation target of
110
+ * `fromNumber`).
108
111
  */
109
112
  MAX_VALUE,
110
113
 
@@ -125,13 +128,16 @@ export const Int8 = {
125
128
  max: max_,
126
129
 
127
130
  /**
128
- * Clamps a `number` into the `Int8` range, rounding to the nearest integer
129
- * and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
131
+ * Converts an arbitrary `number` into an `Int8`, rounding to the nearest
132
+ * integer and saturating the result into the range `[MIN_VALUE, MAX_VALUE]`.
130
133
  *
131
- * @param value - The value to clamp
132
- * @returns The clamped value as an `Int8`
134
+ * Unlike `asInt8`, this is total: out-of-range inputs are clamped to the
135
+ * nearest representable `Int8` instead of throwing.
136
+ *
137
+ * @param value - The value to convert
138
+ * @returns The value as an `Int8`
133
139
  */
134
- clamp,
140
+ fromNumber,
135
141
 
136
142
  /**
137
143
  * Returns the absolute value of an integer in [-128, 127].
@@ -61,7 +61,7 @@ describe('Int8 test', () => {
61
61
  });
62
62
  });
63
63
 
64
- describe('Int8.clamp', () => {
64
+ describe('Int8.fromNumber', () => {
65
65
  test.each([
66
66
  { value: -200, expected: -128 },
67
67
  { value: -128, expected: -128 },
@@ -69,9 +69,12 @@ describe('Int8 test', () => {
69
69
  { value: 127, expected: 127 },
70
70
  { value: 200, expected: 127 },
71
71
  { value: 1.5, expected: 2 },
72
- ])('Int8.clamp($value) should return $expected', ({ expected, value }) => {
73
- expect(Int8.clamp(value)).toBe(expected);
74
- });
72
+ ])(
73
+ 'Int8.fromNumber($value) should return $expected',
74
+ ({ expected, value }) => {
75
+ expect(Int8.fromNumber(value)).toBe(expected);
76
+ },
77
+ );
75
78
  });
76
79
 
77
80
  describe('Int8.min', () => {