ts-data-forge 11.0.0 → 11.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/dist/number/branded-types/finite-number.d.mts +47 -61
- package/dist/number/branded-types/finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/finite-number.mjs +37 -41
- package/dist/number/branded-types/finite-number.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +40 -46
- package/dist/number/branded-types/int.d.mts.map +1 -1
- package/dist/number/branded-types/int.mjs +29 -31
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/int16.d.mts +65 -58
- package/dist/number/branded-types/int16.d.mts.map +1 -1
- package/dist/number/branded-types/int16.mjs +49 -45
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.d.mts +65 -58
- package/dist/number/branded-types/int32.d.mts.map +1 -1
- package/dist/number/branded-types/int32.mjs +49 -45
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/negative-finite-number.d.mts +68 -59
- package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/negative-finite-number.mjs +52 -46
- package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/negative-int.d.mts +60 -48
- package/dist/number/branded-types/negative-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-int.mjs +40 -36
- package/dist/number/branded-types/negative-int.mjs.map +1 -1
- package/dist/number/branded-types/negative-safe-int.d.mts +57 -56
- package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-safe-int.mjs +40 -41
- package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +74 -64
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.mjs +58 -51
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +64 -57
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int16.mjs +47 -43
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +64 -58
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int32.mjs +47 -44
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.d.mts +64 -54
- package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.mjs +48 -41
- package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-int.d.mts +41 -146
- package/dist/number/branded-types/non-positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-int.mjs +27 -107
- package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.d.mts +54 -53
- package/dist/number/branded-types/non-positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.mjs +37 -39
- package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.d.mts +62 -49
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.mjs +46 -37
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int.d.mts +51 -38
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int.mjs +31 -27
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int16.d.mts +52 -53
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int16.mjs +36 -39
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.d.mts +52 -54
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int32.mjs +36 -40
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.d.mts +53 -54
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs +36 -40
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.d.mts +52 -53
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.mjs +35 -39
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.d.mts +52 -53
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.mjs +35 -39
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +74 -63
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/positive-finite-number.mjs +58 -50
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +46 -68
- package/dist/number/branded-types/positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int.mjs +34 -49
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +61 -57
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int16.mjs +44 -43
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +61 -57
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int32.mjs +44 -43
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +62 -57
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs +45 -43
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +61 -57
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint16.mjs +44 -43
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +61 -58
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint32.mjs +44 -44
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +43 -62
- package/dist/number/branded-types/safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/safe-int.mjs +35 -42
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +62 -57
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
- package/dist/number/branded-types/safe-uint.mjs +46 -44
- package/dist/number/branded-types/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.d.mts +63 -50
- package/dist/number/branded-types/uint.d.mts.map +1 -1
- package/dist/number/branded-types/uint.mjs +42 -39
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.d.mts +60 -58
- package/dist/number/branded-types/uint16.d.mts.map +1 -1
- package/dist/number/branded-types/uint16.mjs +44 -43
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.d.mts +59 -55
- package/dist/number/branded-types/uint32.d.mts.map +1 -1
- package/dist/number/branded-types/uint32.mjs +42 -42
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.d.mts +56 -65
- package/dist/number/enum/int8.d.mts.map +1 -1
- package/dist/number/enum/int8.mjs +44 -43
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.d.mts +60 -58
- package/dist/number/enum/uint8.d.mts.map +1 -1
- package/dist/number/enum/uint8.mjs +44 -43
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/package.json +2 -1
- package/src/number/branded-types/finite-number.mts +47 -61
- package/src/number/branded-types/int.mts +40 -46
- package/src/number/branded-types/int16.mts +65 -58
- package/src/number/branded-types/int32.mts +65 -58
- package/src/number/branded-types/negative-finite-number.mts +68 -68
- package/src/number/branded-types/negative-int.mts +60 -57
- package/src/number/branded-types/negative-safe-int.mts +57 -65
- package/src/number/branded-types/non-negative-finite-number.mts +74 -64
- package/src/number/branded-types/non-negative-int16.mts +64 -57
- package/src/number/branded-types/non-negative-int32.mts +64 -58
- package/src/number/branded-types/non-positive-finite-number.mts +64 -54
- package/src/number/branded-types/non-positive-int.mts +41 -146
- package/src/number/branded-types/non-positive-safe-int.mts +54 -53
- package/src/number/branded-types/non-zero-finite-number.mts +62 -51
- package/src/number/branded-types/non-zero-int.mts +51 -38
- package/src/number/branded-types/non-zero-int16.mts +52 -53
- package/src/number/branded-types/non-zero-int32.mts +52 -54
- package/src/number/branded-types/non-zero-safe-int.mts +53 -54
- package/src/number/branded-types/non-zero-uint16.mts +52 -53
- package/src/number/branded-types/non-zero-uint32.mts +52 -53
- package/src/number/branded-types/positive-finite-number.mts +74 -63
- package/src/number/branded-types/positive-int.mts +46 -68
- package/src/number/branded-types/positive-int16.mts +61 -57
- package/src/number/branded-types/positive-int32.mts +61 -57
- package/src/number/branded-types/positive-safe-int.mts +62 -57
- package/src/number/branded-types/positive-uint16.mts +61 -57
- package/src/number/branded-types/positive-uint32.mts +61 -58
- package/src/number/branded-types/safe-int.mts +43 -62
- package/src/number/branded-types/safe-uint.mts +62 -57
- package/src/number/branded-types/uint.mts +63 -50
- package/src/number/branded-types/uint16.mts +60 -58
- package/src/number/branded-types/uint32.mts +59 -55
- package/src/number/enum/int8.mts +56 -65
- package/src/number/enum/uint8.mts +60 -58
|
@@ -63,129 +63,139 @@ const round = (
|
|
|
63
63
|
>;
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
|
-
*
|
|
66
|
+
* Type guard that checks if a value is a non-positive finite number.
|
|
67
67
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
68
|
+
* Returns `true` only for a finite value — never `NaN`, `Infinity`, or
|
|
69
|
+
* `-Infinity`.
|
|
70
|
+
*
|
|
71
|
+
* @param value - The value to check
|
|
72
|
+
* @returns `true` if the value is a non-positive finite number, `false` otherwise
|
|
70
73
|
*/
|
|
71
74
|
export const isNonPositiveFiniteNumber = is;
|
|
72
75
|
|
|
73
76
|
/**
|
|
74
|
-
* Casts a number to
|
|
77
|
+
* Casts a `number` to the `NonPositiveFiniteNumber` branded type.
|
|
78
|
+
*
|
|
79
|
+
* Validates that the value is a non-positive finite number and returns it with
|
|
80
|
+
* the `NonPositiveFiniteNumber` brand. Throws a `TypeError` otherwise.
|
|
75
81
|
*
|
|
76
|
-
* @param value The value to cast
|
|
77
|
-
* @returns The value as a NonPositiveFiniteNumber
|
|
78
|
-
* @throws {TypeError} If the value is not a non-positive finite number
|
|
82
|
+
* @param value - The value to cast
|
|
83
|
+
* @returns The value as a `NonPositiveFiniteNumber`
|
|
84
|
+
* @throws {TypeError} If the value is not a non-positive finite number
|
|
79
85
|
*/
|
|
80
86
|
export const asNonPositiveFiniteNumber = castType;
|
|
81
87
|
|
|
82
88
|
/**
|
|
83
|
-
* Namespace providing type-safe
|
|
84
|
-
*
|
|
89
|
+
* Namespace providing type-safe operations for the `NonPositiveFiniteNumber`
|
|
90
|
+
* branded type.
|
|
85
91
|
*
|
|
86
|
-
*
|
|
87
|
-
* results to MAX_VALUE (0), while ensuring results remain finite (excluding NaN
|
|
88
|
-
* and Infinity). This type is useful for representing quantities that must
|
|
89
|
-
* always be non-positive, such as debts, penalties, and negative offsets.
|
|
92
|
+
* The `NonPositiveFiniteNumber` type represents a non-positive finite number.
|
|
90
93
|
*/
|
|
91
94
|
export const NonPositiveFiniteNumber = {
|
|
92
95
|
/**
|
|
93
|
-
* Type guard
|
|
96
|
+
* Type guard that checks if a value is a non-positive finite number.
|
|
94
97
|
*
|
|
95
|
-
* @param value The value to check
|
|
96
|
-
* @returns `true` if the value is a non-positive finite number, `false`
|
|
97
|
-
*
|
|
98
|
+
* @param value - The value to check
|
|
99
|
+
* @returns `true` if the value is a non-positive finite number, `false` otherwise
|
|
100
|
+
* @see {@link isNonPositiveFiniteNumber} for usage examples
|
|
98
101
|
*/
|
|
99
102
|
is,
|
|
100
103
|
|
|
101
104
|
/**
|
|
102
|
-
* The
|
|
103
|
-
*
|
|
104
|
-
* @readonly
|
|
105
|
+
* The largest value representable as `NonPositiveFiniteNumber`.
|
|
105
106
|
*/
|
|
106
107
|
MAX_VALUE,
|
|
107
108
|
|
|
108
109
|
/**
|
|
109
|
-
* Returns the
|
|
110
|
+
* Returns the smallest of the given non-positive finite numbers.
|
|
110
111
|
*
|
|
111
|
-
* @param
|
|
112
|
-
* @
|
|
113
|
-
* @returns The minimum value as a NonPositiveFiniteNumber.
|
|
112
|
+
* @param values - The non-positive finite numbers to compare (at least one required)
|
|
113
|
+
* @returns The smallest value as a `NonPositiveFiniteNumber`
|
|
114
114
|
*/
|
|
115
115
|
min: min_,
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
|
-
* Returns the
|
|
118
|
+
* Returns the largest of the given non-positive finite numbers.
|
|
119
119
|
*
|
|
120
|
-
* @param
|
|
121
|
-
* @
|
|
122
|
-
* @returns The maximum value as a NonPositiveFiniteNumber.
|
|
120
|
+
* @param values - The non-positive finite numbers to compare (at least one required)
|
|
121
|
+
* @returns The largest value as a `NonPositiveFiniteNumber`
|
|
123
122
|
*/
|
|
124
123
|
max: max_,
|
|
125
124
|
|
|
126
125
|
/**
|
|
127
|
-
* Clamps a number
|
|
126
|
+
* Clamps a `number` into the `NonPositiveFiniteNumber` range `[MIN_VALUE,
|
|
127
|
+
* MAX_VALUE]`.
|
|
128
128
|
*
|
|
129
|
-
* @param value The
|
|
130
|
-
* @returns The
|
|
129
|
+
* @param value - The value to clamp
|
|
130
|
+
* @returns The clamped value as a `NonPositiveFiniteNumber`
|
|
131
131
|
*/
|
|
132
132
|
clamp,
|
|
133
133
|
|
|
134
134
|
/**
|
|
135
|
-
*
|
|
135
|
+
* Returns the greatest integer less than or equal to a non-positive finite
|
|
136
|
+
* number.
|
|
136
137
|
*
|
|
137
|
-
* @param
|
|
138
|
-
* @returns The
|
|
138
|
+
* @param a - The non-positive finite number value
|
|
139
|
+
* @returns The floored value as an integer
|
|
139
140
|
*/
|
|
140
141
|
floor,
|
|
141
142
|
|
|
142
143
|
/**
|
|
143
|
-
*
|
|
144
|
+
* Returns the smallest integer greater than or equal to a non-positive finite
|
|
145
|
+
* number.
|
|
144
146
|
*
|
|
145
|
-
* @param
|
|
146
|
-
* @returns The
|
|
147
|
+
* @param a - The non-positive finite number value
|
|
148
|
+
* @returns The ceiled value as an integer
|
|
147
149
|
*/
|
|
148
150
|
ceil,
|
|
149
151
|
|
|
150
152
|
/**
|
|
151
|
-
*
|
|
153
|
+
* Returns the value of a non-positive finite number rounded to the nearest
|
|
154
|
+
* integer.
|
|
152
155
|
*
|
|
153
|
-
* @param
|
|
154
|
-
* @returns The rounded value as
|
|
156
|
+
* @param a - The non-positive finite number value
|
|
157
|
+
* @returns The rounded value as an integer
|
|
155
158
|
*/
|
|
156
159
|
round,
|
|
157
160
|
|
|
158
161
|
/**
|
|
159
|
-
* Generates a random NonPositiveFiniteNumber
|
|
162
|
+
* Generates a random `NonPositiveFiniteNumber` within the given range.
|
|
163
|
+
*
|
|
164
|
+
* The range is inclusive on both ends.
|
|
160
165
|
*
|
|
161
|
-
* @
|
|
166
|
+
* @param min - The minimum value (inclusive)
|
|
167
|
+
* @param max - The maximum value (inclusive)
|
|
168
|
+
* @returns A random `NonPositiveFiniteNumber` in `[min, max]`
|
|
162
169
|
*/
|
|
163
170
|
random,
|
|
164
171
|
|
|
165
172
|
/**
|
|
166
|
-
* Raises a
|
|
173
|
+
* Raises `a` to the power `b`, returning `a ** b` as a
|
|
174
|
+
* `NonPositiveFiniteNumber`.
|
|
167
175
|
*
|
|
168
|
-
* @param a The base
|
|
169
|
-
* @param b The exponent
|
|
170
|
-
* @returns `a ** b`
|
|
176
|
+
* @param a - The base non-positive finite number
|
|
177
|
+
* @param b - The exponent non-positive finite number
|
|
178
|
+
* @returns `a ** b` as a `NonPositiveFiniteNumber`
|
|
171
179
|
*/
|
|
172
180
|
pow,
|
|
173
181
|
|
|
174
182
|
/**
|
|
175
|
-
* Adds two
|
|
183
|
+
* Adds two non-positive finite numbers, returning `a + b` as a
|
|
184
|
+
* `NonPositiveFiniteNumber`.
|
|
176
185
|
*
|
|
177
|
-
* @param a The first
|
|
178
|
-
* @param b The second
|
|
179
|
-
* @returns `a
|
|
186
|
+
* @param a - The first non-positive finite number
|
|
187
|
+
* @param b - The second non-positive finite number
|
|
188
|
+
* @returns The sum of `a` and `b` as a `NonPositiveFiniteNumber`
|
|
180
189
|
*/
|
|
181
190
|
add,
|
|
182
191
|
|
|
183
192
|
/**
|
|
184
|
-
* Subtracts
|
|
193
|
+
* Subtracts two non-positive finite numbers, returning `a - b` as a
|
|
194
|
+
* `NonPositiveFiniteNumber`.
|
|
185
195
|
*
|
|
186
|
-
* @param a The
|
|
187
|
-
* @param b The
|
|
188
|
-
* @returns `a
|
|
196
|
+
* @param a - The first non-positive finite number
|
|
197
|
+
* @param b - The second non-positive finite number
|
|
198
|
+
* @returns The difference of `a` and `b` as a `NonPositiveFiniteNumber`
|
|
189
199
|
*/
|
|
190
200
|
sub,
|
|
191
201
|
} as const;
|
|
@@ -32,19 +32,9 @@ const {
|
|
|
32
32
|
/**
|
|
33
33
|
* Type guard that checks if a value is a non-positive integer.
|
|
34
34
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
*
|
|
41
|
-
* ```ts
|
|
42
|
-
* assert.isTrue(isNonPositiveInt(0));
|
|
43
|
-
*
|
|
44
|
-
* assert.isTrue(isNonPositiveInt(-5));
|
|
45
|
-
*
|
|
46
|
-
* assert.isFalse(isNonPositiveInt(1));
|
|
47
|
-
* ```
|
|
35
|
+
* Returns `true` for a non-positive integer — a value with no fractional
|
|
36
|
+
* component, including values outside the safe integer range (unlike
|
|
37
|
+
* `SafeInt`).
|
|
48
38
|
*
|
|
49
39
|
* @param value - The value to check
|
|
50
40
|
* @returns `true` if the value is a non-positive integer, `false` otherwise
|
|
@@ -52,200 +42,105 @@ const {
|
|
|
52
42
|
export const isNonPositiveInt = is;
|
|
53
43
|
|
|
54
44
|
/**
|
|
55
|
-
* Casts a number to
|
|
56
|
-
*
|
|
57
|
-
* This function validates that the input is a non-positive integer (<= 0) and
|
|
58
|
-
* returns it with the NonPositiveInt brand. This ensures type safety for
|
|
59
|
-
* operations that require non-positive integer values.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
45
|
+
* Casts a `number` to the `NonPositiveInt` branded type.
|
|
62
46
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* assert.isTrue(branded === -7);
|
|
67
|
-
*
|
|
68
|
-
* assert.isTrue(NonPositiveInt.is(branded));
|
|
69
|
-
* ```
|
|
47
|
+
* Validates that the value is a non-positive integer and returns it with the
|
|
48
|
+
* `NonPositiveInt` brand. Throws a `TypeError` otherwise.
|
|
70
49
|
*
|
|
71
50
|
* @param value - The value to cast
|
|
72
|
-
* @returns The value as a NonPositiveInt
|
|
51
|
+
* @returns The value as a `NonPositiveInt`
|
|
73
52
|
* @throws {TypeError} If the value is not a non-positive integer
|
|
74
53
|
*/
|
|
75
54
|
export const asNonPositiveInt = castType;
|
|
76
55
|
|
|
77
56
|
/**
|
|
78
|
-
* Namespace providing type-safe operations for NonPositiveInt branded
|
|
79
|
-
*
|
|
80
|
-
* NonPositiveInt represents integers that are less than or equal to zero (<= 0).
|
|
81
|
-
* All operations automatically clamp results to maintain the non-positive
|
|
82
|
-
* constraint, ensuring that arithmetic operations never produce positive values.
|
|
57
|
+
* Namespace providing type-safe operations for the `NonPositiveInt` branded
|
|
58
|
+
* type.
|
|
83
59
|
*
|
|
84
|
-
*
|
|
60
|
+
* The `NonPositiveInt` type represents a non-positive integer. Division (`div`)
|
|
61
|
+
* uses floor division.
|
|
85
62
|
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* - Negative counts or differences
|
|
90
|
-
* - Depth or altitude below a reference point
|
|
63
|
+
* Unlike `SafeInt`, `NonPositiveInt` allows values outside the safe integer
|
|
64
|
+
* range (±2^53 − 1), so very large magnitudes may lose precision in
|
|
65
|
+
* JavaScript's `number` type.
|
|
91
66
|
*/
|
|
92
67
|
export const NonPositiveInt = {
|
|
93
68
|
/**
|
|
94
69
|
* Type guard that checks if a value is a non-positive integer.
|
|
95
70
|
*
|
|
96
|
-
* @example
|
|
97
|
-
*
|
|
98
|
-
* ```ts
|
|
99
|
-
* const value: number = -42;
|
|
100
|
-
*
|
|
101
|
-
* if (NonPositiveInt.is(value)) {
|
|
102
|
-
* // value is now typed as NonPositiveInt
|
|
103
|
-
* const debt = value;
|
|
104
|
-
* }
|
|
105
|
-
* ```
|
|
106
|
-
*
|
|
107
71
|
* @param value - The value to check
|
|
108
72
|
* @returns `true` if the value is a non-positive integer, `false` otherwise
|
|
73
|
+
* @see {@link isNonPositiveInt} for usage examples
|
|
109
74
|
*/
|
|
110
75
|
is,
|
|
111
76
|
|
|
112
77
|
/**
|
|
113
|
-
* The
|
|
114
|
-
*
|
|
115
|
-
* @readonly
|
|
78
|
+
* The largest value representable as `NonPositiveInt`.
|
|
116
79
|
*/
|
|
117
80
|
MAX_VALUE,
|
|
118
81
|
|
|
119
82
|
/**
|
|
120
|
-
* Returns the
|
|
121
|
-
*
|
|
122
|
-
* Since non-positive integers range from negative infinity to 0, the smaller
|
|
123
|
-
* value is the one further from zero (more negative).
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
83
|
+
* Returns the smallest of the given non-positive integers.
|
|
126
84
|
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* assert.strictEqual(result, -10);
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* @param a - The first non-positive integer
|
|
134
|
-
* @param b - The second non-positive integer
|
|
135
|
-
* @returns The minimum value as a non-positive integer
|
|
85
|
+
* @param values - The non-positive integers to compare (at least one required)
|
|
86
|
+
* @returns The smallest value as a `NonPositiveInt`
|
|
136
87
|
*/
|
|
137
88
|
min: min_,
|
|
138
89
|
|
|
139
90
|
/**
|
|
140
|
-
* Returns the
|
|
141
|
-
*
|
|
142
|
-
* Since non-positive integers range from negative infinity to 0, the larger
|
|
143
|
-
* value is the one closer to zero (less negative).
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
*
|
|
147
|
-
* ```ts
|
|
148
|
-
* const result = NonPositiveInt.max(-5, -10);
|
|
149
|
-
*
|
|
150
|
-
* assert.strictEqual(result, -5);
|
|
151
|
-
* ```
|
|
91
|
+
* Returns the largest of the given non-positive integers.
|
|
152
92
|
*
|
|
153
|
-
* @param
|
|
154
|
-
* @
|
|
155
|
-
* @returns The maximum value as a non-positive integer
|
|
93
|
+
* @param values - The non-positive integers to compare (at least one required)
|
|
94
|
+
* @returns The largest value as a `NonPositiveInt`
|
|
156
95
|
*/
|
|
157
96
|
max: max_,
|
|
158
97
|
|
|
159
98
|
/**
|
|
160
|
-
* Clamps a number
|
|
161
|
-
*
|
|
162
|
-
* Any positive value is clamped to 0 (the maximum non-positive integer).
|
|
163
|
-
* Negative values are preserved as long as they are valid integers.
|
|
164
|
-
*
|
|
165
|
-
* @example
|
|
166
|
-
*
|
|
167
|
-
* ```ts
|
|
168
|
-
* assert.strictEqual(NonPositiveInt.clamp(5), 0);
|
|
169
|
-
*
|
|
170
|
-
* assert.strictEqual(NonPositiveInt.clamp(-5), -5);
|
|
99
|
+
* Clamps a `number` into the `NonPositiveInt` range, rounding to the nearest
|
|
100
|
+
* integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
|
|
171
101
|
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* @param value - The number to clamp
|
|
176
|
-
* @returns The value clamped to the non-positive integer range
|
|
102
|
+
* @param value - The value to clamp
|
|
103
|
+
* @returns The clamped value as a `NonPositiveInt`
|
|
177
104
|
*/
|
|
178
105
|
clamp,
|
|
179
106
|
|
|
180
107
|
/**
|
|
181
|
-
* Generates a random
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
*
|
|
185
|
-
* ```ts
|
|
186
|
-
* const randomValue = NonPositiveInt.random();
|
|
108
|
+
* Generates a random `NonPositiveInt` within the given range.
|
|
187
109
|
*
|
|
188
|
-
*
|
|
189
|
-
* ```
|
|
110
|
+
* The range is inclusive on both ends.
|
|
190
111
|
*
|
|
191
|
-
* @
|
|
112
|
+
* @param min - The minimum value (inclusive)
|
|
113
|
+
* @param max - The maximum value (inclusive)
|
|
114
|
+
* @returns A random `NonPositiveInt` in `[min, max]`
|
|
192
115
|
*/
|
|
193
116
|
random,
|
|
194
117
|
|
|
195
118
|
/**
|
|
196
|
-
* Raises a
|
|
197
|
-
*
|
|
198
|
-
* The result is clamped to the non-positive integer range.
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
*
|
|
202
|
-
* ```ts
|
|
203
|
-
* const result = NonPositiveInt.pow(-2, -1);
|
|
204
|
-
* ```
|
|
119
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `NonPositiveInt`
|
|
120
|
+
* (floored to an integer).
|
|
205
121
|
*
|
|
206
122
|
* @param a - The base non-positive integer
|
|
207
123
|
* @param b - The exponent non-positive integer
|
|
208
|
-
* @returns `a ** b`
|
|
124
|
+
* @returns `a ** b` as a `NonPositiveInt`
|
|
209
125
|
*/
|
|
210
126
|
pow,
|
|
211
127
|
|
|
212
128
|
/**
|
|
213
|
-
* Adds two non-positive integers
|
|
214
|
-
*
|
|
215
|
-
* The result is clamped to the non-positive integer range. If the sum is
|
|
216
|
-
* positive, it is clamped to 0.
|
|
217
|
-
*
|
|
218
|
-
* @example
|
|
219
|
-
*
|
|
220
|
-
* ```ts
|
|
221
|
-
* assert.strictEqual(NonPositiveInt.add(-5, -3), -8);
|
|
222
|
-
*
|
|
223
|
-
* assert.strictEqual(NonPositiveInt.add(-5, 10), 0);
|
|
224
|
-
* ```
|
|
129
|
+
* Adds two non-positive integers, returning `a + b` as a `NonPositiveInt`.
|
|
225
130
|
*
|
|
226
131
|
* @param a - The first non-positive integer
|
|
227
132
|
* @param b - The second non-positive integer
|
|
228
|
-
* @returns `a
|
|
133
|
+
* @returns The sum of `a` and `b` as a `NonPositiveInt`
|
|
229
134
|
*/
|
|
230
135
|
add,
|
|
231
136
|
|
|
232
137
|
/**
|
|
233
|
-
* Subtracts
|
|
234
|
-
*
|
|
235
|
-
* The result is clamped to the non-positive integer range. If the result is
|
|
236
|
-
* positive, it is clamped to 0.
|
|
138
|
+
* Subtracts two non-positive integers, returning `a - b` as a
|
|
139
|
+
* `NonPositiveInt`.
|
|
237
140
|
*
|
|
238
|
-
* @
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
* assert.strictEqual(NonPositiveInt.sub(-10, -3), -7);
|
|
242
|
-
*
|
|
243
|
-
* assert.strictEqual(NonPositiveInt.sub(-10, 5), 0);
|
|
244
|
-
* ```
|
|
245
|
-
*
|
|
246
|
-
* @param a - The minuend non-positive integer
|
|
247
|
-
* @param b - The subtrahend non-positive integer
|
|
248
|
-
* @returns `a - b` clamped to the non-positive integer range
|
|
141
|
+
* @param a - The first non-positive integer
|
|
142
|
+
* @param b - The second non-positive integer
|
|
143
|
+
* @returns The difference of `a` and `b` as a `NonPositiveInt`
|
|
249
144
|
*/
|
|
250
145
|
sub,
|
|
251
146
|
} as const;
|
|
@@ -31,117 +31,118 @@ const {
|
|
|
31
31
|
} as const);
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
* range [Number.MIN_SAFE_INTEGER, 0]).
|
|
34
|
+
* Type guard that checks if a value is a non-positive safe integer.
|
|
36
35
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
36
|
+
* Returns `true` for a non-positive safe integer — a value with no fractional
|
|
37
|
+
* component.
|
|
38
|
+
*
|
|
39
|
+
* @param value - The value to check
|
|
40
|
+
* @returns `true` if the value is a non-positive safe integer, `false` otherwise
|
|
39
41
|
*/
|
|
40
42
|
export const isNonPositiveSafeInt = is;
|
|
41
43
|
|
|
42
44
|
/**
|
|
43
|
-
* Casts a number to
|
|
45
|
+
* Casts a `number` to the `NonPositiveSafeInt` branded type.
|
|
46
|
+
*
|
|
47
|
+
* Validates that the value is a non-positive safe integer and returns it with
|
|
48
|
+
* the `NonPositiveSafeInt` brand. Throws a `TypeError` otherwise.
|
|
44
49
|
*
|
|
45
|
-
* @param value The value to cast
|
|
46
|
-
* @returns The value as a NonPositiveSafeInt
|
|
47
|
-
* @throws {TypeError} If the value is not a non-positive safe integer
|
|
50
|
+
* @param value - The value to cast
|
|
51
|
+
* @returns The value as a `NonPositiveSafeInt`
|
|
52
|
+
* @throws {TypeError} If the value is not a non-positive safe integer
|
|
48
53
|
*/
|
|
49
54
|
export const asNonPositiveSafeInt = castType;
|
|
50
55
|
|
|
51
56
|
/**
|
|
52
|
-
* Namespace providing type-safe
|
|
53
|
-
*
|
|
57
|
+
* Namespace providing type-safe operations for the `NonPositiveSafeInt` branded
|
|
58
|
+
* type.
|
|
54
59
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* maintains the non-positive safe integer constraint, with positive results
|
|
58
|
-
* clamped to 0 and underflow results clamped to MIN_VALUE.
|
|
60
|
+
* The `NonPositiveSafeInt` type represents a non-positive safe integer.
|
|
61
|
+
* Division (`div`) uses floor division.
|
|
59
62
|
*/
|
|
60
63
|
export const NonPositiveSafeInt = {
|
|
61
64
|
/**
|
|
62
|
-
* Type guard
|
|
65
|
+
* Type guard that checks if a value is a non-positive safe integer.
|
|
63
66
|
*
|
|
64
|
-
* @param value The value to check
|
|
65
|
-
* @returns `true` if the value is a non-positive safe integer, `false`
|
|
66
|
-
*
|
|
67
|
+
* @param value - The value to check
|
|
68
|
+
* @returns `true` if the value is a non-positive safe integer, `false` otherwise
|
|
69
|
+
* @see {@link isNonPositiveSafeInt} for usage examples
|
|
67
70
|
*/
|
|
68
71
|
is,
|
|
69
72
|
|
|
70
73
|
/**
|
|
71
|
-
* The
|
|
72
|
-
*
|
|
73
|
-
* @readonly
|
|
74
|
+
* The smallest value representable as `NonPositiveSafeInt`.
|
|
74
75
|
*/
|
|
75
76
|
MIN_VALUE,
|
|
76
77
|
|
|
77
78
|
/**
|
|
78
|
-
* The
|
|
79
|
-
*
|
|
80
|
-
* @readonly
|
|
79
|
+
* The largest value representable as `NonPositiveSafeInt`.
|
|
81
80
|
*/
|
|
82
81
|
MAX_VALUE,
|
|
83
82
|
|
|
84
83
|
/**
|
|
85
|
-
* Returns the
|
|
84
|
+
* Returns the smallest of the given non-positive safe integers.
|
|
86
85
|
*
|
|
87
|
-
* @param
|
|
88
|
-
* @
|
|
89
|
-
* @returns The minimum value as a NonPositiveSafeInt.
|
|
86
|
+
* @param values - The non-positive safe integers to compare (at least one required)
|
|
87
|
+
* @returns The smallest value as a `NonPositiveSafeInt`
|
|
90
88
|
*/
|
|
91
89
|
min: min_,
|
|
92
90
|
|
|
93
91
|
/**
|
|
94
|
-
* Returns the
|
|
92
|
+
* Returns the largest of the given non-positive safe integers.
|
|
95
93
|
*
|
|
96
|
-
* @param
|
|
97
|
-
* @
|
|
98
|
-
* @returns The maximum value as a NonPositiveSafeInt.
|
|
94
|
+
* @param values - The non-positive safe integers to compare (at least one required)
|
|
95
|
+
* @returns The largest value as a `NonPositiveSafeInt`
|
|
99
96
|
*/
|
|
100
97
|
max: max_,
|
|
101
98
|
|
|
102
99
|
/**
|
|
103
|
-
* Clamps a number
|
|
100
|
+
* Clamps a `number` into the `NonPositiveSafeInt` range, rounding to the
|
|
101
|
+
* nearest integer and constraining the result to `[MIN_VALUE, MAX_VALUE]`.
|
|
104
102
|
*
|
|
105
|
-
* @param value The
|
|
106
|
-
* @returns The
|
|
107
|
-
* NonPositiveSafeInt.
|
|
103
|
+
* @param value - The value to clamp
|
|
104
|
+
* @returns The clamped value as a `NonPositiveSafeInt`
|
|
108
105
|
*/
|
|
109
106
|
clamp,
|
|
110
107
|
|
|
111
108
|
/**
|
|
112
|
-
* Generates a random NonPositiveSafeInt
|
|
109
|
+
* Generates a random `NonPositiveSafeInt` within the given range.
|
|
110
|
+
*
|
|
111
|
+
* The range is inclusive on both ends.
|
|
113
112
|
*
|
|
114
|
-
* @
|
|
113
|
+
* @param min - The minimum value (inclusive)
|
|
114
|
+
* @param max - The maximum value (inclusive)
|
|
115
|
+
* @returns A random `NonPositiveSafeInt` in `[min, max]`
|
|
115
116
|
*/
|
|
116
117
|
random,
|
|
117
118
|
|
|
118
119
|
/**
|
|
119
|
-
* Raises a
|
|
120
|
+
* Raises `a` to the power `b`, returning `a ** b` as a `NonPositiveSafeInt`
|
|
121
|
+
* (floored to an integer).
|
|
120
122
|
*
|
|
121
|
-
* @param a The base
|
|
122
|
-
* @param b The exponent
|
|
123
|
-
* @returns `a ** b`
|
|
124
|
-
* NonPositiveSafeInt.
|
|
123
|
+
* @param a - The base non-positive safe integer
|
|
124
|
+
* @param b - The exponent non-positive safe integer
|
|
125
|
+
* @returns `a ** b` as a `NonPositiveSafeInt`
|
|
125
126
|
*/
|
|
126
127
|
pow,
|
|
127
128
|
|
|
128
129
|
/**
|
|
129
|
-
* Adds two
|
|
130
|
+
* Adds two non-positive safe integers, returning `a + b` as a
|
|
131
|
+
* `NonPositiveSafeInt`.
|
|
130
132
|
*
|
|
131
|
-
* @param a The first
|
|
132
|
-
* @param b The second
|
|
133
|
-
* @returns `a
|
|
134
|
-
* NonPositiveSafeInt.
|
|
133
|
+
* @param a - The first non-positive safe integer
|
|
134
|
+
* @param b - The second non-positive safe integer
|
|
135
|
+
* @returns The sum of `a` and `b` as a `NonPositiveSafeInt`
|
|
135
136
|
*/
|
|
136
137
|
add,
|
|
137
138
|
|
|
138
139
|
/**
|
|
139
|
-
* Subtracts
|
|
140
|
+
* Subtracts two non-positive safe integers, returning `a - b` as a
|
|
141
|
+
* `NonPositiveSafeInt`.
|
|
140
142
|
*
|
|
141
|
-
* @param a The
|
|
142
|
-
* @param b The
|
|
143
|
-
* @returns `a
|
|
144
|
-
* NonPositiveSafeInt (maximum 0).
|
|
143
|
+
* @param a - The first non-positive safe integer
|
|
144
|
+
* @param b - The second non-positive safe integer
|
|
145
|
+
* @returns The difference of `a` and `b` as a `NonPositiveSafeInt`
|
|
145
146
|
*/
|
|
146
147
|
sub,
|
|
147
148
|
} as const;
|