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
@@ -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,130 +34,139 @@ const {
34
34
  } as const);
35
35
 
36
36
  /**
37
- * Checks if a number is a Uint32 (32-bit unsigned integer in the range [0,
38
- * 2^32)).
37
+ * Type guard that checks if a value is a non-negative integer less than 2^32.
39
38
  *
40
- * @param value The value to check.
41
- * @returns `true` if the value is a Uint32, `false` otherwise.
39
+ * Returns `true` for a non-negative integer less than 2^32 — 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^32, `false` otherwise
42
44
  */
43
45
  export const isUint32 = is;
44
46
 
45
47
  /**
46
- * Casts a number to a Uint32 type.
48
+ * Casts a `number` to the `Uint32` branded type.
49
+ *
50
+ * Validates that the value is a non-negative integer less than 2^32 and returns
51
+ * it with the `Uint32` brand. Throws a `TypeError` otherwise.
47
52
  *
48
- * @param value The value to cast.
49
- * @returns The value as a Uint32 type.
50
- * @throws {TypeError} If the value is not a non-negative integer less than
51
- * 2^32.
53
+ * @param value - The value to cast
54
+ * @returns The value as an `Uint32`
55
+ * @throws {TypeError} If the value is not a non-negative integer less than 2^32
52
56
  */
53
57
  export const asUint32 = castType;
54
58
 
55
59
  /**
56
- * Utility functions for working with Uint32 (32-bit unsigned integer) branded
57
- * types. Provides type-safe operations that ensure results remain within the
58
- * valid range [0, 2^32). All arithmetic operations are clamped to maintain the
59
- * Uint32 constraint.
60
+ * Namespace providing type-safe operations for the `Uint32` branded type.
61
+ *
62
+ * The `Uint32` type represents a non-negative integer less than 2^32. Division
63
+ * (`div`) uses floor division.
60
64
  */
61
65
  export const Uint32 = {
62
66
  /**
63
- * Type guard that checks if a value is a 32-bit unsigned integer.
67
+ * Type guard that checks if a value is a non-negative integer less than 2^32.
64
68
  *
65
69
  * @param value - The value to check
66
- * @returns `true` if the value is within the range [0, 2^32), `false`
67
- * otherwise
70
+ * @returns `true` if the value is a non-negative integer less than 2^32, `false` otherwise
71
+ * @see {@link isUint32} for usage examples
68
72
  */
69
73
  is,
70
74
 
71
75
  /**
72
- * The minimum value for a Uint32.
73
- *
74
- * @readonly
76
+ * The smallest value representable as `Uint32` (the lower saturation target
77
+ * of `fromNumber`).
75
78
  */
76
79
  MIN_VALUE,
77
80
 
78
81
  /**
79
- * The maximum value for a Uint32.
80
- *
81
- * @readonly
82
+ * The largest value representable as `Uint32` (the upper saturation target of
83
+ * `fromNumber`).
82
84
  */
83
85
  MAX_VALUE,
84
86
 
85
87
  /**
86
- * Returns the minimum of multiple Uint32 values.
88
+ * Returns the smallest of the given non-negative integers.
87
89
  *
88
- * @param values - The Uint32 values to compare
89
- * @returns The smallest value as a Uint32
90
+ * @param values - The non-negative integers to compare (at least one required)
91
+ * @returns The smallest value as an `Uint32`
90
92
  */
91
93
  min: min_,
92
94
 
93
95
  /**
94
- * Returns the maximum of multiple Uint32 values.
96
+ * Returns the largest of the given non-negative integers.
95
97
  *
96
- * @param values - The Uint32 values to compare
97
- * @returns The largest value as a Uint32
98
+ * @param values - The non-negative integers to compare (at least one required)
99
+ * @returns The largest value as an `Uint32`
98
100
  */
99
101
  max: max_,
100
102
 
101
103
  /**
102
- * Clamps a Uint32 to be within the specified range.
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]`.
106
+ *
107
+ * Unlike `asUint32`, this is total: out-of-range inputs are clamped to the
108
+ * nearest representable `Uint32` instead of throwing.
103
109
  *
104
- * @param value - The value to clamp
105
- * @param min - The minimum value
106
- * @param max - The maximum value
107
- * @returns The clamped value as a Uint32
110
+ * @param value - The value to convert
111
+ * @returns The value as an `Uint32`
108
112
  */
109
- clamp,
113
+ fromNumber,
110
114
 
111
115
  /**
112
- * Generates a random Uint32 value.
116
+ * Generates a random `Uint32` within the given range.
117
+ *
118
+ * The range is inclusive on both ends.
113
119
  *
114
- * @returns A random Uint32 value within [0, 2^32)
120
+ * @param min - The minimum value (inclusive)
121
+ * @param max - The maximum value (inclusive)
122
+ * @returns A random `Uint32` in `[min, max]`
115
123
  */
116
124
  random,
117
125
 
118
126
  /**
119
- * Raises a Uint32 to a power, with result clamped to [0, 2^32).
127
+ * Raises `a` to the power `b`, returning `a ** b` as an `Uint32` (floored to
128
+ * an integer).
120
129
  *
121
- * @param a - The base Uint32
122
- * @param b - The exponent Uint32
123
- * @returns `a ** b` as a Uint32, clamped to valid range
130
+ * @param a - The base non-negative integer
131
+ * @param b - The exponent non-negative integer
132
+ * @returns `a ** b` as an `Uint32`
124
133
  */
125
134
  pow,
126
135
 
127
136
  /**
128
- * Adds two Uint32 values, with result clamped to [0, 2^32).
137
+ * Adds two non-negative integers, returning `a + b` as an `Uint32`.
129
138
  *
130
- * @param a - First Uint32
131
- * @param b - Second Uint32
132
- * @returns `a + b` as a Uint32, clamped to valid range
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 `Uint32`
133
142
  */
134
143
  add,
135
144
 
136
145
  /**
137
- * Subtracts two Uint32 values, with result clamped to [0, 2^32).
146
+ * Subtracts two non-negative integers, returning `a - b` as an `Uint32`.
138
147
  *
139
- * @param a - First Uint32
140
- * @param b - Second Uint32
141
- * @returns `a - b` as a Uint32, clamped to valid range (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 `Uint32`
142
151
  */
143
152
  sub,
144
153
 
145
154
  /**
146
- * Multiplies two Uint32 values, with result clamped to [0, 2^32).
155
+ * Multiplies two non-negative integers, returning `a * b` as an `Uint32`.
147
156
  *
148
- * @param a - First Uint32
149
- * @param b - Second Uint32
150
- * @returns `a * b` as a Uint32, clamped to valid range
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 `Uint32`
151
160
  */
152
161
  mul,
153
162
 
154
163
  /**
155
- * Divides two Uint32 values using floor division, with result clamped to [0,
156
- * 2^32).
164
+ * Divides two non-negative integers using floor division (`⌊a / b⌋`): the
165
+ * result is `a / b` rounded toward negative infinity, as an `Uint32`.
157
166
  *
158
- * @param a - The dividend Uint32
159
- * @param b - The divisor Uint32
160
- * @returns `⌊a / b⌋` as a Uint32, clamped to valid range
167
+ * @param a - The dividend
168
+ * @param b - The divisor (must be non-zero)
169
+ * @returns The floored quotient as an `Uint32`
161
170
  */
162
171
  div,
163
172
  } as const;
@@ -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,167 +46,164 @@ 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)));
61
62
 
62
63
  /**
63
- * Type guard that checks if a value is an 8-bit signed integer.
64
+ * Type guard that checks if a value is an integer in [-128, 127].
64
65
  *
65
- * An Int8 is a signed integer in the range [-128, 127], representing values
66
- * that fit in exactly 8 bits of memory.
66
+ * Returns `true` for an integer in [-128, 127] — a value with no fractional
67
+ * component.
67
68
  *
68
69
  * @param value - The value to check
69
- * @returns `true` if the value is an Int8, `false` otherwise
70
+ * @returns `true` if the value is an integer in [-128, 127], `false` otherwise
70
71
  */
71
72
  export const isInt8 = is;
72
73
 
73
74
  /**
74
- * Casts a number to an Int8 branded type.
75
+ * Casts a `number` to the `Int8` branded type.
75
76
  *
76
- * This function validates that the input is within the Int8 range [-128, 127]
77
- * and is an integer, then returns it with the Int8 brand.
77
+ * Validates that the value is an integer in [-128, 127] and returns it with the
78
+ * `Int8` brand. Throws a `TypeError` otherwise.
78
79
  *
79
80
  * @param value - The value to cast
80
- * @returns The value as an Int8 branded type
81
- * @throws {TypeError} If the value is not a valid 8-bit signed integer
81
+ * @returns The value as an `Int8`
82
+ * @throws {TypeError} If the value is not an integer in [-128, 127]
82
83
  */
83
84
  export const asInt8 = castType;
84
85
 
85
86
  /**
86
- * Namespace providing type-safe operations for Int8 (8-bit signed integer)
87
- * branded types.
87
+ * Namespace providing type-safe operations for the `Int8` branded type.
88
88
  *
89
- * Int8 represents signed integers in the range [-128, 127], equivalent to a
90
- * signed byte in many programming languages. All operations automatically clamp
91
- * results to stay within this range, preventing overflow/underflow issues.
92
- *
93
- * This type is useful for:
94
- *
95
- * - Binary data processing (signed bytes)
96
- * - Small integer values with known bounds
97
- * - Embedded systems programming
98
- * - Memory-efficient integer storage
99
- * - Image processing (signed pixel offsets)
89
+ * The `Int8` type represents an integer in [-128, 127]. Division (`div`) uses
90
+ * floor division.
100
91
  */
101
92
  export const Int8 = {
102
93
  /**
103
- * Type guard that checks if a value is an 8-bit signed integer.
94
+ * Type guard that checks if a value is an integer in [-128, 127].
104
95
  *
105
96
  * @param value - The value to check
106
- * @returns `true` if the value is in range [-128, 127] and is an integer
97
+ * @returns `true` if the value is an integer in [-128, 127], `false` otherwise
107
98
  * @see {@link isInt8} for usage examples
108
99
  */
109
100
  is,
110
101
 
111
102
  /**
112
- * The minimum value for an 8-bit signed integer.
113
- *
114
- * @readonly
103
+ * The smallest value representable as `Int8` (the lower saturation target of
104
+ * `fromNumber`).
115
105
  */
116
106
  MIN_VALUE,
117
107
 
118
108
  /**
119
- * The maximum value for an 8-bit signed integer.
120
- *
121
- * @readonly
109
+ * The largest value representable as `Int8` (the upper saturation target of
110
+ * `fromNumber`).
122
111
  */
123
112
  MAX_VALUE,
124
113
 
125
114
  /**
126
- * Returns the minimum value from a list of Int8 values.
115
+ * Returns the smallest of the given integers.
127
116
  *
128
- * @param values - The Int8 values to compare (at least one required)
129
- * @returns The smallest value as an Int8
117
+ * @param values - The integers to compare (at least one required)
118
+ * @returns The smallest value as an `Int8`
130
119
  */
131
120
  min: min_,
132
121
 
133
122
  /**
134
- * Returns the maximum value from a list of Int8 values.
123
+ * Returns the largest of the given integers.
135
124
  *
136
- * @param values - The Int8 values to compare (at least one required)
137
- * @returns The largest value as an Int8
125
+ * @param values - The integers to compare (at least one required)
126
+ * @returns The largest value as an `Int8`
138
127
  */
139
128
  max: max_,
140
129
 
141
130
  /**
142
- * Clamps a number to the Int8 range.
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]`.
133
+ *
134
+ * Unlike `asInt8`, this is total: out-of-range inputs are clamped to the
135
+ * nearest representable `Int8` instead of throwing.
143
136
  *
144
- * @param value The number to clamp.
145
- * @returns The value clamped to [-128, 127] as an Int8.
137
+ * @param value - The value to convert
138
+ * @returns The value as an `Int8`
146
139
  */
147
- clamp,
140
+ fromNumber,
148
141
 
149
142
  /**
150
- * Returns the absolute value of an Int8.
143
+ * Returns the absolute value of an integer in [-128, 127].
144
+ *
145
+ * The result is non-negative and keeps the `Int8` brand.
151
146
  *
152
- * @param value The Int8 value.
153
- * @returns The absolute value as an Int8, clamped to valid range.
147
+ * @param a - The integer value
148
+ * @returns The absolute value as a non-negative `Int8`
154
149
  */
155
150
  abs,
156
151
 
157
152
  /**
158
- * Generates a random Int8 value within the specified range (inclusive).
153
+ * Generates a random `Int8` within the given range.
159
154
  *
160
- * Both bounds are inclusive. If min > max, they are automatically swapped.
155
+ * The range is inclusive on both ends.
161
156
  *
162
157
  * @param min - The minimum value (inclusive)
163
158
  * @param max - The maximum value (inclusive)
164
- * @returns A random Int8 in the range [min, max]
159
+ * @returns A random `Int8` in `[min, max]`
165
160
  */
166
161
  random,
167
162
 
168
163
  /**
169
- * Raises x to the power of y, clamped to Int8 range.
164
+ * Raises `a` to the power `b`, returning `a ** b` as an `Int8` (floored to an
165
+ * integer).
170
166
  *
171
- * @param x - The base
172
- * @param y - The exponent
173
- * @returns `x ** y` clamped to [-128, 127]
167
+ * @param a - The base integer
168
+ * @param b - The exponent integer
169
+ * @returns `a ** b` as an `Int8`
174
170
  */
175
171
  pow,
176
172
 
177
173
  /**
178
- * Adds two Int8 values, clamped to Int8 range.
174
+ * Adds two integers, returning `a + b` as an `Int8`.
179
175
  *
180
- * @param x - First operand
181
- * @param y - Second operand
182
- * @returns `x + y` clamped to [-128, 127]
176
+ * @param a - The first integer
177
+ * @param b - The second integer
178
+ * @returns The sum of `a` and `b` as an `Int8`
183
179
  */
184
180
  add,
185
181
 
186
182
  /**
187
- * Subtracts two Int8 values, clamped to Int8 range.
183
+ * Subtracts two integers, returning `a - b` as an `Int8`.
188
184
  *
189
- * @param x - First operand
190
- * @param y - Second operand
191
- * @returns `x - y` clamped to [-128, 127]
185
+ * @param a - The first integer
186
+ * @param b - The second integer
187
+ * @returns The difference of `a` and `b` as an `Int8`
192
188
  */
193
189
  sub,
194
190
 
195
191
  /**
196
- * Multiplies two Int8 values, clamped to Int8 range.
192
+ * Multiplies two integers, returning `a * b` as an `Int8`.
197
193
  *
198
- * @param x - First operand
199
- * @param y - Second operand
200
- * @returns `x * y` clamped to [-128, 127]
194
+ * @param a - The first integer
195
+ * @param b - The second integer
196
+ * @returns The product of `a` and `b` as an `Int8`
201
197
  */
202
198
  mul,
203
199
 
204
200
  /**
205
- * Divides two Int8 values, clamped to Int8 range.
201
+ * Divides two integers using floor division (`⌊a / b⌋`): the result is `a /
202
+ * b` rounded toward negative infinity, as an `Int8`.
206
203
  *
207
- * @param x - The dividend
208
- * @param y - The divisor (cannot be 0)
209
- * @returns `⌊x / y⌋` clamped to [-128, 127]
204
+ * @param a - The dividend
205
+ * @param b - The divisor (must be non-zero)
206
+ * @returns The floored quotient as an `Int8`
210
207
  */
211
208
  div,
212
209
  } as const;
@@ -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', () => {