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.
Files changed (143) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +534 -0
  3. package/package.json +101 -0
  4. package/src/array/array-utils-creation.test.mts +443 -0
  5. package/src/array/array-utils-modification.test.mts +197 -0
  6. package/src/array/array-utils-overload-type-error.test.mts +149 -0
  7. package/src/array/array-utils-reducing-value.test.mts +425 -0
  8. package/src/array/array-utils-search.test.mts +169 -0
  9. package/src/array/array-utils-set-op.test.mts +335 -0
  10. package/src/array/array-utils-slice-clamped.test.mts +113 -0
  11. package/src/array/array-utils-slicing.test.mts +316 -0
  12. package/src/array/array-utils-transformation.test.mts +790 -0
  13. package/src/array/array-utils-validation.test.mts +492 -0
  14. package/src/array/array-utils.mts +4000 -0
  15. package/src/array/array.test.mts +146 -0
  16. package/src/array/index.mts +2 -0
  17. package/src/array/tuple-utils.mts +519 -0
  18. package/src/array/tuple-utils.test.mts +518 -0
  19. package/src/collections/imap-mapped.mts +801 -0
  20. package/src/collections/imap-mapped.test.mts +860 -0
  21. package/src/collections/imap.mts +651 -0
  22. package/src/collections/imap.test.mts +932 -0
  23. package/src/collections/index.mts +6 -0
  24. package/src/collections/iset-mapped.mts +889 -0
  25. package/src/collections/iset-mapped.test.mts +1187 -0
  26. package/src/collections/iset.mts +682 -0
  27. package/src/collections/iset.test.mts +1084 -0
  28. package/src/collections/queue.mts +390 -0
  29. package/src/collections/queue.test.mts +282 -0
  30. package/src/collections/stack.mts +423 -0
  31. package/src/collections/stack.test.mts +225 -0
  32. package/src/expect-type.mts +206 -0
  33. package/src/functional/index.mts +4 -0
  34. package/src/functional/match.mts +300 -0
  35. package/src/functional/match.test.mts +177 -0
  36. package/src/functional/optional.mts +733 -0
  37. package/src/functional/optional.test.mts +619 -0
  38. package/src/functional/pipe.mts +212 -0
  39. package/src/functional/pipe.test.mts +85 -0
  40. package/src/functional/result.mts +1134 -0
  41. package/src/functional/result.test.mts +777 -0
  42. package/src/globals.d.mts +38 -0
  43. package/src/guard/has-key.mts +119 -0
  44. package/src/guard/has-key.test.mts +219 -0
  45. package/src/guard/index.mts +7 -0
  46. package/src/guard/is-non-empty-string.mts +108 -0
  47. package/src/guard/is-non-empty-string.test.mts +91 -0
  48. package/src/guard/is-non-null-object.mts +106 -0
  49. package/src/guard/is-non-null-object.test.mts +90 -0
  50. package/src/guard/is-primitive.mts +165 -0
  51. package/src/guard/is-primitive.test.mts +102 -0
  52. package/src/guard/is-record.mts +153 -0
  53. package/src/guard/is-record.test.mts +112 -0
  54. package/src/guard/is-type.mts +450 -0
  55. package/src/guard/is-type.test.mts +496 -0
  56. package/src/guard/key-is-in.mts +163 -0
  57. package/src/guard/key-is-in.test.mts +19 -0
  58. package/src/index.mts +10 -0
  59. package/src/iterator/index.mts +1 -0
  60. package/src/iterator/range.mts +120 -0
  61. package/src/iterator/range.test.mts +33 -0
  62. package/src/json/index.mts +1 -0
  63. package/src/json/json.mts +711 -0
  64. package/src/json/json.test.mts +628 -0
  65. package/src/number/branded-types/finite-number.mts +354 -0
  66. package/src/number/branded-types/finite-number.test.mts +135 -0
  67. package/src/number/branded-types/index.mts +26 -0
  68. package/src/number/branded-types/int.mts +278 -0
  69. package/src/number/branded-types/int.test.mts +140 -0
  70. package/src/number/branded-types/int16.mts +192 -0
  71. package/src/number/branded-types/int16.test.mts +170 -0
  72. package/src/number/branded-types/int32.mts +193 -0
  73. package/src/number/branded-types/int32.test.mts +170 -0
  74. package/src/number/branded-types/non-negative-finite-number.mts +223 -0
  75. package/src/number/branded-types/non-negative-finite-number.test.mts +188 -0
  76. package/src/number/branded-types/non-negative-int16.mts +187 -0
  77. package/src/number/branded-types/non-negative-int16.test.mts +201 -0
  78. package/src/number/branded-types/non-negative-int32.mts +187 -0
  79. package/src/number/branded-types/non-negative-int32.test.mts +204 -0
  80. package/src/number/branded-types/non-zero-finite-number.mts +229 -0
  81. package/src/number/branded-types/non-zero-finite-number.test.mts +198 -0
  82. package/src/number/branded-types/non-zero-int.mts +167 -0
  83. package/src/number/branded-types/non-zero-int.test.mts +177 -0
  84. package/src/number/branded-types/non-zero-int16.mts +196 -0
  85. package/src/number/branded-types/non-zero-int16.test.mts +195 -0
  86. package/src/number/branded-types/non-zero-int32.mts +196 -0
  87. package/src/number/branded-types/non-zero-int32.test.mts +197 -0
  88. package/src/number/branded-types/non-zero-safe-int.mts +196 -0
  89. package/src/number/branded-types/non-zero-safe-int.test.mts +232 -0
  90. package/src/number/branded-types/non-zero-uint16.mts +189 -0
  91. package/src/number/branded-types/non-zero-uint16.test.mts +199 -0
  92. package/src/number/branded-types/non-zero-uint32.mts +189 -0
  93. package/src/number/branded-types/non-zero-uint32.test.mts +199 -0
  94. package/src/number/branded-types/positive-finite-number.mts +241 -0
  95. package/src/number/branded-types/positive-finite-number.test.mts +204 -0
  96. package/src/number/branded-types/positive-int.mts +304 -0
  97. package/src/number/branded-types/positive-int.test.mts +176 -0
  98. package/src/number/branded-types/positive-int16.mts +188 -0
  99. package/src/number/branded-types/positive-int16.test.mts +197 -0
  100. package/src/number/branded-types/positive-int32.mts +188 -0
  101. package/src/number/branded-types/positive-int32.test.mts +197 -0
  102. package/src/number/branded-types/positive-safe-int.mts +187 -0
  103. package/src/number/branded-types/positive-safe-int.test.mts +210 -0
  104. package/src/number/branded-types/positive-uint16.mts +188 -0
  105. package/src/number/branded-types/positive-uint16.test.mts +203 -0
  106. package/src/number/branded-types/positive-uint32.mts +188 -0
  107. package/src/number/branded-types/positive-uint32.test.mts +203 -0
  108. package/src/number/branded-types/safe-int.mts +291 -0
  109. package/src/number/branded-types/safe-int.test.mts +170 -0
  110. package/src/number/branded-types/safe-uint.mts +187 -0
  111. package/src/number/branded-types/safe-uint.test.mts +176 -0
  112. package/src/number/branded-types/uint.mts +179 -0
  113. package/src/number/branded-types/uint.test.mts +158 -0
  114. package/src/number/branded-types/uint16.mts +186 -0
  115. package/src/number/branded-types/uint16.test.mts +170 -0
  116. package/src/number/branded-types/uint32.mts +218 -0
  117. package/src/number/branded-types/uint32.test.mts +170 -0
  118. package/src/number/enum/index.mts +2 -0
  119. package/src/number/enum/int8.mts +344 -0
  120. package/src/number/enum/int8.test.mts +180 -0
  121. package/src/number/enum/uint8.mts +293 -0
  122. package/src/number/enum/uint8.test.mts +164 -0
  123. package/src/number/index.mts +4 -0
  124. package/src/number/num.mts +604 -0
  125. package/src/number/num.test.mts +242 -0
  126. package/src/number/refined-number-utils.mts +566 -0
  127. package/src/object/index.mts +1 -0
  128. package/src/object/object.mts +447 -0
  129. package/src/object/object.test.mts +124 -0
  130. package/src/others/cast-mutable.mts +113 -0
  131. package/src/others/cast-readonly.mts +192 -0
  132. package/src/others/cast-readonly.test.mts +89 -0
  133. package/src/others/if-then.mts +98 -0
  134. package/src/others/if-then.test.mts +75 -0
  135. package/src/others/index.mts +7 -0
  136. package/src/others/map-nullable.mts +172 -0
  137. package/src/others/map-nullable.test.mts +297 -0
  138. package/src/others/memoize-function.mts +196 -0
  139. package/src/others/memoize-function.test.mts +168 -0
  140. package/src/others/tuple.mts +160 -0
  141. package/src/others/tuple.test.mts +11 -0
  142. package/src/others/unknown-to-string.mts +215 -0
  143. package/src/others/unknown-to-string.test.mts +114 -0
@@ -0,0 +1,186 @@
1
+ import { expectType } from '../../expect-type.mjs';
2
+ import { TsVerifiedInternals } from '../refined-number-utils.mjs';
3
+
4
+ type ElementType = Uint16;
5
+
6
+ const typeNameInMessage = 'a non-negative integer less than 2^16';
7
+
8
+ const {
9
+ MIN_VALUE,
10
+ MAX_VALUE,
11
+ min: min_,
12
+ max: max_,
13
+ pow,
14
+ add,
15
+ sub,
16
+ mul,
17
+ div,
18
+ random,
19
+ is,
20
+ castType,
21
+ clamp,
22
+ } = TsVerifiedInternals.RefinedNumberUtils.operatorsForInteger<
23
+ ElementType,
24
+ 0,
25
+ number
26
+ >({
27
+ integerOrSafeInteger: 'SafeInteger',
28
+ MIN_VALUE: 0,
29
+ MAX_VALUE: 2 ** 16 - 1,
30
+ typeNameInMessage,
31
+ } as const);
32
+
33
+ /**
34
+ * Checks if a number is a Uint16 (16-bit unsigned integer in the range [0, 2^16)).
35
+ * @param value The value to check.
36
+ * @returns `true` if the value is a Uint16, `false` otherwise.
37
+ */
38
+ export const isUint16 = is;
39
+
40
+ /**
41
+ * Casts a number to a Uint16 type.
42
+ * @param value The value to cast.
43
+ * @returns The value as a Uint16 type.
44
+ * @throws {TypeError} If the value is not a non-negative integer less than 2^16.
45
+ * @example
46
+ * ```typescript
47
+ * const x = asUint16(1000); // Uint16
48
+ * const y = asUint16(0); // Uint16
49
+ * // asUint16(-1); // throws TypeError
50
+ * // asUint16(70000); // throws TypeError
51
+ * ```
52
+ */
53
+ export const asUint16 = castType;
54
+
55
+ /**
56
+ * Namespace providing type-safe arithmetic operations for 16-bit unsigned integers.
57
+ *
58
+ * All operations automatically clamp results to the valid Uint16 range [0, 65535].
59
+ * This ensures that all arithmetic maintains the 16-bit unsigned integer constraint,
60
+ * with negative results clamped to 0 and overflow results clamped to MAX_VALUE.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const a = asUint16(60000);
65
+ * const b = asUint16(10000);
66
+ *
67
+ * // Arithmetic operations with automatic clamping
68
+ * const sum = Uint16.add(a, b); // Uint16 (65535 - clamped to MAX_VALUE)
69
+ * const diff = Uint16.sub(b, a); // Uint16 (0 - clamped to MIN_VALUE)
70
+ * const product = Uint16.mul(a, b); // Uint16 (65535 - clamped due to overflow)
71
+ *
72
+ * // Range operations
73
+ * const clamped = Uint16.clamp(-100); // Uint16 (0)
74
+ * const minimum = Uint16.min(a, b); // Uint16 (10000)
75
+ * const maximum = Uint16.max(a, b); // Uint16 (60000)
76
+ *
77
+ * // Utility operations
78
+ * const random = Uint16.random(); // Uint16 (random value in [0, 65535])
79
+ * const power = Uint16.pow(asUint16(2), asUint16(10)); // Uint16 (1024)
80
+ * ```
81
+ */
82
+ export const Uint16 = {
83
+ /**
84
+ * Type guard to check if a value is a Uint16.
85
+ * @param value The value to check.
86
+ * @returns `true` if the value is a 16-bit unsigned integer, `false` otherwise.
87
+ */
88
+ is,
89
+
90
+ /**
91
+ * The minimum value for a 16-bit unsigned integer.
92
+ * @readonly
93
+ */
94
+ MIN_VALUE,
95
+
96
+ /**
97
+ * The maximum value for a 16-bit unsigned integer.
98
+ * @readonly
99
+ */
100
+ MAX_VALUE,
101
+
102
+ /**
103
+ * Returns the smaller of two Uint16 values.
104
+ * @param a The first Uint16.
105
+ * @param b The second Uint16.
106
+ * @returns The minimum value as a Uint16.
107
+ */
108
+ min: min_,
109
+
110
+ /**
111
+ * Returns the larger of two Uint16 values.
112
+ * @param a The first Uint16.
113
+ * @param b The second Uint16.
114
+ * @returns The maximum value as a Uint16.
115
+ */
116
+ max: max_,
117
+
118
+ /**
119
+ * Clamps a number to the Uint16 range.
120
+ * @param value The number to clamp.
121
+ * @returns The value clamped to [0, 65535] as a Uint16.
122
+ */
123
+ clamp,
124
+
125
+ /**
126
+ * Generates a random Uint16 value within the valid range.
127
+ * @returns A random Uint16 between 0 and 65535.
128
+ */
129
+ random,
130
+
131
+ /**
132
+ * Raises a Uint16 to the power of another Uint16.
133
+ * @param a The base Uint16.
134
+ * @param b The exponent Uint16.
135
+ * @returns `a ** b` clamped to [0, 65535] as a Uint16.
136
+ */
137
+ pow,
138
+
139
+ /**
140
+ * Adds two Uint16 values.
141
+ * @param a The first Uint16.
142
+ * @param b The second Uint16.
143
+ * @returns `a + b` clamped to [0, 65535] as a Uint16.
144
+ */
145
+ add,
146
+
147
+ /**
148
+ * Subtracts one Uint16 from another.
149
+ * @param a The minuend Uint16.
150
+ * @param b The subtrahend Uint16.
151
+ * @returns `a - b` clamped to [0, 65535] as a Uint16 (minimum 0).
152
+ */
153
+ sub,
154
+
155
+ /**
156
+ * Multiplies two Uint16 values.
157
+ * @param a The first Uint16.
158
+ * @param b The second Uint16.
159
+ * @returns `a * b` clamped to [0, 65535] as a Uint16.
160
+ */
161
+ mul,
162
+
163
+ /**
164
+ * Divides one Uint16 by another using floor division.
165
+ * @param a The dividend Uint16.
166
+ * @param b The divisor Uint16.
167
+ * @returns `⌊a / b⌋` clamped to [0, 65535] as a Uint16.
168
+ */
169
+ div,
170
+ } as const;
171
+
172
+ expectType<
173
+ keyof typeof Uint16,
174
+ keyof TsVerifiedInternals.RefinedNumberUtils.NumberClass<
175
+ ElementType,
176
+ 'int' | 'non-negative' | 'range'
177
+ >
178
+ >('=');
179
+
180
+ expectType<
181
+ typeof Uint16,
182
+ TsVerifiedInternals.RefinedNumberUtils.NumberClass<
183
+ ElementType,
184
+ 'int' | 'non-negative' | 'range'
185
+ >
186
+ >('<=');
@@ -0,0 +1,170 @@
1
+ import { expectType } from '../../expect-type.mjs';
2
+ import { asNonZeroUint16 } from './non-zero-uint16.mjs';
3
+ import { asUint16, isUint16, Uint16 } from './uint16.mjs';
4
+
5
+ describe('Uint16', () => {
6
+ describe('asUint16', () => {
7
+ test('accepts valid uint16 values', () => {
8
+ expect(() => asUint16(0)).not.toThrow();
9
+ expect(() => asUint16(1)).not.toThrow();
10
+ expect(() => asUint16(65535)).not.toThrow(); // 2^16 - 1
11
+ expect(() => asUint16(32768)).not.toThrow(); // 2^15
12
+ });
13
+
14
+ test('rejects values outside uint16 range', () => {
15
+ expect(() => asUint16(65536)).toThrow(TypeError); // 2^16
16
+ expect(() => asUint16(100000)).toThrow(TypeError);
17
+ });
18
+
19
+ test('rejects negative integers', () => {
20
+ expect(() => asUint16(-1)).toThrow(TypeError);
21
+ expect(() => asUint16(-42)).toThrow(TypeError);
22
+ });
23
+
24
+ test('rejects non-integers', () => {
25
+ expect(() => asUint16(Number.NaN)).toThrow(TypeError);
26
+ expect(() => asUint16(Number.POSITIVE_INFINITY)).toThrow(TypeError);
27
+ expect(() => asUint16(Number.NEGATIVE_INFINITY)).toThrow(TypeError);
28
+ expect(() => asUint16(1.2)).toThrow(TypeError);
29
+ expect(() => asUint16(-3.4)).toThrow(TypeError);
30
+ });
31
+
32
+ test('returns the same value for valid inputs', () => {
33
+ expect(asUint16(5)).toBe(5);
34
+ expect(asUint16(0)).toBe(0);
35
+ expect(asUint16(65535)).toBe(65535);
36
+ });
37
+
38
+ test.each([
39
+ { name: 'Number.NaN', value: Number.NaN },
40
+ { name: 'Number.POSITIVE_INFINITY', value: Number.POSITIVE_INFINITY },
41
+ { name: 'Number.NEGATIVE_INFINITY', value: Number.NEGATIVE_INFINITY },
42
+ { name: '1.2', value: 1.2 },
43
+ { name: '-3.4', value: -3.4 },
44
+ { name: '-1', value: -1 },
45
+ ] as const)(`asUint16($name) should throw a TypeError`, ({ value }) => {
46
+ expect(() => asUint16(value)).toThrow(
47
+ new TypeError(
48
+ `Expected a non-negative integer less than 2^16, got: ${value}`,
49
+ ),
50
+ );
51
+ });
52
+ });
53
+
54
+ describe('isUint16', () => {
55
+ test('correctly identifies uint16 values', () => {
56
+ expect(isUint16(0)).toBe(true);
57
+ expect(isUint16(1)).toBe(true);
58
+ expect(isUint16(65535)).toBe(true);
59
+ expect(isUint16(32768)).toBe(true);
60
+ });
61
+
62
+ test('correctly identifies values outside uint16 range', () => {
63
+ expect(isUint16(65536)).toBe(false);
64
+ expect(isUint16(100000)).toBe(false);
65
+ });
66
+
67
+ test('correctly identifies negative integers', () => {
68
+ expect(isUint16(-1)).toBe(false);
69
+ expect(isUint16(-42)).toBe(false);
70
+ });
71
+
72
+ test('correctly identifies non-integers', () => {
73
+ expect(isUint16(Number.NaN)).toBe(false);
74
+ expect(isUint16(Number.POSITIVE_INFINITY)).toBe(false);
75
+ expect(isUint16(Number.NEGATIVE_INFINITY)).toBe(false);
76
+ expect(isUint16(1.2)).toBe(false);
77
+ expect(isUint16(-3.4)).toBe(false);
78
+ });
79
+ });
80
+
81
+ describe('Uint16.is', () => {
82
+ test('same as isUint16 function', () => {
83
+ expect(Uint16.is(5)).toBe(isUint16(5));
84
+ expect(Uint16.is(65536)).toBe(isUint16(65536));
85
+ expect(Uint16.is(-1)).toBe(isUint16(-1));
86
+ });
87
+ });
88
+
89
+ describe('constants', () => {
90
+ test('MIN_VALUE and MAX_VALUE', () => {
91
+ expect(Uint16.MIN_VALUE).toBe(0);
92
+ expect(Uint16.MAX_VALUE).toBe(65535);
93
+ });
94
+ });
95
+
96
+ describe('mathematical operations', () => {
97
+ const a = asUint16(100);
98
+ const b = asUint16(50);
99
+ const c = asUint16(0);
100
+
101
+ test('min and max', () => {
102
+ expect(Uint16.min(a, b)).toBe(50);
103
+ expect(Uint16.max(a, b)).toBe(100);
104
+ expect(Uint16.min(a, c)).toBe(0);
105
+ expect(Uint16.max(a, c)).toBe(100);
106
+ });
107
+
108
+ test('add (with clamping to uint16 range)', () => {
109
+ const result = Uint16.add(asUint16(65000), asUint16(1000));
110
+ expect(result).toBe(65535); // clamped to max
111
+ expect(Uint16.add(a, b)).toBe(150);
112
+ });
113
+
114
+ test('sub (never goes below 0)', () => {
115
+ expect(Uint16.sub(a, b)).toBe(50);
116
+ expect(Uint16.sub(b, a)).toBe(0); // clamped to 0
117
+ expect(Uint16.sub(c, a)).toBe(0); // clamped to 0
118
+ });
119
+
120
+ test('mul (with clamping to uint16 range)', () => {
121
+ const result = Uint16.mul(asUint16(1000), asUint16(100));
122
+ expect(result).toBe(65535); // clamped to max
123
+ expect(Uint16.mul(asUint16(10), asUint16(5))).toBe(50);
124
+ });
125
+
126
+ test('div (floor division, never goes below 0)', () => {
127
+ expect(Uint16.div(a, asNonZeroUint16(50))).toBe(2);
128
+ expect(Uint16.div(asUint16(7), asNonZeroUint16(3))).toBe(2);
129
+ expect(Uint16.div(asUint16(50), asNonZeroUint16(100))).toBe(0); // floor(50/100) = 0
130
+ });
131
+
132
+ test('pow (with clamping to uint16 range)', () => {
133
+ const result = Uint16.pow(asUint16(256), asUint16(3));
134
+ expect(result).toBe(65535); // clamped to max
135
+ expect(Uint16.pow(asUint16(2), asUint16(3))).toBe(8);
136
+ });
137
+ });
138
+
139
+ describe('random', () => {
140
+ test('generates uint16 values within specified range', () => {
141
+ const min = 0;
142
+ const max = 20;
143
+
144
+ for (let i = 0; i < 10; i++) {
145
+ const result = Uint16.random(min, max);
146
+ expect(result).toBeGreaterThanOrEqual(min);
147
+ expect(result).toBeLessThanOrEqual(max);
148
+ expect(Uint16.is(result)).toBe(true);
149
+ expect(Number.isInteger(result)).toBe(true);
150
+ expect(result).toBeGreaterThanOrEqual(0);
151
+ }
152
+ });
153
+
154
+ test('generates values within Uint16 range', () => {
155
+ for (let i = 0; i < 10; i++) {
156
+ const result = Uint16.random(0, 30);
157
+ expect(result).toBeGreaterThanOrEqual(0);
158
+ expect(result).toBeLessThanOrEqual(65535);
159
+ }
160
+ });
161
+ });
162
+
163
+ describe('type assertions', () => {
164
+ test('type relationships', () => {
165
+ expectType<Uint16, number>('<=');
166
+
167
+ expectTypeOf(asUint16(100)).toExtend<Uint16>();
168
+ });
169
+ });
170
+ });
@@ -0,0 +1,218 @@
1
+ import { expectType } from '../../expect-type.mjs';
2
+ import { TsVerifiedInternals } from '../refined-number-utils.mjs';
3
+
4
+ type ElementType = Uint32;
5
+
6
+ const typeNameInMessage = 'a non-negative integer less than 2^32';
7
+
8
+ const {
9
+ MIN_VALUE,
10
+ MAX_VALUE,
11
+ min: min_,
12
+ max: max_,
13
+ pow,
14
+ add,
15
+ sub,
16
+ mul,
17
+ div,
18
+ random,
19
+ is,
20
+ castType,
21
+ clamp,
22
+ } = TsVerifiedInternals.RefinedNumberUtils.operatorsForInteger<
23
+ ElementType,
24
+ 0,
25
+ number
26
+ >({
27
+ integerOrSafeInteger: 'SafeInteger',
28
+ MIN_VALUE: 0,
29
+ MAX_VALUE: 2 ** 32 - 1,
30
+ typeNameInMessage,
31
+ } as const);
32
+
33
+ /**
34
+ * Checks if a number is a Uint32 (32-bit unsigned integer in the range [0, 2^32)).
35
+ * @param value The value to check.
36
+ * @returns `true` if the value is a Uint32, `false` otherwise.
37
+ */
38
+ export const isUint32 = is;
39
+
40
+ /**
41
+ * Casts a number to a Uint32 type.
42
+ * @param value The value to cast.
43
+ * @returns The value as a Uint32 type.
44
+ * @throws {TypeError} If the value is not a non-negative integer less than 2^32.
45
+ * @example
46
+ * ```typescript
47
+ * const x = asUint32(1000000); // Uint32
48
+ * const y = asUint32(0); // Uint32
49
+ * // asUint32(-1); // throws TypeError
50
+ * // asUint32(5000000000); // throws TypeError
51
+ * ```
52
+ */
53
+ export const asUint32 = castType;
54
+
55
+ /**
56
+ * Utility functions for working with Uint32 (32-bit unsigned integer) branded types.
57
+ * Provides type-safe operations that ensure results remain within the valid range [0, 2^32).
58
+ * All arithmetic operations are clamped to maintain the Uint32 constraint.
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // Type checking
63
+ * Uint32.is(1000000); // true
64
+ * Uint32.is(-1); // false
65
+ * Uint32.is(5000000000); // false (exceeds 2^32)
66
+ *
67
+ * // Constants
68
+ * console.log(Uint32.MIN_VALUE); // 0
69
+ * console.log(Uint32.MAX_VALUE); // 4294967295 (2^32 - 1)
70
+ *
71
+ * // Arithmetic operations (all results clamped to [0, 2^32))
72
+ * const a = asUint32(1000000);
73
+ * const b = asUint32(500000);
74
+ *
75
+ * Uint32.add(a, b); // Uint32 (1500000)
76
+ * Uint32.sub(a, b); // Uint32 (500000)
77
+ * Uint32.mul(a, b); // Uint32 (clamped if overflow)
78
+ * Uint32.div(a, b); // Uint32 (2)
79
+ * Uint32.pow(asUint32(2), asUint32(10)); // Uint32 (1024)
80
+ *
81
+ * // Utility functions
82
+ * Uint32.min(a, b); // Uint32 (500000)
83
+ * Uint32.max(a, b); // Uint32 (1000000)
84
+ * Uint32.clamp(asUint32(5000000000), Uint32.MIN_VALUE, Uint32.MAX_VALUE); // Uint32 (MAX_VALUE)
85
+ * Uint32.random(); // Random Uint32
86
+ * ```
87
+ */
88
+ export const Uint32 = {
89
+ /**
90
+ * Type guard that checks if a value is a 32-bit unsigned integer.
91
+ * @param value - The value to check
92
+ * @returns `true` if the value is within the range [0, 2^32), `false` otherwise
93
+ */
94
+ is,
95
+
96
+ /**
97
+ * The minimum value for a Uint32.
98
+ * @readonly
99
+ */
100
+ MIN_VALUE,
101
+
102
+ /**
103
+ * The maximum value for a Uint32.
104
+ * @readonly
105
+ */
106
+ MAX_VALUE,
107
+
108
+ /**
109
+ * Returns the minimum of multiple Uint32 values.
110
+ * @param values - The Uint32 values to compare
111
+ * @returns The smallest value as a Uint32
112
+ */
113
+ min: min_,
114
+
115
+ /**
116
+ * Returns the maximum of multiple Uint32 values.
117
+ * @param values - The Uint32 values to compare
118
+ * @returns The largest value as a Uint32
119
+ */
120
+ max: max_,
121
+
122
+ /**
123
+ * Clamps a Uint32 to be within the specified range.
124
+ * @param value - The value to clamp
125
+ * @param min - The minimum value
126
+ * @param max - The maximum value
127
+ * @returns The clamped value as a Uint32
128
+ * @example
129
+ * ```typescript
130
+ * Uint32.clamp(asUint32(5000000000), Uint32.MIN_VALUE, asUint32(1000)); // Uint32 (1000)
131
+ * ```
132
+ */
133
+ clamp,
134
+
135
+ /**
136
+ * Generates a random Uint32 value.
137
+ * @returns A random Uint32 value within [0, 2^32)
138
+ */
139
+ random,
140
+
141
+ /**
142
+ * Raises a Uint32 to a power, with result clamped to [0, 2^32).
143
+ * @param a - The base Uint32
144
+ * @param b - The exponent Uint32
145
+ * @returns `a ** b` as a Uint32, clamped to valid range
146
+ * @example
147
+ * ```typescript
148
+ * Uint32.pow(asUint32(2), asUint32(10)); // Uint32 (1024)
149
+ * ```
150
+ */
151
+ pow,
152
+
153
+ /**
154
+ * Adds two Uint32 values, with result clamped to [0, 2^32).
155
+ * @param a - First Uint32
156
+ * @param b - Second Uint32
157
+ * @returns `a + b` as a Uint32, clamped to valid range
158
+ * @example
159
+ * ```typescript
160
+ * Uint32.add(asUint32(1000000), asUint32(500000)); // Uint32 (1500000)
161
+ * ```
162
+ */
163
+ add,
164
+
165
+ /**
166
+ * Subtracts two Uint32 values, with result clamped to [0, 2^32).
167
+ * @param a - First Uint32
168
+ * @param b - Second Uint32
169
+ * @returns `a - b` as a Uint32, clamped to valid range (minimum 0)
170
+ * @example
171
+ * ```typescript
172
+ * Uint32.sub(asUint32(1000000), asUint32(500000)); // Uint32 (500000)
173
+ * Uint32.sub(asUint32(100), asUint32(500)); // Uint32 (0) - clamped
174
+ * ```
175
+ */
176
+ sub,
177
+
178
+ /**
179
+ * Multiplies two Uint32 values, with result clamped to [0, 2^32).
180
+ * @param a - First Uint32
181
+ * @param b - Second Uint32
182
+ * @returns `a * b` as a Uint32, clamped to valid range
183
+ * @example
184
+ * ```typescript
185
+ * Uint32.mul(asUint32(1000), asUint32(500)); // Uint32 (500000)
186
+ * ```
187
+ */
188
+ mul,
189
+
190
+ /**
191
+ * Divides two Uint32 values using floor division, with result clamped to [0, 2^32).
192
+ * @param a - The dividend Uint32
193
+ * @param b - The divisor Uint32
194
+ * @returns `⌊a / b⌋` as a Uint32, clamped to valid range
195
+ * @example
196
+ * ```typescript
197
+ * Uint32.div(asUint32(1000000), asUint32(500000)); // Uint32 (2)
198
+ * Uint32.div(asUint32(7), asUint32(3)); // Uint32 (2) - floor division
199
+ * ```
200
+ */
201
+ div,
202
+ } as const;
203
+
204
+ expectType<
205
+ keyof typeof Uint32,
206
+ keyof TsVerifiedInternals.RefinedNumberUtils.NumberClass<
207
+ ElementType,
208
+ 'int' | 'non-negative' | 'range'
209
+ >
210
+ >('=');
211
+
212
+ expectType<
213
+ typeof Uint32,
214
+ TsVerifiedInternals.RefinedNumberUtils.NumberClass<
215
+ ElementType,
216
+ 'int' | 'non-negative' | 'range'
217
+ >
218
+ >('<=');