ts-data-forge 1.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.
- package/LICENSE +201 -0
- package/README.md +534 -0
- package/package.json +101 -0
- package/src/array/array-utils-creation.test.mts +443 -0
- package/src/array/array-utils-modification.test.mts +197 -0
- package/src/array/array-utils-overload-type-error.test.mts +149 -0
- package/src/array/array-utils-reducing-value.test.mts +425 -0
- package/src/array/array-utils-search.test.mts +169 -0
- package/src/array/array-utils-set-op.test.mts +335 -0
- package/src/array/array-utils-slice-clamped.test.mts +113 -0
- package/src/array/array-utils-slicing.test.mts +316 -0
- package/src/array/array-utils-transformation.test.mts +790 -0
- package/src/array/array-utils-validation.test.mts +492 -0
- package/src/array/array-utils.mts +4000 -0
- package/src/array/array.test.mts +146 -0
- package/src/array/index.mts +2 -0
- package/src/array/tuple-utils.mts +519 -0
- package/src/array/tuple-utils.test.mts +518 -0
- package/src/collections/imap-mapped.mts +801 -0
- package/src/collections/imap-mapped.test.mts +860 -0
- package/src/collections/imap.mts +651 -0
- package/src/collections/imap.test.mts +932 -0
- package/src/collections/index.mts +6 -0
- package/src/collections/iset-mapped.mts +889 -0
- package/src/collections/iset-mapped.test.mts +1187 -0
- package/src/collections/iset.mts +682 -0
- package/src/collections/iset.test.mts +1084 -0
- package/src/collections/queue.mts +390 -0
- package/src/collections/queue.test.mts +282 -0
- package/src/collections/stack.mts +423 -0
- package/src/collections/stack.test.mts +225 -0
- package/src/expect-type.mts +206 -0
- package/src/functional/index.mts +4 -0
- package/src/functional/match.mts +300 -0
- package/src/functional/match.test.mts +177 -0
- package/src/functional/optional.mts +733 -0
- package/src/functional/optional.test.mts +619 -0
- package/src/functional/pipe.mts +212 -0
- package/src/functional/pipe.test.mts +85 -0
- package/src/functional/result.mts +1134 -0
- package/src/functional/result.test.mts +777 -0
- package/src/globals.d.mts +38 -0
- package/src/guard/has-key.mts +119 -0
- package/src/guard/has-key.test.mts +219 -0
- package/src/guard/index.mts +7 -0
- package/src/guard/is-non-empty-string.mts +108 -0
- package/src/guard/is-non-empty-string.test.mts +91 -0
- package/src/guard/is-non-null-object.mts +106 -0
- package/src/guard/is-non-null-object.test.mts +90 -0
- package/src/guard/is-primitive.mts +165 -0
- package/src/guard/is-primitive.test.mts +102 -0
- package/src/guard/is-record.mts +153 -0
- package/src/guard/is-record.test.mts +112 -0
- package/src/guard/is-type.mts +450 -0
- package/src/guard/is-type.test.mts +496 -0
- package/src/guard/key-is-in.mts +163 -0
- package/src/guard/key-is-in.test.mts +19 -0
- package/src/index.mts +10 -0
- package/src/iterator/index.mts +1 -0
- package/src/iterator/range.mts +120 -0
- package/src/iterator/range.test.mts +33 -0
- package/src/json/index.mts +1 -0
- package/src/json/json.mts +711 -0
- package/src/json/json.test.mts +628 -0
- package/src/number/branded-types/finite-number.mts +354 -0
- package/src/number/branded-types/finite-number.test.mts +135 -0
- package/src/number/branded-types/index.mts +26 -0
- package/src/number/branded-types/int.mts +278 -0
- package/src/number/branded-types/int.test.mts +140 -0
- package/src/number/branded-types/int16.mts +192 -0
- package/src/number/branded-types/int16.test.mts +170 -0
- package/src/number/branded-types/int32.mts +193 -0
- package/src/number/branded-types/int32.test.mts +170 -0
- package/src/number/branded-types/non-negative-finite-number.mts +223 -0
- package/src/number/branded-types/non-negative-finite-number.test.mts +188 -0
- package/src/number/branded-types/non-negative-int16.mts +187 -0
- package/src/number/branded-types/non-negative-int16.test.mts +201 -0
- package/src/number/branded-types/non-negative-int32.mts +187 -0
- package/src/number/branded-types/non-negative-int32.test.mts +204 -0
- package/src/number/branded-types/non-zero-finite-number.mts +229 -0
- package/src/number/branded-types/non-zero-finite-number.test.mts +198 -0
- package/src/number/branded-types/non-zero-int.mts +167 -0
- package/src/number/branded-types/non-zero-int.test.mts +177 -0
- package/src/number/branded-types/non-zero-int16.mts +196 -0
- package/src/number/branded-types/non-zero-int16.test.mts +195 -0
- package/src/number/branded-types/non-zero-int32.mts +196 -0
- package/src/number/branded-types/non-zero-int32.test.mts +197 -0
- package/src/number/branded-types/non-zero-safe-int.mts +196 -0
- package/src/number/branded-types/non-zero-safe-int.test.mts +232 -0
- package/src/number/branded-types/non-zero-uint16.mts +189 -0
- package/src/number/branded-types/non-zero-uint16.test.mts +199 -0
- package/src/number/branded-types/non-zero-uint32.mts +189 -0
- package/src/number/branded-types/non-zero-uint32.test.mts +199 -0
- package/src/number/branded-types/positive-finite-number.mts +241 -0
- package/src/number/branded-types/positive-finite-number.test.mts +204 -0
- package/src/number/branded-types/positive-int.mts +304 -0
- package/src/number/branded-types/positive-int.test.mts +176 -0
- package/src/number/branded-types/positive-int16.mts +188 -0
- package/src/number/branded-types/positive-int16.test.mts +197 -0
- package/src/number/branded-types/positive-int32.mts +188 -0
- package/src/number/branded-types/positive-int32.test.mts +197 -0
- package/src/number/branded-types/positive-safe-int.mts +187 -0
- package/src/number/branded-types/positive-safe-int.test.mts +210 -0
- package/src/number/branded-types/positive-uint16.mts +188 -0
- package/src/number/branded-types/positive-uint16.test.mts +203 -0
- package/src/number/branded-types/positive-uint32.mts +188 -0
- package/src/number/branded-types/positive-uint32.test.mts +203 -0
- package/src/number/branded-types/safe-int.mts +291 -0
- package/src/number/branded-types/safe-int.test.mts +170 -0
- package/src/number/branded-types/safe-uint.mts +187 -0
- package/src/number/branded-types/safe-uint.test.mts +176 -0
- package/src/number/branded-types/uint.mts +179 -0
- package/src/number/branded-types/uint.test.mts +158 -0
- package/src/number/branded-types/uint16.mts +186 -0
- package/src/number/branded-types/uint16.test.mts +170 -0
- package/src/number/branded-types/uint32.mts +218 -0
- package/src/number/branded-types/uint32.test.mts +170 -0
- package/src/number/enum/index.mts +2 -0
- package/src/number/enum/int8.mts +344 -0
- package/src/number/enum/int8.test.mts +180 -0
- package/src/number/enum/uint8.mts +293 -0
- package/src/number/enum/uint8.test.mts +164 -0
- package/src/number/index.mts +4 -0
- package/src/number/num.mts +604 -0
- package/src/number/num.test.mts +242 -0
- package/src/number/refined-number-utils.mts +566 -0
- package/src/object/index.mts +1 -0
- package/src/object/object.mts +447 -0
- package/src/object/object.test.mts +124 -0
- package/src/others/cast-mutable.mts +113 -0
- package/src/others/cast-readonly.mts +192 -0
- package/src/others/cast-readonly.test.mts +89 -0
- package/src/others/if-then.mts +98 -0
- package/src/others/if-then.test.mts +75 -0
- package/src/others/index.mts +7 -0
- package/src/others/map-nullable.mts +172 -0
- package/src/others/map-nullable.test.mts +297 -0
- package/src/others/memoize-function.mts +196 -0
- package/src/others/memoize-function.test.mts +168 -0
- package/src/others/tuple.mts +160 -0
- package/src/others/tuple.test.mts +11 -0
- package/src/others/unknown-to-string.mts +215 -0
- package/src/others/unknown-to-string.test.mts +114 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { expectType } from '../../expect-type.mjs';
|
|
2
|
+
import { TsVerifiedInternals } from '../refined-number-utils.mjs';
|
|
3
|
+
|
|
4
|
+
const typeNameInMessage = 'an non-negative integer less than 256';
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
MIN_VALUE,
|
|
8
|
+
MAX_VALUE,
|
|
9
|
+
random: randomImpl,
|
|
10
|
+
is: isImpl,
|
|
11
|
+
castType: castTypeImpl,
|
|
12
|
+
clamp: clampImpl,
|
|
13
|
+
} = TsVerifiedInternals.RefinedNumberUtils.operatorsForInteger<Uint16, 0, 255>({
|
|
14
|
+
integerOrSafeInteger: 'SafeInteger',
|
|
15
|
+
MIN_VALUE: 0,
|
|
16
|
+
MAX_VALUE: 255,
|
|
17
|
+
typeNameInMessage,
|
|
18
|
+
} as const);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Type guard that checks if a value is an 8-bit unsigned integer.
|
|
22
|
+
*
|
|
23
|
+
* A Uint8 is an unsigned integer in the range [0, 255], representing
|
|
24
|
+
* values that fit in exactly 8 bits of memory (1 byte).
|
|
25
|
+
*
|
|
26
|
+
* @param x - The number to check
|
|
27
|
+
* @returns `true` if x is a valid Uint8, `false` otherwise
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* is(100); // true
|
|
32
|
+
* is(0); // true (minimum value)
|
|
33
|
+
* is(255); // true (maximum value)
|
|
34
|
+
* is(256); // false (exceeds max)
|
|
35
|
+
* is(-1); // false (negative)
|
|
36
|
+
* is(5.5); // false (not integer)
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
const is = (x: number): x is Uint8 => isImpl(x);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Casts a number to a Uint8 branded type.
|
|
43
|
+
*
|
|
44
|
+
* This function validates that the input is within the Uint8 range [0, 255]
|
|
45
|
+
* and is an integer, then returns it with the Uint8 brand.
|
|
46
|
+
*
|
|
47
|
+
* @param x - The number to convert
|
|
48
|
+
* @returns The number as a Uint8 branded type
|
|
49
|
+
* @throws {TypeError} If x is not a valid 8-bit unsigned integer
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const byte = castType(200); // Uint8
|
|
54
|
+
* const zero = castType(0); // Uint8 (minimum)
|
|
55
|
+
* const max = castType(255); // Uint8 (maximum)
|
|
56
|
+
*
|
|
57
|
+
* // These throw TypeError:
|
|
58
|
+
* // castType(256); // Exceeds maximum
|
|
59
|
+
* // castType(-1); // Negative value
|
|
60
|
+
* // castType(1.5); // Not an integer
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
const castType = (x: number): Uint8 =>
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
65
|
+
castTypeImpl(x) as Uint8;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Clamps a number to the Uint8 range [0, 255].
|
|
69
|
+
* @param a - The number to clamp
|
|
70
|
+
* @returns The clamped value as Uint8
|
|
71
|
+
*/
|
|
72
|
+
const clamp = (a: number): Uint8 => castType(clampImpl(a));
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Returns the minimum value from a list of Uint8 values.
|
|
76
|
+
*
|
|
77
|
+
* @param values - The Uint8 values to compare (at least one required)
|
|
78
|
+
* @returns The smallest value as a Uint8
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* min_(asUint8(50), asUint8(30), asUint8(100)); // Uint8 (30)
|
|
83
|
+
* min_(asUint8(0), asUint8(255)); // Uint8 (0)
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
const min_ = (...values: readonly Uint8[]): Uint8 =>
|
|
87
|
+
castType(Math.min(...values));
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the maximum of the given Uint8 values.
|
|
91
|
+
* @param values - The Uint8 values to compare
|
|
92
|
+
* @returns The maximum value
|
|
93
|
+
*/
|
|
94
|
+
const max_ = (...values: readonly Uint8[]): Uint8 =>
|
|
95
|
+
castType(Math.max(...values));
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Raises x to the power of y, clamped to Uint8 range.
|
|
99
|
+
* @param x - The base
|
|
100
|
+
* @param y - The exponent
|
|
101
|
+
* @returns x^y clamped to [0, 255]
|
|
102
|
+
*/
|
|
103
|
+
const pow = (x: Uint8, y: Uint8): Uint8 => clamp(x ** y);
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Adds two Uint8 values, clamped to Uint8 range.
|
|
107
|
+
* @param x - First operand
|
|
108
|
+
* @param y - Second operand
|
|
109
|
+
* @returns x + y clamped to [0, 255]
|
|
110
|
+
*/
|
|
111
|
+
const add = (x: Uint8, y: Uint8): Uint8 => clamp(x + y);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Subtracts two Uint8 values, clamped to Uint8 range.
|
|
115
|
+
* @param x - First operand
|
|
116
|
+
* @param y - Second operand
|
|
117
|
+
* @returns x - y clamped to [0, 255]
|
|
118
|
+
*/
|
|
119
|
+
const sub = (x: Uint8, y: Uint8): Uint8 => clamp(x - y);
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Multiplies two Uint8 values, clamped to Uint8 range.
|
|
123
|
+
* @param x - First operand
|
|
124
|
+
* @param y - Second operand
|
|
125
|
+
* @returns x * y clamped to [0, 255]
|
|
126
|
+
*/
|
|
127
|
+
const mul = (x: Uint8, y: Uint8): Uint8 => clamp(x * y);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Divides two Uint8 values, clamped to Uint8 range.
|
|
131
|
+
* @param x - The dividend
|
|
132
|
+
* @param y - The divisor (cannot be 0)
|
|
133
|
+
* @returns ⌊x / y⌋ clamped to [0, 255]
|
|
134
|
+
*/
|
|
135
|
+
const div = (x: Uint8, y: Exclude<Uint8, 0>): Uint8 => clamp(Math.floor(x / y));
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Generates a random Uint8 value within the specified range.
|
|
139
|
+
* @param min - The minimum value (inclusive)
|
|
140
|
+
* @param max - The maximum value (inclusive)
|
|
141
|
+
* @returns A random Uint8 between min and max
|
|
142
|
+
*/
|
|
143
|
+
const random = (min: Uint8, max: Uint8): Uint8 =>
|
|
144
|
+
castType(randomImpl(castTypeImpl(min), castTypeImpl(max)));
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Checks if a number is a Uint8 (8-bit unsigned integer in the range [0, 255]).
|
|
148
|
+
* @param value The value to check.
|
|
149
|
+
* @returns `true` if the value is a Uint8, `false` otherwise.
|
|
150
|
+
*/
|
|
151
|
+
export const isUint8 = is;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Casts a number to a Uint8 type.
|
|
155
|
+
* @param value The value to cast.
|
|
156
|
+
* @returns The value as a Uint8 type.
|
|
157
|
+
* @throws {TypeError} If the value is not a valid 8-bit unsigned integer.
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const x = asUint8(255); // Uint8
|
|
161
|
+
* const y = asUint8(0); // Uint8
|
|
162
|
+
* // asUint8(-1); // throws TypeError
|
|
163
|
+
* // asUint8(256); // throws TypeError
|
|
164
|
+
* // asUint8(1.5); // throws TypeError
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export const asUint8 = castType;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Namespace providing type-safe arithmetic operations for 8-bit unsigned integers.
|
|
171
|
+
*
|
|
172
|
+
* All operations automatically clamp results to the valid Uint8 range [0, 255].
|
|
173
|
+
* This ensures that all arithmetic maintains the 8-bit unsigned integer constraint,
|
|
174
|
+
* with negative results clamped to 0 and overflow results clamped to MAX_VALUE.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* const a = asUint8(200);
|
|
179
|
+
* const b = asUint8(100);
|
|
180
|
+
*
|
|
181
|
+
* // Arithmetic operations with automatic clamping
|
|
182
|
+
* const sum = Uint8.add(a, b); // Uint8 (255 - clamped to MAX_VALUE)
|
|
183
|
+
* const diff = Uint8.sub(a, b); // Uint8 (100)
|
|
184
|
+
* const reverseDiff = Uint8.sub(b, a); // Uint8 (0 - clamped to MIN_VALUE)
|
|
185
|
+
* const product = Uint8.mul(a, b); // Uint8 (255 - clamped due to overflow)
|
|
186
|
+
*
|
|
187
|
+
* // Range operations
|
|
188
|
+
* const clamped = Uint8.clamp(-10); // Uint8 (0)
|
|
189
|
+
* const minimum = Uint8.min(a, b); // Uint8 (100)
|
|
190
|
+
* const maximum = Uint8.max(a, b); // Uint8 (200)
|
|
191
|
+
*
|
|
192
|
+
* // Utility operations
|
|
193
|
+
* const random = Uint8.random(asUint8(50), asUint8(150)); // Uint8 (random value in [50, 150])
|
|
194
|
+
* const power = Uint8.pow(asUint8(2), asUint8(7)); // Uint8 (128)
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
export const Uint8 = {
|
|
198
|
+
/**
|
|
199
|
+
* Type guard to check if a value is a Uint8.
|
|
200
|
+
* @param value The value to check.
|
|
201
|
+
* @returns `true` if the value is an 8-bit unsigned integer, `false` otherwise.
|
|
202
|
+
*/
|
|
203
|
+
is,
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The minimum value for an 8-bit unsigned integer.
|
|
207
|
+
* @readonly
|
|
208
|
+
*/
|
|
209
|
+
MIN_VALUE,
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The maximum value for an 8-bit unsigned integer.
|
|
213
|
+
* @readonly
|
|
214
|
+
*/
|
|
215
|
+
MAX_VALUE,
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Returns the larger of the given Uint8 values.
|
|
219
|
+
* @param values The Uint8 values to compare.
|
|
220
|
+
* @returns The maximum value as a Uint8.
|
|
221
|
+
*/
|
|
222
|
+
max: max_,
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Returns the smaller of the given Uint8 values.
|
|
226
|
+
* @param values The Uint8 values to compare.
|
|
227
|
+
* @returns The minimum value as a Uint8.
|
|
228
|
+
*/
|
|
229
|
+
min: min_,
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Clamps a number to the Uint8 range.
|
|
233
|
+
* @param value The number to clamp.
|
|
234
|
+
* @returns The value clamped to [0, 255] as a Uint8.
|
|
235
|
+
*/
|
|
236
|
+
clamp,
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Generates a random Uint8 value within the specified range.
|
|
240
|
+
* @param min The minimum value (inclusive).
|
|
241
|
+
* @param max The maximum value (inclusive).
|
|
242
|
+
* @returns A random Uint8 between min and max.
|
|
243
|
+
*/
|
|
244
|
+
random,
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Raises a Uint8 to the power of another Uint8.
|
|
248
|
+
* @param a The base Uint8.
|
|
249
|
+
* @param b The exponent Uint8.
|
|
250
|
+
* @returns `a ** b` clamped to [0, 255] as a Uint8.
|
|
251
|
+
*/
|
|
252
|
+
pow,
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Adds two Uint8 values.
|
|
256
|
+
* @param a The first Uint8.
|
|
257
|
+
* @param b The second Uint8.
|
|
258
|
+
* @returns `a + b` clamped to [0, 255] as a Uint8.
|
|
259
|
+
*/
|
|
260
|
+
add,
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Subtracts one Uint8 from another.
|
|
264
|
+
* @param a The minuend Uint8.
|
|
265
|
+
* @param b The subtrahend Uint8.
|
|
266
|
+
* @returns `a - b` clamped to [0, 255] as a Uint8 (minimum 0).
|
|
267
|
+
*/
|
|
268
|
+
sub,
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Multiplies two Uint8 values.
|
|
272
|
+
* @param a The first Uint8.
|
|
273
|
+
* @param b The second Uint8.
|
|
274
|
+
* @returns `a * b` clamped to [0, 255] as a Uint8.
|
|
275
|
+
*/
|
|
276
|
+
mul,
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Divides one Uint8 by another using floor division.
|
|
280
|
+
* @param a The dividend Uint8.
|
|
281
|
+
* @param b The divisor Uint8 (cannot be 0).
|
|
282
|
+
* @returns `⌊a / b⌋` clamped to [0, 255] as a Uint8.
|
|
283
|
+
*/
|
|
284
|
+
div,
|
|
285
|
+
} as const;
|
|
286
|
+
|
|
287
|
+
expectType<
|
|
288
|
+
keyof typeof Uint8,
|
|
289
|
+
keyof TsVerifiedInternals.RefinedNumberUtils.NumberClass<
|
|
290
|
+
Uint16,
|
|
291
|
+
'int' | 'non-negative' | 'range'
|
|
292
|
+
>
|
|
293
|
+
>('=');
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { expectType } from '../../expect-type.mjs';
|
|
2
|
+
import { Uint8, asUint8, isUint8 } from './uint8.mjs';
|
|
3
|
+
|
|
4
|
+
describe('Uint8', () => {
|
|
5
|
+
describe('isUint8', () => {
|
|
6
|
+
test.each([
|
|
7
|
+
{ value: 0, expected: true },
|
|
8
|
+
{ value: 128, expected: true },
|
|
9
|
+
{ value: 255, expected: true },
|
|
10
|
+
{ value: -1, expected: false },
|
|
11
|
+
{ value: 256, expected: false },
|
|
12
|
+
{ value: 1.5, expected: false },
|
|
13
|
+
{ value: Number.NaN, expected: false },
|
|
14
|
+
{ value: Number.POSITIVE_INFINITY, expected: false },
|
|
15
|
+
{ value: Number.NEGATIVE_INFINITY, expected: false },
|
|
16
|
+
])('isUint8($value) should return $expected', ({ value, expected }) => {
|
|
17
|
+
expect(isUint8(value)).toBe(expected);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('asUint8', () => {
|
|
22
|
+
test.each([
|
|
23
|
+
{ value: 0, expected: 0 },
|
|
24
|
+
{ value: 128, expected: 128 },
|
|
25
|
+
{ value: 255, expected: 255 },
|
|
26
|
+
])('asUint8($value) should return $expected', ({ value, expected }) => {
|
|
27
|
+
expect(asUint8(value)).toBe(expected);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test.each([
|
|
31
|
+
{ value: -1, name: '-1' },
|
|
32
|
+
{ value: 256, name: '256' },
|
|
33
|
+
{ value: 1.5, name: '1.5' },
|
|
34
|
+
{ value: Number.NaN, name: 'NaN' },
|
|
35
|
+
{ value: Number.POSITIVE_INFINITY, name: 'Infinity' },
|
|
36
|
+
{ value: Number.NEGATIVE_INFINITY, name: '-Infinity' },
|
|
37
|
+
])('asUint8($name) should throw TypeError', ({ value }) => {
|
|
38
|
+
expect(() => asUint8(value)).toThrow(TypeError);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('Uint8.is', () => {
|
|
43
|
+
test('should be the same as isUint8', () => {
|
|
44
|
+
expect(Uint8.is).toBe(isUint8);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('Uint8.MIN_VALUE', () => {
|
|
49
|
+
test('should be 0', () => {
|
|
50
|
+
expect(Uint8.MIN_VALUE).toBe(0);
|
|
51
|
+
expectType<typeof Uint8.MIN_VALUE, 0>('=');
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('Uint8.MAX_VALUE', () => {
|
|
56
|
+
test('should be 255', () => {
|
|
57
|
+
expect(Uint8.MAX_VALUE).toBe(255);
|
|
58
|
+
expectType<typeof Uint8.MAX_VALUE, 255>('=');
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('Uint8.clamp', () => {
|
|
63
|
+
test.each([
|
|
64
|
+
{ value: -100, expected: 0 },
|
|
65
|
+
{ value: 0, expected: 0 },
|
|
66
|
+
{ value: 128, expected: 128 },
|
|
67
|
+
{ value: 255, expected: 255 },
|
|
68
|
+
{ value: 300, expected: 255 },
|
|
69
|
+
{ value: 1.5, expected: 2 },
|
|
70
|
+
])('Uint8.clamp($value) should return $expected', ({ value, expected }) => {
|
|
71
|
+
expect(Uint8.clamp(value)).toBe(expected);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('Uint8.min', () => {
|
|
76
|
+
test('should return minimum value', () => {
|
|
77
|
+
expect(Uint8.min(100, 50, 10)).toBe(10);
|
|
78
|
+
expect(Uint8.min(1, 2, 3)).toBe(1);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe('Uint8.max', () => {
|
|
83
|
+
test('should return maximum value', () => {
|
|
84
|
+
expect(Uint8.max(100, 50, 10)).toBe(100);
|
|
85
|
+
expect(Uint8.max(1, 2, 3)).toBe(3);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('Uint8.add', () => {
|
|
90
|
+
test.each([
|
|
91
|
+
{ x: 10, y: 20, expected: 30 },
|
|
92
|
+
{ x: 200, y: 100, expected: 255 }, // clamped to max
|
|
93
|
+
{ x: 100, y: 50, expected: 150 },
|
|
94
|
+
] as const)(
|
|
95
|
+
'Uint8.add($x, $y) should return $expected',
|
|
96
|
+
({ x, y, expected }) => {
|
|
97
|
+
expect(Uint8.add(x, y)).toBe(expected);
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('Uint8.sub', () => {
|
|
103
|
+
test.each([
|
|
104
|
+
{ x: 30, y: 10, expected: 20 },
|
|
105
|
+
{ x: 10, y: 30, expected: 0 }, // clamped to min
|
|
106
|
+
{ x: 200, y: 50, expected: 150 },
|
|
107
|
+
] as const)(
|
|
108
|
+
'Uint8.sub($x, $y) should return $expected',
|
|
109
|
+
({ x, y, expected }) => {
|
|
110
|
+
expect(Uint8.sub(x, y)).toBe(expected);
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('Uint8.mul', () => {
|
|
116
|
+
test.each([
|
|
117
|
+
{ x: 5, y: 10, expected: 50 },
|
|
118
|
+
{ x: 20, y: 20, expected: 255 }, // clamped to max
|
|
119
|
+
{ x: 100, y: 2, expected: 200 },
|
|
120
|
+
] as const)(
|
|
121
|
+
'Uint8.mul($x, $y) should return $expected',
|
|
122
|
+
({ x, y, expected }) => {
|
|
123
|
+
expect(Uint8.mul(x, y)).toBe(expected);
|
|
124
|
+
},
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('Uint8.div', () => {
|
|
129
|
+
test.each([
|
|
130
|
+
{ x: 100, y: 2, expected: 50 },
|
|
131
|
+
{ x: 100, y: 3, expected: 33 }, // floor division
|
|
132
|
+
{ x: 255, y: 5, expected: 51 },
|
|
133
|
+
] as const)(
|
|
134
|
+
'Uint8.div($x, $y) should return $expected',
|
|
135
|
+
({ x, y, expected }) => {
|
|
136
|
+
expect(Uint8.div(x, y)).toBe(expected);
|
|
137
|
+
},
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('Uint8.pow', () => {
|
|
142
|
+
test.each([
|
|
143
|
+
{ x: 2, y: 3, expected: 8 },
|
|
144
|
+
{ x: 4, y: 4, expected: 255 }, // clamped to max
|
|
145
|
+
{ x: 10, y: 2, expected: 100 },
|
|
146
|
+
] as const)(
|
|
147
|
+
'Uint8.pow($x, $y) should return $expected',
|
|
148
|
+
({ x, y, expected }) => {
|
|
149
|
+
expect(Uint8.pow(x, y)).toBe(expected);
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('Uint8.random', () => {
|
|
155
|
+
test('should generate value within range', () => {
|
|
156
|
+
const min = 10;
|
|
157
|
+
const max = 50;
|
|
158
|
+
const result = Uint8.random(min, max);
|
|
159
|
+
expect(result).toBeGreaterThanOrEqual(min);
|
|
160
|
+
expect(result).toBeLessThanOrEqual(max);
|
|
161
|
+
expect(Number.isInteger(result)).toBe(true);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|