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,188 @@
1
+ import { expectType } from '../../expect-type.mjs';
2
+ import { TsVerifiedInternals } from '../refined-number-utils.mjs';
3
+
4
+ type ElementType = PositiveUint16;
5
+
6
+ const typeNameInMessage = 'a positive integer in [1, 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
+ 1,
25
+ number
26
+ >({
27
+ integerOrSafeInteger: 'SafeInteger',
28
+ MIN_VALUE: 1,
29
+ MAX_VALUE: 2 ** 16 - 1,
30
+ typeNameInMessage,
31
+ } as const);
32
+
33
+ /**
34
+ * Checks if a number is a PositiveUint16 (16-bit positive unsigned integer in the range [1, 2^16)).
35
+ * @param value The value to check.
36
+ * @returns `true` if the value is a PositiveUint16, `false` otherwise.
37
+ */
38
+ export const isPositiveUint16 = is;
39
+
40
+ /**
41
+ * Casts a number to a PositiveUint16 type.
42
+ * @param value The value to cast.
43
+ * @returns The value as a PositiveUint16 type.
44
+ * @throws {TypeError} If the value is not a positive integer in [1, 2^16).
45
+ * @example
46
+ * ```typescript
47
+ * const x = asPositiveUint16(1000); // PositiveUint16
48
+ * const y = asPositiveUint16(65535); // PositiveUint16
49
+ * // asPositiveUint16(0); // throws TypeError
50
+ * // asPositiveUint16(-1); // throws TypeError
51
+ * // asPositiveUint16(65536); // throws TypeError
52
+ * ```
53
+ */
54
+ export const asPositiveUint16 = castType;
55
+
56
+ /**
57
+ * Namespace providing type-safe arithmetic operations for 16-bit positive unsigned integers.
58
+ *
59
+ * All operations automatically clamp results to the valid PositiveUint16 range [1, 65535].
60
+ * This ensures that all arithmetic maintains the 16-bit positive unsigned integer constraint,
61
+ * with results below 1 clamped to MIN_VALUE and overflow results clamped to MAX_VALUE.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const a = asPositiveUint16(60000);
66
+ * const b = asPositiveUint16(10000);
67
+ *
68
+ * // Arithmetic operations with automatic clamping and positive constraint
69
+ * const sum = PositiveUint16.add(a, b); // PositiveUint16 (65535 - clamped to MAX_VALUE)
70
+ * const diff = PositiveUint16.sub(a, b); // PositiveUint16 (50000)
71
+ * const reverseDiff = PositiveUint16.sub(b, a); // PositiveUint16 (1 - clamped to MIN_VALUE)
72
+ * const product = PositiveUint16.mul(a, b); // PositiveUint16 (65535 - clamped due to overflow)
73
+ *
74
+ * // Range operations (maintaining positive constraint)
75
+ * const clamped = PositiveUint16.clamp(-100); // PositiveUint16 (1)
76
+ * const minimum = PositiveUint16.min(a, b); // PositiveUint16 (10000)
77
+ * const maximum = PositiveUint16.max(a, b); // PositiveUint16 (60000)
78
+ *
79
+ * // Utility operations
80
+ * const random = PositiveUint16.random(); // PositiveUint16 (random value in [1, 65535])
81
+ * const power = PositiveUint16.pow(asPositiveUint16(2), asPositiveUint16(10)); // PositiveUint16 (1024)
82
+ * ```
83
+ */
84
+ export const PositiveUint16 = {
85
+ /**
86
+ * Type guard to check if a value is a PositiveUint16.
87
+ * @param value The value to check.
88
+ * @returns `true` if the value is a 16-bit positive unsigned integer, `false` otherwise.
89
+ */
90
+ is,
91
+
92
+ /**
93
+ * The minimum value for a 16-bit positive unsigned integer.
94
+ * @readonly
95
+ */
96
+ MIN_VALUE,
97
+
98
+ /**
99
+ * The maximum value for a 16-bit positive unsigned integer.
100
+ * @readonly
101
+ */
102
+ MAX_VALUE,
103
+
104
+ /**
105
+ * Returns the smaller of two PositiveUint16 values.
106
+ * @param a The first PositiveUint16.
107
+ * @param b The second PositiveUint16.
108
+ * @returns The minimum value as a PositiveUint16.
109
+ */
110
+ min: min_,
111
+
112
+ /**
113
+ * Returns the larger of two PositiveUint16 values.
114
+ * @param a The first PositiveUint16.
115
+ * @param b The second PositiveUint16.
116
+ * @returns The maximum value as a PositiveUint16.
117
+ */
118
+ max: max_,
119
+
120
+ /**
121
+ * Clamps a number to the PositiveUint16 range.
122
+ * @param value The number to clamp.
123
+ * @returns The value clamped to [1, 65535] as a PositiveUint16.
124
+ */
125
+ clamp,
126
+
127
+ /**
128
+ * Generates a random PositiveUint16 value within the valid range.
129
+ * @returns A random PositiveUint16 between 1 and 65535.
130
+ */
131
+ random,
132
+
133
+ /**
134
+ * Raises a PositiveUint16 to the power of another PositiveUint16.
135
+ * @param a The base PositiveUint16.
136
+ * @param b The exponent PositiveUint16.
137
+ * @returns `a ** b` clamped to [1, 65535] as a PositiveUint16.
138
+ */
139
+ pow,
140
+
141
+ /**
142
+ * Adds two PositiveUint16 values.
143
+ * @param a The first PositiveUint16.
144
+ * @param b The second PositiveUint16.
145
+ * @returns `a + b` clamped to [1, 65535] as a PositiveUint16.
146
+ */
147
+ add,
148
+
149
+ /**
150
+ * Subtracts one PositiveUint16 from another.
151
+ * @param a The minuend PositiveUint16.
152
+ * @param b The subtrahend PositiveUint16.
153
+ * @returns `a - b` clamped to [1, 65535] as a PositiveUint16 (minimum 1).
154
+ */
155
+ sub,
156
+
157
+ /**
158
+ * Multiplies two PositiveUint16 values.
159
+ * @param a The first PositiveUint16.
160
+ * @param b The second PositiveUint16.
161
+ * @returns `a * b` clamped to [1, 65535] as a PositiveUint16.
162
+ */
163
+ mul,
164
+
165
+ /**
166
+ * Divides one PositiveUint16 by another using floor division.
167
+ * @param a The dividend PositiveUint16.
168
+ * @param b The divisor PositiveUint16.
169
+ * @returns `⌊a / b⌋` clamped to [1, 65535] as a PositiveUint16.
170
+ */
171
+ div,
172
+ } as const;
173
+
174
+ expectType<
175
+ keyof typeof PositiveUint16,
176
+ keyof TsVerifiedInternals.RefinedNumberUtils.NumberClass<
177
+ ElementType,
178
+ 'int' | 'positive' | 'range'
179
+ >
180
+ >('=');
181
+
182
+ expectType<
183
+ typeof PositiveUint16,
184
+ TsVerifiedInternals.RefinedNumberUtils.NumberClass<
185
+ ElementType,
186
+ 'int' | 'positive' | 'range'
187
+ >
188
+ >('<=');
@@ -0,0 +1,203 @@
1
+ import { expectType } from '../../expect-type.mjs';
2
+ import {
3
+ asPositiveUint16,
4
+ isPositiveUint16,
5
+ PositiveUint16,
6
+ } from './positive-uint16.mjs';
7
+
8
+ describe('PositiveUint16', () => {
9
+ describe('asPositiveUint16', () => {
10
+ test('accepts valid positive uint16 values', () => {
11
+ expect(() => asPositiveUint16(1)).not.toThrow();
12
+ expect(() => asPositiveUint16(1000)).not.toThrow();
13
+ expect(() => asPositiveUint16(65535)).not.toThrow(); // 2^16 - 1
14
+ expect(() => asPositiveUint16(32768)).not.toThrow(); // 2^15
15
+ });
16
+
17
+ test('rejects zero', () => {
18
+ expect(() => asPositiveUint16(0)).toThrow(TypeError);
19
+ });
20
+
21
+ test('rejects values outside uint16 range', () => {
22
+ expect(() => asPositiveUint16(65536)).toThrow(TypeError); // 2^16
23
+ expect(() => asPositiveUint16(100000)).toThrow(TypeError);
24
+ });
25
+
26
+ test('rejects negative integers', () => {
27
+ expect(() => asPositiveUint16(-1)).toThrow(TypeError);
28
+ expect(() => asPositiveUint16(-42)).toThrow(TypeError);
29
+ });
30
+
31
+ test('rejects non-integers', () => {
32
+ expect(() => asPositiveUint16(Number.NaN)).toThrow(TypeError);
33
+ expect(() => asPositiveUint16(Number.POSITIVE_INFINITY)).toThrow(
34
+ TypeError,
35
+ );
36
+ expect(() => asPositiveUint16(Number.NEGATIVE_INFINITY)).toThrow(
37
+ TypeError,
38
+ );
39
+ expect(() => asPositiveUint16(1.2)).toThrow(TypeError);
40
+ expect(() => asPositiveUint16(-3.4)).toThrow(TypeError);
41
+ });
42
+
43
+ test('returns the same value for valid inputs', () => {
44
+ expect(asPositiveUint16(5)).toBe(5);
45
+ expect(asPositiveUint16(1)).toBe(1);
46
+ expect(asPositiveUint16(65535)).toBe(65535);
47
+ });
48
+
49
+ test.each([
50
+ { name: 'Number.NaN', value: Number.NaN },
51
+ { name: 'Number.POSITIVE_INFINITY', value: Number.POSITIVE_INFINITY },
52
+ { name: 'Number.NEGATIVE_INFINITY', value: Number.NEGATIVE_INFINITY },
53
+ { name: '1.2', value: 1.2 },
54
+ { name: '-3.4', value: -3.4 },
55
+ { name: '0', value: 0 },
56
+ { name: '-1', value: -1 },
57
+ { name: '65536', value: 65536 },
58
+ ] as const)(
59
+ `asPositiveUint16($name) should throw a TypeError`,
60
+ ({ value }) => {
61
+ expect(() => asPositiveUint16(value)).toThrow(
62
+ new TypeError(
63
+ `Expected a positive integer in [1, 2^16), got: ${value}`,
64
+ ),
65
+ );
66
+ },
67
+ );
68
+ });
69
+
70
+ describe('isPositiveUint16', () => {
71
+ test('correctly identifies positive uint16 values', () => {
72
+ expect(isPositiveUint16(1)).toBe(true);
73
+ expect(isPositiveUint16(1000)).toBe(true);
74
+ expect(isPositiveUint16(65535)).toBe(true);
75
+ expect(isPositiveUint16(32768)).toBe(true);
76
+ });
77
+
78
+ test('correctly identifies zero', () => {
79
+ expect(isPositiveUint16(0)).toBe(false);
80
+ });
81
+
82
+ test('correctly identifies values outside uint16 range', () => {
83
+ expect(isPositiveUint16(65536)).toBe(false);
84
+ expect(isPositiveUint16(100000)).toBe(false);
85
+ });
86
+
87
+ test('correctly identifies negative integers', () => {
88
+ expect(isPositiveUint16(-1)).toBe(false);
89
+ expect(isPositiveUint16(-42)).toBe(false);
90
+ });
91
+
92
+ test('correctly identifies non-integers', () => {
93
+ expect(isPositiveUint16(Number.NaN)).toBe(false);
94
+ expect(isPositiveUint16(Number.POSITIVE_INFINITY)).toBe(false);
95
+ expect(isPositiveUint16(Number.NEGATIVE_INFINITY)).toBe(false);
96
+ expect(isPositiveUint16(1.2)).toBe(false);
97
+ expect(isPositiveUint16(-3.4)).toBe(false);
98
+ });
99
+ });
100
+
101
+ describe('PositiveUint16.is', () => {
102
+ test('same as isPositiveUint16 function', () => {
103
+ expect(PositiveUint16.is(5)).toBe(isPositiveUint16(5));
104
+ expect(PositiveUint16.is(0)).toBe(isPositiveUint16(0));
105
+ expect(PositiveUint16.is(-1)).toBe(isPositiveUint16(-1));
106
+ });
107
+ });
108
+
109
+ describe('constants', () => {
110
+ test('MIN_VALUE and MAX_VALUE', () => {
111
+ expect(PositiveUint16.MIN_VALUE).toBe(1);
112
+ expect(PositiveUint16.MAX_VALUE).toBe(65535);
113
+ });
114
+ });
115
+
116
+ describe('mathematical operations', () => {
117
+ const a = asPositiveUint16(100);
118
+ const b = asPositiveUint16(50);
119
+
120
+ test('min and max', () => {
121
+ expect(PositiveUint16.min(a, b)).toBe(50);
122
+ expect(PositiveUint16.max(a, b)).toBe(100);
123
+ });
124
+
125
+ test('add (with clamping to positive uint16 range)', () => {
126
+ const result = PositiveUint16.add(
127
+ asPositiveUint16(65000),
128
+ asPositiveUint16(1000),
129
+ );
130
+ expect(result).toBe(65535); // clamped to max
131
+ expect(PositiveUint16.add(a, b)).toBe(150);
132
+ });
133
+
134
+ test('sub (never goes below 1)', () => {
135
+ expect(PositiveUint16.sub(a, b)).toBe(50);
136
+ expect(PositiveUint16.sub(b, a)).toBe(1); // clamped to 1
137
+ });
138
+
139
+ test('mul (with clamping to positive uint16 range)', () => {
140
+ const result = PositiveUint16.mul(
141
+ asPositiveUint16(1000),
142
+ asPositiveUint16(100),
143
+ );
144
+ expect(result).toBe(65535); // clamped to max
145
+ expect(
146
+ PositiveUint16.mul(asPositiveUint16(10), asPositiveUint16(5)),
147
+ ).toBe(50);
148
+ });
149
+
150
+ test('div (floor division, never goes below 1)', () => {
151
+ expect(PositiveUint16.div(a, asPositiveUint16(50))).toBe(2);
152
+ expect(PositiveUint16.div(asPositiveUint16(7), asPositiveUint16(3))).toBe(
153
+ 2,
154
+ );
155
+ expect(
156
+ PositiveUint16.div(asPositiveUint16(50), asPositiveUint16(100)),
157
+ ).toBe(1); // floor(50/100) = 0, clamped to 1
158
+ });
159
+
160
+ test('pow (with clamping to positive uint16 range)', () => {
161
+ const result = PositiveUint16.pow(
162
+ asPositiveUint16(256),
163
+ asPositiveUint16(3),
164
+ );
165
+ expect(result).toBe(65535); // clamped to max
166
+ expect(PositiveUint16.pow(asPositiveUint16(2), asPositiveUint16(3))).toBe(
167
+ 8,
168
+ );
169
+ });
170
+ });
171
+
172
+ describe('random', () => {
173
+ test('generates positive uint16 values within specified range', () => {
174
+ const min = 1;
175
+ const max = 20;
176
+
177
+ for (let i = 0; i < 10; i++) {
178
+ const result = PositiveUint16.random(min, max);
179
+ expect(result).toBeGreaterThanOrEqual(min);
180
+ expect(result).toBeLessThanOrEqual(max);
181
+ expect(PositiveUint16.is(result)).toBe(true);
182
+ expect(Number.isInteger(result)).toBe(true);
183
+ expect(result).toBeGreaterThan(0);
184
+ }
185
+ });
186
+
187
+ test('generates values within PositiveUint16 range', () => {
188
+ for (let i = 0; i < 10; i++) {
189
+ const result = PositiveUint16.random(1, 30);
190
+ expect(result).toBeGreaterThanOrEqual(1);
191
+ expect(result).toBeLessThanOrEqual(65535);
192
+ }
193
+ });
194
+ });
195
+
196
+ describe('type assertions', () => {
197
+ test('type relationships', () => {
198
+ expectType<PositiveUint16, number>('<=');
199
+
200
+ expectTypeOf(asPositiveUint16(100)).toExtend<PositiveUint16>();
201
+ });
202
+ });
203
+ });
@@ -0,0 +1,188 @@
1
+ import { expectType } from '../../expect-type.mjs';
2
+ import { TsVerifiedInternals } from '../refined-number-utils.mjs';
3
+
4
+ type ElementType = PositiveUint32;
5
+
6
+ const typeNameInMessage = 'a positive integer in [1, 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
+ 1,
25
+ number
26
+ >({
27
+ integerOrSafeInteger: 'SafeInteger',
28
+ MIN_VALUE: 1,
29
+ MAX_VALUE: 2 ** 32 - 1,
30
+ typeNameInMessage,
31
+ } as const);
32
+
33
+ /**
34
+ * Checks if a number is a PositiveUint32 (32-bit positive unsigned integer in the range [1, 2^32)).
35
+ * @param value The value to check.
36
+ * @returns `true` if the value is a PositiveUint32, `false` otherwise.
37
+ */
38
+ export const isPositiveUint32 = is;
39
+
40
+ /**
41
+ * Casts a number to a PositiveUint32 type.
42
+ * @param value The value to cast.
43
+ * @returns The value as a PositiveUint32 type.
44
+ * @throws {TypeError} If the value is not a positive integer in [1, 2^32).
45
+ * @example
46
+ * ```typescript
47
+ * const x = asPositiveUint32(1000); // PositiveUint32
48
+ * const y = asPositiveUint32(4294967295); // PositiveUint32
49
+ * // asPositiveUint32(0); // throws TypeError
50
+ * // asPositiveUint32(-1); // throws TypeError
51
+ * // asPositiveUint32(4294967296); // throws TypeError
52
+ * ```
53
+ */
54
+ export const asPositiveUint32 = castType;
55
+
56
+ /**
57
+ * Namespace providing type-safe arithmetic operations for 32-bit positive unsigned integers.
58
+ *
59
+ * All operations automatically clamp results to the valid PositiveUint32 range [1, 4294967295].
60
+ * This ensures that all arithmetic maintains the 32-bit positive unsigned integer constraint,
61
+ * with results below 1 clamped to MIN_VALUE and overflow results clamped to MAX_VALUE.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const a = asPositiveUint32(4000000000);
66
+ * const b = asPositiveUint32(1000000000);
67
+ *
68
+ * // Arithmetic operations with automatic clamping and positive constraint
69
+ * const sum = PositiveUint32.add(a, b); // PositiveUint32 (4294967295 - clamped to MAX_VALUE)
70
+ * const diff = PositiveUint32.sub(a, b); // PositiveUint32 (3000000000)
71
+ * const reverseDiff = PositiveUint32.sub(b, a); // PositiveUint32 (1 - clamped to MIN_VALUE)
72
+ * const product = PositiveUint32.mul(a, b); // PositiveUint32 (4294967295 - clamped due to overflow)
73
+ *
74
+ * // Range operations (maintaining positive constraint)
75
+ * const clamped = PositiveUint32.clamp(-100); // PositiveUint32 (1)
76
+ * const minimum = PositiveUint32.min(a, b); // PositiveUint32 (1000000000)
77
+ * const maximum = PositiveUint32.max(a, b); // PositiveUint32 (4000000000)
78
+ *
79
+ * // Utility operations
80
+ * const random = PositiveUint32.random(); // PositiveUint32 (random value in [1, 4294967295])
81
+ * const power = PositiveUint32.pow(asPositiveUint32(2), asPositiveUint32(20)); // PositiveUint32 (1048576)
82
+ * ```
83
+ */
84
+ export const PositiveUint32 = {
85
+ /**
86
+ * Type guard to check if a value is a PositiveUint32.
87
+ * @param value The value to check.
88
+ * @returns `true` if the value is a 32-bit positive unsigned integer, `false` otherwise.
89
+ */
90
+ is,
91
+
92
+ /**
93
+ * The minimum value for a 32-bit positive unsigned integer.
94
+ * @readonly
95
+ */
96
+ MIN_VALUE,
97
+
98
+ /**
99
+ * The maximum value for a 32-bit positive unsigned integer.
100
+ * @readonly
101
+ */
102
+ MAX_VALUE,
103
+
104
+ /**
105
+ * Returns the smaller of two PositiveUint32 values.
106
+ * @param a The first PositiveUint32.
107
+ * @param b The second PositiveUint32.
108
+ * @returns The minimum value as a PositiveUint32.
109
+ */
110
+ min: min_,
111
+
112
+ /**
113
+ * Returns the larger of two PositiveUint32 values.
114
+ * @param a The first PositiveUint32.
115
+ * @param b The second PositiveUint32.
116
+ * @returns The maximum value as a PositiveUint32.
117
+ */
118
+ max: max_,
119
+
120
+ /**
121
+ * Clamps a number to the PositiveUint32 range.
122
+ * @param value The number to clamp.
123
+ * @returns The value clamped to [1, 4294967295] as a PositiveUint32.
124
+ */
125
+ clamp,
126
+
127
+ /**
128
+ * Generates a random PositiveUint32 value within the valid range.
129
+ * @returns A random PositiveUint32 between 1 and 4294967295.
130
+ */
131
+ random,
132
+
133
+ /**
134
+ * Raises a PositiveUint32 to the power of another PositiveUint32.
135
+ * @param a The base PositiveUint32.
136
+ * @param b The exponent PositiveUint32.
137
+ * @returns `a ** b` clamped to [1, 4294967295] as a PositiveUint32.
138
+ */
139
+ pow,
140
+
141
+ /**
142
+ * Adds two PositiveUint32 values.
143
+ * @param a The first PositiveUint32.
144
+ * @param b The second PositiveUint32.
145
+ * @returns `a + b` clamped to [1, 4294967295] as a PositiveUint32.
146
+ */
147
+ add,
148
+
149
+ /**
150
+ * Subtracts one PositiveUint32 from another.
151
+ * @param a The minuend PositiveUint32.
152
+ * @param b The subtrahend PositiveUint32.
153
+ * @returns `a - b` clamped to [1, 4294967295] as a PositiveUint32 (minimum 1).
154
+ */
155
+ sub,
156
+
157
+ /**
158
+ * Multiplies two PositiveUint32 values.
159
+ * @param a The first PositiveUint32.
160
+ * @param b The second PositiveUint32.
161
+ * @returns `a * b` clamped to [1, 4294967295] as a PositiveUint32.
162
+ */
163
+ mul,
164
+
165
+ /**
166
+ * Divides one PositiveUint32 by another using floor division.
167
+ * @param a The dividend PositiveUint32.
168
+ * @param b The divisor PositiveUint32.
169
+ * @returns `⌊a / b⌋` clamped to [1, 4294967295] as a PositiveUint32.
170
+ */
171
+ div,
172
+ } as const;
173
+
174
+ expectType<
175
+ keyof typeof PositiveUint32,
176
+ keyof TsVerifiedInternals.RefinedNumberUtils.NumberClass<
177
+ ElementType,
178
+ 'int' | 'positive' | 'range'
179
+ >
180
+ >('=');
181
+
182
+ expectType<
183
+ typeof PositiveUint32,
184
+ TsVerifiedInternals.RefinedNumberUtils.NumberClass<
185
+ ElementType,
186
+ 'int' | 'positive' | 'range'
187
+ >
188
+ >('<=');