ts-data-forge 1.0.0 → 1.0.1
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/README.md +1 -1
- package/dist/array/array-utils.d.mts +2617 -0
- package/dist/array/array-utils.d.mts.map +1 -0
- package/dist/array/array-utils.mjs +2915 -0
- package/dist/array/array-utils.mjs.map +1 -0
- package/dist/array/index.d.mts +3 -0
- package/dist/array/index.d.mts.map +1 -0
- package/dist/array/index.mjs +3 -0
- package/dist/array/index.mjs.map +1 -0
- package/dist/array/tuple-utils.d.mts +421 -0
- package/dist/array/tuple-utils.d.mts.map +1 -0
- package/dist/array/tuple-utils.mjs +391 -0
- package/dist/array/tuple-utils.mjs.map +1 -0
- package/dist/collections/imap-mapped.d.mts +445 -0
- package/dist/collections/imap-mapped.d.mts.map +1 -0
- package/dist/collections/imap-mapped.mjs +424 -0
- package/dist/collections/imap-mapped.mjs.map +1 -0
- package/dist/collections/imap.d.mts +359 -0
- package/dist/collections/imap.d.mts.map +1 -0
- package/dist/collections/imap.mjs +338 -0
- package/dist/collections/imap.mjs.map +1 -0
- package/dist/collections/index.d.mts +7 -0
- package/dist/collections/index.d.mts.map +1 -0
- package/dist/collections/index.mjs +7 -0
- package/dist/collections/index.mjs.map +1 -0
- package/dist/collections/iset-mapped.d.mts +576 -0
- package/dist/collections/iset-mapped.d.mts.map +1 -0
- package/dist/collections/iset-mapped.mjs +522 -0
- package/dist/collections/iset-mapped.mjs.map +1 -0
- package/dist/collections/iset.d.mts +426 -0
- package/dist/collections/iset.d.mts.map +1 -0
- package/dist/collections/iset.mjs +437 -0
- package/dist/collections/iset.mjs.map +1 -0
- package/dist/collections/queue.d.mts +190 -0
- package/dist/collections/queue.d.mts.map +1 -0
- package/dist/collections/queue.mjs +317 -0
- package/dist/collections/queue.mjs.map +1 -0
- package/dist/collections/stack.d.mts +210 -0
- package/dist/collections/stack.d.mts.map +1 -0
- package/dist/collections/stack.mjs +353 -0
- package/dist/collections/stack.mjs.map +1 -0
- package/dist/expect-type.d.mts +199 -0
- package/dist/expect-type.d.mts.map +1 -0
- package/dist/expect-type.mjs +201 -0
- package/dist/expect-type.mjs.map +1 -0
- package/dist/functional/index.d.mts +5 -0
- package/dist/functional/index.d.mts.map +1 -0
- package/dist/functional/index.mjs +5 -0
- package/dist/functional/index.mjs.map +1 -0
- package/dist/functional/match.d.mts +215 -0
- package/dist/functional/match.d.mts.map +1 -0
- package/dist/functional/match.mjs +139 -0
- package/dist/functional/match.mjs.map +1 -0
- package/dist/functional/optional.d.mts +517 -0
- package/dist/functional/optional.d.mts.map +1 -0
- package/dist/functional/optional.mjs +532 -0
- package/dist/functional/optional.mjs.map +1 -0
- package/dist/functional/pipe.d.mts +185 -0
- package/dist/functional/pipe.d.mts.map +1 -0
- package/dist/functional/pipe.mjs +129 -0
- package/dist/functional/pipe.mjs.map +1 -0
- package/dist/functional/result.d.mts +796 -0
- package/dist/functional/result.d.mts.map +1 -0
- package/dist/functional/result.mjs +844 -0
- package/dist/functional/result.mjs.map +1 -0
- package/dist/globals.d.mts +38 -0
- package/dist/guard/has-key.d.mts +100 -0
- package/dist/guard/has-key.d.mts.map +1 -0
- package/dist/guard/has-key.mjs +94 -0
- package/dist/guard/has-key.mjs.map +1 -0
- package/dist/guard/index.d.mts +8 -0
- package/dist/guard/index.d.mts.map +1 -0
- package/dist/guard/index.mjs +8 -0
- package/dist/guard/index.mjs.map +1 -0
- package/dist/guard/is-non-empty-string.d.mts +106 -0
- package/dist/guard/is-non-empty-string.d.mts.map +1 -0
- package/dist/guard/is-non-empty-string.mjs +108 -0
- package/dist/guard/is-non-empty-string.mjs.map +1 -0
- package/dist/guard/is-non-null-object.d.mts +105 -0
- package/dist/guard/is-non-null-object.d.mts.map +1 -0
- package/dist/guard/is-non-null-object.mjs +108 -0
- package/dist/guard/is-non-null-object.mjs.map +1 -0
- package/dist/guard/is-primitive.d.mts +146 -0
- package/dist/guard/is-primitive.d.mts.map +1 -0
- package/dist/guard/is-primitive.mjs +161 -0
- package/dist/guard/is-primitive.mjs.map +1 -0
- package/dist/guard/is-record.d.mts +151 -0
- package/dist/guard/is-record.d.mts.map +1 -0
- package/dist/guard/is-record.mjs +155 -0
- package/dist/guard/is-record.mjs.map +1 -0
- package/dist/guard/is-type.d.mts +430 -0
- package/dist/guard/is-type.d.mts.map +1 -0
- package/dist/guard/is-type.mjs +432 -0
- package/dist/guard/is-type.mjs.map +1 -0
- package/dist/guard/key-is-in.d.mts +158 -0
- package/dist/guard/key-is-in.d.mts.map +1 -0
- package/dist/guard/key-is-in.mjs +160 -0
- package/dist/guard/key-is-in.mjs.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +61 -0
- package/dist/index.mjs.map +1 -0
- package/dist/iterator/index.d.mts +2 -0
- package/dist/iterator/index.d.mts.map +1 -0
- package/dist/iterator/index.mjs +2 -0
- package/dist/iterator/index.mjs.map +1 -0
- package/dist/iterator/range.d.mts +97 -0
- package/dist/iterator/range.d.mts.map +1 -0
- package/dist/iterator/range.mjs +130 -0
- package/dist/iterator/range.mjs.map +1 -0
- package/dist/json/index.d.mts +2 -0
- package/dist/json/index.d.mts.map +1 -0
- package/dist/json/index.mjs +2 -0
- package/dist/json/index.mjs.map +1 -0
- package/dist/json/json.d.mts +597 -0
- package/dist/json/json.d.mts.map +1 -0
- package/dist/json/json.mjs +687 -0
- package/dist/json/json.mjs.map +1 -0
- package/dist/number/branded-types/finite-number.d.mts +291 -0
- package/dist/number/branded-types/finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/finite-number.mjs +296 -0
- package/dist/number/branded-types/finite-number.mjs.map +1 -0
- package/dist/number/branded-types/index.d.mts +27 -0
- package/dist/number/branded-types/index.d.mts.map +1 -0
- package/dist/number/branded-types/index.mjs +27 -0
- package/dist/number/branded-types/index.mjs.map +1 -0
- package/dist/number/branded-types/int.d.mts +242 -0
- package/dist/number/branded-types/int.d.mts.map +1 -0
- package/dist/number/branded-types/int.mjs +239 -0
- package/dist/number/branded-types/int.mjs.map +1 -0
- package/dist/number/branded-types/int16.d.mts +162 -0
- package/dist/number/branded-types/int16.d.mts.map +1 -0
- package/dist/number/branded-types/int16.mjs +141 -0
- package/dist/number/branded-types/int16.mjs.map +1 -0
- package/dist/number/branded-types/int32.d.mts +155 -0
- package/dist/number/branded-types/int32.d.mts.map +1 -0
- package/dist/number/branded-types/int32.mjs +142 -0
- package/dist/number/branded-types/int32.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts +165 -0
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int16.d.mts +160 -0
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int16.mjs +138 -0
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-negative-int32.d.mts +156 -0
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-negative-int32.mjs +138 -0
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts +154 -0
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs +160 -0
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int.d.mts +131 -0
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int.mjs +128 -0
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int16.d.mts +166 -0
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int16.mjs +145 -0
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-int32.d.mts +158 -0
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-int32.mjs +145 -0
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts +148 -0
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs +145 -0
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts +160 -0
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint16.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts +156 -0
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/non-zero-uint32.mjs +140 -0
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -0
- package/dist/number/branded-types/positive-finite-number.d.mts +171 -0
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -0
- package/dist/number/branded-types/positive-finite-number.mjs +165 -0
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -0
- package/dist/number/branded-types/positive-int.d.mts +270 -0
- package/dist/number/branded-types/positive-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int.mjs +257 -0
- package/dist/number/branded-types/positive-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-int16.d.mts +162 -0
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int16.mjs +139 -0
- package/dist/number/branded-types/positive-int16.mjs.map +1 -0
- package/dist/number/branded-types/positive-int32.d.mts +158 -0
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-int32.mjs +139 -0
- package/dist/number/branded-types/positive-int32.mjs.map +1 -0
- package/dist/number/branded-types/positive-safe-int.d.mts +152 -0
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/positive-safe-int.mjs +138 -0
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint16.d.mts +160 -0
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint16.mjs +139 -0
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -0
- package/dist/number/branded-types/positive-uint32.d.mts +156 -0
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -0
- package/dist/number/branded-types/positive-uint32.mjs +139 -0
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -0
- package/dist/number/branded-types/safe-int.d.mts +243 -0
- package/dist/number/branded-types/safe-int.d.mts.map +1 -0
- package/dist/number/branded-types/safe-int.mjs +240 -0
- package/dist/number/branded-types/safe-int.mjs.map +1 -0
- package/dist/number/branded-types/safe-uint.d.mts +151 -0
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -0
- package/dist/number/branded-types/safe-uint.mjs +138 -0
- package/dist/number/branded-types/safe-uint.mjs.map +1 -0
- package/dist/number/branded-types/uint.d.mts +144 -0
- package/dist/number/branded-types/uint.d.mts.map +1 -0
- package/dist/number/branded-types/uint.mjs +132 -0
- package/dist/number/branded-types/uint.mjs.map +1 -0
- package/dist/number/branded-types/uint16.d.mts +157 -0
- package/dist/number/branded-types/uint16.d.mts.map +1 -0
- package/dist/number/branded-types/uint16.mjs +137 -0
- package/dist/number/branded-types/uint16.mjs.map +1 -0
- package/dist/number/branded-types/uint32.d.mts +185 -0
- package/dist/number/branded-types/uint32.d.mts.map +1 -0
- package/dist/number/branded-types/uint32.mjs +169 -0
- package/dist/number/branded-types/uint32.mjs.map +1 -0
- package/dist/number/enum/index.d.mts +3 -0
- package/dist/number/enum/index.d.mts.map +1 -0
- package/dist/number/enum/index.mjs +3 -0
- package/dist/number/enum/index.mjs.map +1 -0
- package/dist/number/enum/int8.d.mts +202 -0
- package/dist/number/enum/int8.d.mts.map +1 -0
- package/dist/number/enum/int8.mjs +296 -0
- package/dist/number/enum/int8.mjs.map +1 -0
- package/dist/number/enum/uint8.d.mts +128 -0
- package/dist/number/enum/uint8.d.mts.map +1 -0
- package/dist/number/enum/uint8.mjs +251 -0
- package/dist/number/enum/uint8.mjs.map +1 -0
- package/dist/number/index.d.mts +5 -0
- package/dist/number/index.d.mts.map +1 -0
- package/dist/number/index.mjs +31 -0
- package/dist/number/index.mjs.map +1 -0
- package/dist/number/num.d.mts +515 -0
- package/dist/number/num.d.mts.map +1 -0
- package/dist/number/num.mjs +513 -0
- package/dist/number/num.mjs.map +1 -0
- package/dist/number/refined-number-utils.d.mts +191 -0
- package/dist/number/refined-number-utils.d.mts.map +1 -0
- package/dist/number/refined-number-utils.mjs +179 -0
- package/dist/number/refined-number-utils.mjs.map +1 -0
- package/dist/object/index.d.mts +2 -0
- package/dist/object/index.d.mts.map +1 -0
- package/dist/object/index.mjs +2 -0
- package/dist/object/index.mjs.map +1 -0
- package/dist/object/object.d.mts +296 -0
- package/dist/object/object.d.mts.map +1 -0
- package/dist/object/object.mjs +295 -0
- package/dist/object/object.mjs.map +1 -0
- package/dist/others/cast-mutable.d.mts +110 -0
- package/dist/others/cast-mutable.d.mts.map +1 -0
- package/dist/others/cast-mutable.mjs +114 -0
- package/dist/others/cast-mutable.mjs.map +1 -0
- package/dist/others/cast-readonly.d.mts +189 -0
- package/dist/others/cast-readonly.d.mts.map +1 -0
- package/dist/others/cast-readonly.mjs +193 -0
- package/dist/others/cast-readonly.mjs.map +1 -0
- package/dist/others/if-then.d.mts +98 -0
- package/dist/others/if-then.d.mts.map +1 -0
- package/dist/others/if-then.mjs +100 -0
- package/dist/others/if-then.mjs.map +1 -0
- package/dist/others/index.d.mts +8 -0
- package/dist/others/index.d.mts.map +1 -0
- package/dist/others/index.mjs +8 -0
- package/dist/others/index.mjs.map +1 -0
- package/dist/others/map-nullable.d.mts +151 -0
- package/dist/others/map-nullable.d.mts.map +1 -0
- package/dist/others/map-nullable.mjs +159 -0
- package/dist/others/map-nullable.mjs.map +1 -0
- package/dist/others/memoize-function.d.mts +173 -0
- package/dist/others/memoize-function.d.mts.map +1 -0
- package/dist/others/memoize-function.mjs +189 -0
- package/dist/others/memoize-function.mjs.map +1 -0
- package/dist/others/tuple.d.mts +159 -0
- package/dist/others/tuple.d.mts.map +1 -0
- package/dist/others/tuple.mjs +161 -0
- package/dist/others/tuple.mjs.map +1 -0
- package/dist/others/unknown-to-string.d.mts +180 -0
- package/dist/others/unknown-to-string.d.mts.map +1 -0
- package/dist/others/unknown-to-string.mjs +211 -0
- package/dist/others/unknown-to-string.mjs.map +1 -0
- package/dist/tsconfig.json +1 -0
- package/package.json +16 -14
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { TsVerifiedInternals } from '../refined-number-utils.mjs';
|
|
2
|
+
|
|
3
|
+
const typeNameInMessage = 'a positive integer';
|
|
4
|
+
const { MIN_VALUE, min: min_, max: max_, pow, add, sub, mul, div, random, is, castType, clamp, } = TsVerifiedInternals.RefinedNumberUtils.operatorsForInteger({
|
|
5
|
+
integerOrSafeInteger: 'Integer',
|
|
6
|
+
MIN_VALUE: 1,
|
|
7
|
+
MAX_VALUE: undefined,
|
|
8
|
+
typeNameInMessage,
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Type guard that checks if a value is a positive integer.
|
|
12
|
+
*
|
|
13
|
+
* A positive integer is any integer greater than zero (>= 1).
|
|
14
|
+
* This excludes zero, negative numbers, and non-integers.
|
|
15
|
+
*
|
|
16
|
+
* @param value - The value to check
|
|
17
|
+
* @returns `true` if the value is a positive integer, `false` otherwise
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* isPositiveInt(5); // true
|
|
22
|
+
* isPositiveInt(1); // true
|
|
23
|
+
* isPositiveInt(0); // false (zero is not positive)
|
|
24
|
+
* isPositiveInt(-1); // false (negative)
|
|
25
|
+
* isPositiveInt(5.5); // false (not an integer)
|
|
26
|
+
* isPositiveInt(NaN); // false
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
const isPositiveInt = is;
|
|
30
|
+
/**
|
|
31
|
+
* Casts a number to a PositiveInt branded type.
|
|
32
|
+
*
|
|
33
|
+
* This function validates that the input is a positive integer (>= 1)
|
|
34
|
+
* and returns it with the PositiveInt brand. This ensures type safety
|
|
35
|
+
* for operations that require strictly positive integer values.
|
|
36
|
+
*
|
|
37
|
+
* @param value - The value to cast
|
|
38
|
+
* @returns The value as a PositiveInt branded type
|
|
39
|
+
* @throws {TypeError} If the value is not a positive integer
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const count = asPositiveInt(5); // PositiveInt
|
|
44
|
+
* const length = asPositiveInt(100); // PositiveInt
|
|
45
|
+
* const one = asPositiveInt(1); // PositiveInt (minimum valid)
|
|
46
|
+
*
|
|
47
|
+
* // These throw TypeError:
|
|
48
|
+
* // asPositiveInt(0); // Zero is not positive
|
|
49
|
+
* // asPositiveInt(-1); // Negative numbers not allowed
|
|
50
|
+
* // asPositiveInt(5.5); // Not an integer
|
|
51
|
+
* // asPositiveInt(Infinity); // Not finite
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
const asPositiveInt = castType;
|
|
55
|
+
/**
|
|
56
|
+
* Namespace providing type-safe operations for PositiveInt branded types.
|
|
57
|
+
*
|
|
58
|
+
* PositiveInt represents integers that are strictly greater than zero (>= 1).
|
|
59
|
+
* All operations automatically clamp results to maintain the positive constraint,
|
|
60
|
+
* ensuring that arithmetic operations never produce zero or negative values.
|
|
61
|
+
*
|
|
62
|
+
* This type is essential for:
|
|
63
|
+
* - Array lengths and sizes (length >= 1)
|
|
64
|
+
* - Counts and quantities that must be positive
|
|
65
|
+
* - Denominators in division operations
|
|
66
|
+
* - Loop counters and iteration counts
|
|
67
|
+
* - Database primary keys and IDs
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* // Type validation
|
|
72
|
+
* PositiveInt.is(5); // true
|
|
73
|
+
* PositiveInt.is(1); // true (minimum value)
|
|
74
|
+
* PositiveInt.is(0); // false
|
|
75
|
+
* PositiveInt.is(-1); // false
|
|
76
|
+
*
|
|
77
|
+
* // Automatic clamping in operations
|
|
78
|
+
* const a = asPositiveInt(10);
|
|
79
|
+
* const b = asPositiveInt(3);
|
|
80
|
+
*
|
|
81
|
+
* const sum = PositiveInt.add(a, b); // PositiveInt (13)
|
|
82
|
+
* const diff1 = PositiveInt.sub(a, b); // PositiveInt (7)
|
|
83
|
+
* const diff2 = PositiveInt.sub(b, a); // PositiveInt (1) - clamped!
|
|
84
|
+
* const product = PositiveInt.mul(a, b); // PositiveInt (30)
|
|
85
|
+
* const quotient = PositiveInt.div(a, b); // PositiveInt (3)
|
|
86
|
+
*
|
|
87
|
+
* // Edge case: division that would be < 1
|
|
88
|
+
* const small = PositiveInt.div(asPositiveInt(2), asPositiveInt(3)); // PositiveInt (1)
|
|
89
|
+
*
|
|
90
|
+
* // Range operations
|
|
91
|
+
* const minimum = PositiveInt.min(a, b); // PositiveInt (3)
|
|
92
|
+
* const maximum = PositiveInt.max(a, b); // PositiveInt (10)
|
|
93
|
+
*
|
|
94
|
+
* // Random generation
|
|
95
|
+
* const dice = PositiveInt.random(asPositiveInt(1), asPositiveInt(6)); // 1-6
|
|
96
|
+
* const id = PositiveInt.random(asPositiveInt(1000), asPositiveInt(9999)); // 4-digit ID
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
const PositiveInt = {
|
|
100
|
+
/**
|
|
101
|
+
* Type guard that checks if a value is a positive integer.
|
|
102
|
+
*
|
|
103
|
+
* @param value - The value to check
|
|
104
|
+
* @returns `true` if the value is a positive integer, `false` otherwise
|
|
105
|
+
*
|
|
106
|
+
* @see {@link isPositiveInt} for usage examples
|
|
107
|
+
*/
|
|
108
|
+
is,
|
|
109
|
+
/**
|
|
110
|
+
* The minimum value for a PositiveInt.
|
|
111
|
+
* @readonly
|
|
112
|
+
*/
|
|
113
|
+
MIN_VALUE,
|
|
114
|
+
/**
|
|
115
|
+
* Returns the minimum value from a list of positive integers.
|
|
116
|
+
*
|
|
117
|
+
* Since all inputs are guaranteed to be >= 1, the result is also guaranteed
|
|
118
|
+
* to be a positive integer.
|
|
119
|
+
*
|
|
120
|
+
* @param values - The positive integers to compare (at least one required)
|
|
121
|
+
* @returns The smallest value as a PositiveInt
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* PositiveInt.min(asPositiveInt(5), asPositiveInt(3)); // PositiveInt (3)
|
|
126
|
+
* PositiveInt.min(asPositiveInt(10), asPositiveInt(1), asPositiveInt(7)); // PositiveInt (1)
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
min: min_,
|
|
130
|
+
/**
|
|
131
|
+
* Returns the maximum value from a list of positive integers.
|
|
132
|
+
*
|
|
133
|
+
* @param values - The positive integers to compare (at least one required)
|
|
134
|
+
* @returns The largest value as a PositiveInt
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* PositiveInt.max(asPositiveInt(5), asPositiveInt(3)); // PositiveInt (5)
|
|
139
|
+
* PositiveInt.max(asPositiveInt(10), asPositiveInt(1), asPositiveInt(7)); // PositiveInt (10)
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
max: max_,
|
|
143
|
+
/**
|
|
144
|
+
* Clamps a number to the positive integer range.
|
|
145
|
+
*
|
|
146
|
+
* Since PositiveInt has a minimum value of 1, this function ensures
|
|
147
|
+
* that any input less than 1 is clamped to 1.
|
|
148
|
+
*
|
|
149
|
+
* @param value - The number to clamp
|
|
150
|
+
* @returns The value clamped to >= 1 as a PositiveInt
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* PositiveInt.clamp(5); // PositiveInt (5)
|
|
155
|
+
* PositiveInt.clamp(0); // PositiveInt (1) - clamped to minimum
|
|
156
|
+
* PositiveInt.clamp(-10); // PositiveInt (1) - clamped to minimum
|
|
157
|
+
* PositiveInt.clamp(100); // PositiveInt (100)
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
clamp,
|
|
161
|
+
/**
|
|
162
|
+
* Generates a random positive integer within the specified range (inclusive).
|
|
163
|
+
*
|
|
164
|
+
* Both bounds are inclusive, and both min and max must be positive integers.
|
|
165
|
+
* If min > max, they are automatically swapped.
|
|
166
|
+
*
|
|
167
|
+
* @param min - The minimum value (inclusive, must be >= 1)
|
|
168
|
+
* @param max - The maximum value (inclusive, must be >= min)
|
|
169
|
+
* @returns A random PositiveInt in the range [min, max]
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* // Dice roll
|
|
174
|
+
* const d6 = PositiveInt.random(asPositiveInt(1), asPositiveInt(6));
|
|
175
|
+
*
|
|
176
|
+
* // Random user ID
|
|
177
|
+
* const userId = PositiveInt.random(asPositiveInt(1000), asPositiveInt(9999));
|
|
178
|
+
*
|
|
179
|
+
* // Random page count
|
|
180
|
+
* const pages = PositiveInt.random(asPositiveInt(50), asPositiveInt(500));
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
random,
|
|
184
|
+
/**
|
|
185
|
+
* Raises a positive integer to a power, ensuring the result is never less than 1.
|
|
186
|
+
* @param a - The base positive integer
|
|
187
|
+
* @param b - The exponent positive integer
|
|
188
|
+
* @returns `a ** b` as a PositiveInt, but never less than 1
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* PositiveInt.pow(asPositiveInt(2), asPositiveInt(3)); // PositiveInt (8)
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
pow,
|
|
195
|
+
/**
|
|
196
|
+
* Adds two positive integers, ensuring the result is never less than 1.
|
|
197
|
+
* @param a - First positive integer
|
|
198
|
+
* @param b - Second positive integer
|
|
199
|
+
* @returns `a + b` as a PositiveInt, but never less than 1
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* PositiveInt.add(asPositiveInt(5), asPositiveInt(3)); // PositiveInt (8)
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
add,
|
|
206
|
+
/**
|
|
207
|
+
* Subtracts two positive integers, clamping the result to remain positive.
|
|
208
|
+
*
|
|
209
|
+
* If the mathematical result would be <= 0, it is clamped to 1 to maintain
|
|
210
|
+
* the positive integer constraint.
|
|
211
|
+
*
|
|
212
|
+
* @param a - The minuend (positive integer)
|
|
213
|
+
* @param b - The subtrahend (positive integer)
|
|
214
|
+
* @returns `max(1, a - b)` as a PositiveInt
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* PositiveInt.sub(asPositiveInt(8), asPositiveInt(3)); // PositiveInt (5)
|
|
219
|
+
* PositiveInt.sub(asPositiveInt(3), asPositiveInt(8)); // PositiveInt (1) - clamped
|
|
220
|
+
* PositiveInt.sub(asPositiveInt(5), asPositiveInt(5)); // PositiveInt (1) - clamped
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
sub,
|
|
224
|
+
/**
|
|
225
|
+
* Multiplies two positive integers, ensuring the result is never less than 1.
|
|
226
|
+
* @param a - First positive integer
|
|
227
|
+
* @param b - Second positive integer
|
|
228
|
+
* @returns `a * b` as a PositiveInt, but never less than 1
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* PositiveInt.mul(asPositiveInt(4), asPositiveInt(3)); // PositiveInt (12)
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
mul,
|
|
235
|
+
/**
|
|
236
|
+
* Divides two positive integers using floor division, clamping to remain positive.
|
|
237
|
+
*
|
|
238
|
+
* Performs mathematical floor division: `⌊a / b⌋`. If the result would be 0
|
|
239
|
+
* (when a < b), it is clamped to 1 to maintain the positive integer constraint.
|
|
240
|
+
*
|
|
241
|
+
* @param a - The dividend (positive integer)
|
|
242
|
+
* @param b - The divisor (positive integer, guaranteed non-zero)
|
|
243
|
+
* @returns `max(1, ⌊a / b⌋)` as a PositiveInt
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```typescript
|
|
247
|
+
* PositiveInt.div(asPositiveInt(10), asPositiveInt(3)); // PositiveInt (3)
|
|
248
|
+
* PositiveInt.div(asPositiveInt(9), asPositiveInt(3)); // PositiveInt (3)
|
|
249
|
+
* PositiveInt.div(asPositiveInt(2), asPositiveInt(3)); // PositiveInt (1) - clamped
|
|
250
|
+
* PositiveInt.div(asPositiveInt(1), asPositiveInt(5)); // PositiveInt (1) - clamped
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
div,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
export { PositiveInt, asPositiveInt, isPositiveInt };
|
|
257
|
+
//# sourceMappingURL=positive-int.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positive-int.mjs","sources":["../../../src/number/branded-types/positive-int.mts"],"sourcesContent":[null],"names":[],"mappings":";;AAKA,MAAM,iBAAiB,GAAG,oBAAoB;AAE9C,MAAM,EACJ,SAAS,EACT,GAAG,EAAE,IAAI,EACT,GAAG,EAAE,IAAI,EACT,GAAG,EACH,GAAG,EACH,GAAG,EACH,GAAG,EACH,GAAG,EACH,MAAM,EACN,EAAE,EACF,QAAQ,EACR,KAAK,GACN,GAAG,mBAAmB,CAAC,kBAAkB,CAAC,mBAAmB,CAI5D;AACA,IAAA,oBAAoB,EAAE,SAAS;AAC/B,IAAA,SAAS,EAAE,CAAC;AACZ,IAAA,SAAS,EAAE,SAAS;IACpB,iBAAiB;AACT,CAAA,CAAC;AAEX;;;;;;;;;;;;;;;;;;AAkBG;AACI,MAAM,aAAa,GAAG;AAE7B;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACI,MAAM,aAAa,GAAG;AAE7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CG;AACI,MAAM,WAAW,GAAG;AACzB;;;;;;;AAOG;IACH,EAAE;AAEF;;;AAGG;IACH,SAAS;AAET;;;;;;;;;;;;;;AAcG;AACH,IAAA,GAAG,EAAE,IAAI;AAET;;;;;;;;;;;AAWG;AACH,IAAA,GAAG,EAAE,IAAI;AAET;;;;;;;;;;;;;;;;AAgBG;IACH,KAAK;AAEL;;;;;;;;;;;;;;;;;;;;;AAqBG;IACH,MAAM;AAEN;;;;;;;;;AASG;IACH,GAAG;AAEH;;;;;;;;;AASG;IACH,GAAG;AAEH;;;;;;;;;;;;;;;;AAgBG;IACH,GAAG;AAEH;;;;;;;;;AASG;IACH,GAAG;AAEH;;;;;;;;;;;;;;;;;AAiBG;IACH,GAAG;;;;;"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a number is a PositiveInt16 (16-bit positive signed integer in the range [1, 2^15)).
|
|
3
|
+
* @param value The value to check.
|
|
4
|
+
* @returns `true` if the value is a PositiveInt16, `false` otherwise.
|
|
5
|
+
*/
|
|
6
|
+
export declare const isPositiveInt16: (a: number) => a is PositiveInt16;
|
|
7
|
+
/**
|
|
8
|
+
* Casts a number to a PositiveInt16 type.
|
|
9
|
+
* @param value The value to cast.
|
|
10
|
+
* @returns The value as a PositiveInt16 type.
|
|
11
|
+
* @throws {TypeError} If the value is not a positive integer in [1, 2^15).
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const x = asPositiveInt16(1000); // PositiveInt16
|
|
15
|
+
* const y = asPositiveInt16(32767); // PositiveInt16
|
|
16
|
+
* // asPositiveInt16(0); // throws TypeError
|
|
17
|
+
* // asPositiveInt16(-1); // throws TypeError
|
|
18
|
+
* // asPositiveInt16(32768); // throws TypeError
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare const asPositiveInt16: <N extends number>(x: N) => number & {
|
|
22
|
+
readonly NaNValue: false;
|
|
23
|
+
readonly "!=0": true;
|
|
24
|
+
readonly "> -2^16": true;
|
|
25
|
+
readonly "> -2^32": true;
|
|
26
|
+
readonly ">= -2^15": true;
|
|
27
|
+
readonly ">= -2^31": true;
|
|
28
|
+
readonly ">=0": true;
|
|
29
|
+
readonly "< 2^15": true;
|
|
30
|
+
readonly "< 2^16": true;
|
|
31
|
+
readonly "< 2^31": true;
|
|
32
|
+
readonly "< 2^32": true;
|
|
33
|
+
readonly Finite: true;
|
|
34
|
+
readonly Int: true;
|
|
35
|
+
readonly SafeInt: true;
|
|
36
|
+
} & Readonly<{
|
|
37
|
+
'TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3': unknown;
|
|
38
|
+
}> & N;
|
|
39
|
+
/**
|
|
40
|
+
* Namespace providing type-safe arithmetic operations for 16-bit positive integers.
|
|
41
|
+
*
|
|
42
|
+
* All operations automatically clamp results to the valid PositiveInt16 range [1, 32767].
|
|
43
|
+
* This ensures that all arithmetic maintains the 16-bit positive integer constraint,
|
|
44
|
+
* with results below 1 clamped to MIN_VALUE and overflow results clamped to MAX_VALUE.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const a = asPositiveInt16(30000);
|
|
49
|
+
* const b = asPositiveInt16(5000);
|
|
50
|
+
*
|
|
51
|
+
* // Arithmetic operations with automatic clamping
|
|
52
|
+
* const sum = PositiveInt16.add(a, b); // PositiveInt16 (32767 - clamped to MAX_VALUE)
|
|
53
|
+
* const diff = PositiveInt16.sub(a, b); // PositiveInt16 (25000)
|
|
54
|
+
* const reverseDiff = PositiveInt16.sub(b, a); // PositiveInt16 (1 - clamped to MIN_VALUE)
|
|
55
|
+
* const product = PositiveInt16.mul(a, b); // PositiveInt16 (32767 - clamped due to overflow)
|
|
56
|
+
*
|
|
57
|
+
* // Range operations
|
|
58
|
+
* const clamped = PositiveInt16.clamp(0); // PositiveInt16 (1)
|
|
59
|
+
* const minimum = PositiveInt16.min(a, b); // PositiveInt16 (5000)
|
|
60
|
+
* const maximum = PositiveInt16.max(a, b); // PositiveInt16 (30000)
|
|
61
|
+
*
|
|
62
|
+
* // Utility operations
|
|
63
|
+
* const random = PositiveInt16.random(); // PositiveInt16 (random value in [1, 32767])
|
|
64
|
+
* const power = PositiveInt16.pow(asPositiveInt16(2), asPositiveInt16(10)); // PositiveInt16 (1024)
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare const PositiveInt16: {
|
|
68
|
+
/**
|
|
69
|
+
* Type guard to check if a value is a PositiveInt16.
|
|
70
|
+
* @param value The value to check.
|
|
71
|
+
* @returns `true` if the value is a 16-bit positive integer, `false` otherwise.
|
|
72
|
+
*/
|
|
73
|
+
readonly is: (a: number) => a is PositiveInt16;
|
|
74
|
+
/**
|
|
75
|
+
* The minimum value for a 16-bit positive integer.
|
|
76
|
+
* @readonly
|
|
77
|
+
*/
|
|
78
|
+
readonly MIN_VALUE: 1;
|
|
79
|
+
/**
|
|
80
|
+
* The maximum value for a 16-bit positive integer.
|
|
81
|
+
* @readonly
|
|
82
|
+
*/
|
|
83
|
+
readonly MAX_VALUE: number;
|
|
84
|
+
/**
|
|
85
|
+
* Returns the smaller of two PositiveInt16 values.
|
|
86
|
+
* @param a The first PositiveInt16.
|
|
87
|
+
* @param b The second PositiveInt16.
|
|
88
|
+
* @returns The minimum value as a PositiveInt16.
|
|
89
|
+
*/
|
|
90
|
+
readonly min: (...values: readonly WithSmallInt<PositiveInt16, 40>[]) => PositiveInt16;
|
|
91
|
+
/**
|
|
92
|
+
* Returns the larger of two PositiveInt16 values.
|
|
93
|
+
* @param a The first PositiveInt16.
|
|
94
|
+
* @param b The second PositiveInt16.
|
|
95
|
+
* @returns The maximum value as a PositiveInt16.
|
|
96
|
+
*/
|
|
97
|
+
readonly max: (...values: readonly WithSmallInt<PositiveInt16, 40>[]) => PositiveInt16;
|
|
98
|
+
/**
|
|
99
|
+
* Clamps a number to the PositiveInt16 range.
|
|
100
|
+
* @param value The number to clamp.
|
|
101
|
+
* @returns The value clamped to [1, 32767] as a PositiveInt16.
|
|
102
|
+
*/
|
|
103
|
+
readonly clamp: (x: number) => PositiveInt16;
|
|
104
|
+
/**
|
|
105
|
+
* Generates a random PositiveInt16 value within the valid range.
|
|
106
|
+
* @returns A random PositiveInt16 between 1 and 32767.
|
|
107
|
+
*/
|
|
108
|
+
readonly random: (min: WithSmallInt<PositiveInt16, 40>, max: WithSmallInt<PositiveInt16, 40>) => PositiveInt16;
|
|
109
|
+
/**
|
|
110
|
+
* Raises a PositiveInt16 to the power of another PositiveInt16.
|
|
111
|
+
* @param a The base PositiveInt16.
|
|
112
|
+
* @param b The exponent PositiveInt16.
|
|
113
|
+
* @returns `a ** b` clamped to [1, 32767] as a PositiveInt16.
|
|
114
|
+
*/
|
|
115
|
+
readonly pow: (x: WithSmallInt<PositiveInt16, 40>, y: WithSmallInt<PositiveInt16, 40>) => PositiveInt16;
|
|
116
|
+
/**
|
|
117
|
+
* Adds two PositiveInt16 values.
|
|
118
|
+
* @param a The first PositiveInt16.
|
|
119
|
+
* @param b The second PositiveInt16.
|
|
120
|
+
* @returns `a + b` clamped to [1, 32767] as a PositiveInt16.
|
|
121
|
+
*/
|
|
122
|
+
readonly add: (x: WithSmallInt<PositiveInt16, 40>, y: WithSmallInt<PositiveInt16, 40>) => PositiveInt16;
|
|
123
|
+
/**
|
|
124
|
+
* Subtracts one PositiveInt16 from another.
|
|
125
|
+
* @param a The minuend PositiveInt16.
|
|
126
|
+
* @param b The subtrahend PositiveInt16.
|
|
127
|
+
* @returns `a - b` clamped to [1, 32767] as a PositiveInt16 (minimum 1).
|
|
128
|
+
*/
|
|
129
|
+
readonly sub: (x: WithSmallInt<PositiveInt16, 40>, y: WithSmallInt<PositiveInt16, 40>) => PositiveInt16;
|
|
130
|
+
/**
|
|
131
|
+
* Multiplies two PositiveInt16 values.
|
|
132
|
+
* @param a The first PositiveInt16.
|
|
133
|
+
* @param b The second PositiveInt16.
|
|
134
|
+
* @returns `a * b` clamped to [1, 32767] as a PositiveInt16.
|
|
135
|
+
*/
|
|
136
|
+
readonly mul: (x: WithSmallInt<PositiveInt16, 40>, y: WithSmallInt<PositiveInt16, 40>) => PositiveInt16;
|
|
137
|
+
/**
|
|
138
|
+
* Divides one PositiveInt16 by another using floor division.
|
|
139
|
+
* @param a The dividend PositiveInt16.
|
|
140
|
+
* @param b The divisor PositiveInt16.
|
|
141
|
+
* @returns `⌊a / b⌋` clamped to [1, 32767] as a PositiveInt16.
|
|
142
|
+
*/
|
|
143
|
+
readonly div: (x: WithSmallInt<PositiveInt16, 40>, y: 1 | 2 | 3 | 32 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 10 | 24 | 14 | 34 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 35 | 36 | 37 | 38 | 39 | NormalizeBrandUnion<number & {
|
|
144
|
+
readonly NaNValue: false;
|
|
145
|
+
readonly "!=0": true;
|
|
146
|
+
readonly "> -2^16": true;
|
|
147
|
+
readonly "> -2^32": true;
|
|
148
|
+
readonly ">= -2^15": true;
|
|
149
|
+
readonly ">= -2^31": true;
|
|
150
|
+
readonly ">=0": true;
|
|
151
|
+
readonly "< 2^15": true;
|
|
152
|
+
readonly "< 2^16": true;
|
|
153
|
+
readonly "< 2^31": true;
|
|
154
|
+
readonly "< 2^32": true;
|
|
155
|
+
readonly Finite: true;
|
|
156
|
+
readonly Int: true;
|
|
157
|
+
readonly SafeInt: true;
|
|
158
|
+
} & Readonly<{
|
|
159
|
+
'TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3': unknown;
|
|
160
|
+
}>>) => PositiveInt16;
|
|
161
|
+
};
|
|
162
|
+
//# sourceMappingURL=positive-int16.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positive-int16.d.mts","sourceRoot":"","sources":["../../../src/number/branded-types/positive-int16.mts"],"names":[],"mappings":"AAgCA;;;;GAIG;AACH,eAAO,MAAM,eAAe,mCAAK,CAAC;AAElC;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;MAAW,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,aAAa;IACxB;;;;OAIG;;IAGH;;;OAGG;;IAGH;;;OAGG;;IAGH;;;;;OAKG;;IAGH;;;;;OAKG;;IAGH;;;;OAIG;;IAGH;;;OAGG;;IAGH;;;;;OAKG;;IAGH;;;;;OAKG;;IAGH;;;;;OAKG;;IAGH;;;;;OAKG;;IAGH;;;;;OAKG;;;;;;;;;;;;;;;;;;;CAEK,CAAC"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { TsVerifiedInternals } from '../refined-number-utils.mjs';
|
|
2
|
+
|
|
3
|
+
const typeNameInMessage = 'a positive integer in [1, 2^15)';
|
|
4
|
+
const { MIN_VALUE, MAX_VALUE, min: min_, max: max_, pow, add, sub, mul, div, random, is, castType, clamp, } = TsVerifiedInternals.RefinedNumberUtils.operatorsForInteger({
|
|
5
|
+
integerOrSafeInteger: 'SafeInteger',
|
|
6
|
+
MIN_VALUE: 1,
|
|
7
|
+
MAX_VALUE: 2 ** 15 - 1,
|
|
8
|
+
typeNameInMessage,
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Checks if a number is a PositiveInt16 (16-bit positive signed integer in the range [1, 2^15)).
|
|
12
|
+
* @param value The value to check.
|
|
13
|
+
* @returns `true` if the value is a PositiveInt16, `false` otherwise.
|
|
14
|
+
*/
|
|
15
|
+
const isPositiveInt16 = is;
|
|
16
|
+
/**
|
|
17
|
+
* Casts a number to a PositiveInt16 type.
|
|
18
|
+
* @param value The value to cast.
|
|
19
|
+
* @returns The value as a PositiveInt16 type.
|
|
20
|
+
* @throws {TypeError} If the value is not a positive integer in [1, 2^15).
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const x = asPositiveInt16(1000); // PositiveInt16
|
|
24
|
+
* const y = asPositiveInt16(32767); // PositiveInt16
|
|
25
|
+
* // asPositiveInt16(0); // throws TypeError
|
|
26
|
+
* // asPositiveInt16(-1); // throws TypeError
|
|
27
|
+
* // asPositiveInt16(32768); // throws TypeError
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
const asPositiveInt16 = castType;
|
|
31
|
+
/**
|
|
32
|
+
* Namespace providing type-safe arithmetic operations for 16-bit positive integers.
|
|
33
|
+
*
|
|
34
|
+
* All operations automatically clamp results to the valid PositiveInt16 range [1, 32767].
|
|
35
|
+
* This ensures that all arithmetic maintains the 16-bit positive integer constraint,
|
|
36
|
+
* with results below 1 clamped to MIN_VALUE and overflow results clamped to MAX_VALUE.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const a = asPositiveInt16(30000);
|
|
41
|
+
* const b = asPositiveInt16(5000);
|
|
42
|
+
*
|
|
43
|
+
* // Arithmetic operations with automatic clamping
|
|
44
|
+
* const sum = PositiveInt16.add(a, b); // PositiveInt16 (32767 - clamped to MAX_VALUE)
|
|
45
|
+
* const diff = PositiveInt16.sub(a, b); // PositiveInt16 (25000)
|
|
46
|
+
* const reverseDiff = PositiveInt16.sub(b, a); // PositiveInt16 (1 - clamped to MIN_VALUE)
|
|
47
|
+
* const product = PositiveInt16.mul(a, b); // PositiveInt16 (32767 - clamped due to overflow)
|
|
48
|
+
*
|
|
49
|
+
* // Range operations
|
|
50
|
+
* const clamped = PositiveInt16.clamp(0); // PositiveInt16 (1)
|
|
51
|
+
* const minimum = PositiveInt16.min(a, b); // PositiveInt16 (5000)
|
|
52
|
+
* const maximum = PositiveInt16.max(a, b); // PositiveInt16 (30000)
|
|
53
|
+
*
|
|
54
|
+
* // Utility operations
|
|
55
|
+
* const random = PositiveInt16.random(); // PositiveInt16 (random value in [1, 32767])
|
|
56
|
+
* const power = PositiveInt16.pow(asPositiveInt16(2), asPositiveInt16(10)); // PositiveInt16 (1024)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
const PositiveInt16 = {
|
|
60
|
+
/**
|
|
61
|
+
* Type guard to check if a value is a PositiveInt16.
|
|
62
|
+
* @param value The value to check.
|
|
63
|
+
* @returns `true` if the value is a 16-bit positive integer, `false` otherwise.
|
|
64
|
+
*/
|
|
65
|
+
is,
|
|
66
|
+
/**
|
|
67
|
+
* The minimum value for a 16-bit positive integer.
|
|
68
|
+
* @readonly
|
|
69
|
+
*/
|
|
70
|
+
MIN_VALUE,
|
|
71
|
+
/**
|
|
72
|
+
* The maximum value for a 16-bit positive integer.
|
|
73
|
+
* @readonly
|
|
74
|
+
*/
|
|
75
|
+
MAX_VALUE,
|
|
76
|
+
/**
|
|
77
|
+
* Returns the smaller of two PositiveInt16 values.
|
|
78
|
+
* @param a The first PositiveInt16.
|
|
79
|
+
* @param b The second PositiveInt16.
|
|
80
|
+
* @returns The minimum value as a PositiveInt16.
|
|
81
|
+
*/
|
|
82
|
+
min: min_,
|
|
83
|
+
/**
|
|
84
|
+
* Returns the larger of two PositiveInt16 values.
|
|
85
|
+
* @param a The first PositiveInt16.
|
|
86
|
+
* @param b The second PositiveInt16.
|
|
87
|
+
* @returns The maximum value as a PositiveInt16.
|
|
88
|
+
*/
|
|
89
|
+
max: max_,
|
|
90
|
+
/**
|
|
91
|
+
* Clamps a number to the PositiveInt16 range.
|
|
92
|
+
* @param value The number to clamp.
|
|
93
|
+
* @returns The value clamped to [1, 32767] as a PositiveInt16.
|
|
94
|
+
*/
|
|
95
|
+
clamp,
|
|
96
|
+
/**
|
|
97
|
+
* Generates a random PositiveInt16 value within the valid range.
|
|
98
|
+
* @returns A random PositiveInt16 between 1 and 32767.
|
|
99
|
+
*/
|
|
100
|
+
random,
|
|
101
|
+
/**
|
|
102
|
+
* Raises a PositiveInt16 to the power of another PositiveInt16.
|
|
103
|
+
* @param a The base PositiveInt16.
|
|
104
|
+
* @param b The exponent PositiveInt16.
|
|
105
|
+
* @returns `a ** b` clamped to [1, 32767] as a PositiveInt16.
|
|
106
|
+
*/
|
|
107
|
+
pow,
|
|
108
|
+
/**
|
|
109
|
+
* Adds two PositiveInt16 values.
|
|
110
|
+
* @param a The first PositiveInt16.
|
|
111
|
+
* @param b The second PositiveInt16.
|
|
112
|
+
* @returns `a + b` clamped to [1, 32767] as a PositiveInt16.
|
|
113
|
+
*/
|
|
114
|
+
add,
|
|
115
|
+
/**
|
|
116
|
+
* Subtracts one PositiveInt16 from another.
|
|
117
|
+
* @param a The minuend PositiveInt16.
|
|
118
|
+
* @param b The subtrahend PositiveInt16.
|
|
119
|
+
* @returns `a - b` clamped to [1, 32767] as a PositiveInt16 (minimum 1).
|
|
120
|
+
*/
|
|
121
|
+
sub,
|
|
122
|
+
/**
|
|
123
|
+
* Multiplies two PositiveInt16 values.
|
|
124
|
+
* @param a The first PositiveInt16.
|
|
125
|
+
* @param b The second PositiveInt16.
|
|
126
|
+
* @returns `a * b` clamped to [1, 32767] as a PositiveInt16.
|
|
127
|
+
*/
|
|
128
|
+
mul,
|
|
129
|
+
/**
|
|
130
|
+
* Divides one PositiveInt16 by another using floor division.
|
|
131
|
+
* @param a The dividend PositiveInt16.
|
|
132
|
+
* @param b The divisor PositiveInt16.
|
|
133
|
+
* @returns `⌊a / b⌋` clamped to [1, 32767] as a PositiveInt16.
|
|
134
|
+
*/
|
|
135
|
+
div,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export { PositiveInt16, asPositiveInt16, isPositiveInt16 };
|
|
139
|
+
//# sourceMappingURL=positive-int16.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positive-int16.mjs","sources":["../../../src/number/branded-types/positive-int16.mts"],"sourcesContent":[null],"names":[],"mappings":";;AAKA,MAAM,iBAAiB,GAAG,iCAAiC;AAE3D,MAAM,EACJ,SAAS,EACT,SAAS,EACT,GAAG,EAAE,IAAI,EACT,GAAG,EAAE,IAAI,EACT,GAAG,EACH,GAAG,EACH,GAAG,EACH,GAAG,EACH,GAAG,EACH,MAAM,EACN,EAAE,EACF,QAAQ,EACR,KAAK,GACN,GAAG,mBAAmB,CAAC,kBAAkB,CAAC,mBAAmB,CAI5D;AACA,IAAA,oBAAoB,EAAE,aAAa;AACnC,IAAA,SAAS,EAAE,CAAC;AACZ,IAAA,SAAS,EAAE,CAAC,IAAI,EAAE,GAAG,CAAC;IACtB,iBAAiB;AACT,CAAA,CAAC;AAEX;;;;AAIG;AACI,MAAM,eAAe,GAAG;AAE/B;;;;;;;;;;;;;AAaG;AACI,MAAM,eAAe,GAAG;AAE/B;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;AACI,MAAM,aAAa,GAAG;AAC3B;;;;AAIG;IACH,EAAE;AAEF;;;AAGG;IACH,SAAS;AAET;;;AAGG;IACH,SAAS;AAET;;;;;AAKG;AACH,IAAA,GAAG,EAAE,IAAI;AAET;;;;;AAKG;AACH,IAAA,GAAG,EAAE,IAAI;AAET;;;;AAIG;IACH,KAAK;AAEL;;;AAGG;IACH,MAAM;AAEN;;;;;AAKG;IACH,GAAG;AAEH;;;;;AAKG;IACH,GAAG;AAEH;;;;;AAKG;IACH,GAAG;AAEH;;;;;AAKG;IACH,GAAG;AAEH;;;;;AAKG;IACH,GAAG;;;;;"}
|