ts-data-forge 14.6.2 → 14.6.4

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 (182) hide show
  1. package/dist/array/impl/array-utils-creation.mjs +103 -103
  2. package/dist/array/impl/array-utils-element-access.mjs +46 -46
  3. package/dist/array/impl/array-utils-iterators.mjs +54 -54
  4. package/dist/array/impl/array-utils-length-bounded-array-cast.mjs +49 -49
  5. package/dist/array/impl/array-utils-length-bounded-array-guard.mjs +40 -40
  6. package/dist/array/impl/array-utils-length-bounded-tuple-cast.mjs +42 -42
  7. package/dist/array/impl/array-utils-length-bounded-tuple-guard.mjs +46 -46
  8. package/dist/array/impl/array-utils-modification.mjs +5 -5
  9. package/dist/array/impl/array-utils-reducing-value.mjs +11 -11
  10. package/dist/array/impl/array-utils-search.mjs +6 -6
  11. package/dist/array/impl/array-utils-set-op.mjs +116 -116
  12. package/dist/array/impl/array-utils-size.mjs +6 -6
  13. package/dist/array/impl/array-utils-slice-clamped.mjs +1 -1
  14. package/dist/array/impl/array-utils-slicing.mjs +56 -56
  15. package/dist/array/impl/array-utils-transformation.mjs +158 -158
  16. package/dist/array/impl/array-utils-validation.mjs +36 -36
  17. package/dist/collections/imap-mapped.mjs +195 -195
  18. package/dist/collections/imap.mjs +143 -143
  19. package/dist/collections/iset-mapped.mjs +320 -320
  20. package/dist/collections/iset.mjs +207 -207
  21. package/dist/collections/queue.mjs +130 -130
  22. package/dist/collections/stack.mjs +125 -125
  23. package/dist/expect-type.mjs +68 -68
  24. package/dist/functional/async-result/impl/async-result-flat-map.mjs +3 -3
  25. package/dist/functional/async-result/impl/async-result-map-err.mjs +2 -2
  26. package/dist/functional/async-result/impl/async-result-map.mjs +2 -2
  27. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs +31 -31
  28. package/dist/functional/optional/impl/optional-expect-to-be.mjs +1 -1
  29. package/dist/functional/optional/impl/optional-filter.mjs +1 -1
  30. package/dist/functional/optional/impl/optional-flat-map.mjs +1 -1
  31. package/dist/functional/optional/impl/optional-from-nullable.mjs +24 -24
  32. package/dist/functional/optional/impl/optional-is-none.mjs +19 -19
  33. package/dist/functional/optional/impl/optional-is-optional.mjs +18 -18
  34. package/dist/functional/optional/impl/optional-is-some.mjs +20 -20
  35. package/dist/functional/optional/impl/optional-map.mjs +2 -2
  36. package/dist/functional/optional/impl/optional-match.mjs +2 -2
  37. package/dist/functional/optional/impl/optional-none.mjs +16 -16
  38. package/dist/functional/optional/impl/optional-or-else.mjs +1 -1
  39. package/dist/functional/optional/impl/optional-some.mjs +19 -19
  40. package/dist/functional/optional/impl/optional-to-nullable.mjs +26 -26
  41. package/dist/functional/optional/impl/optional-unwrap-or.mjs +1 -1
  42. package/dist/functional/optional/impl/optional-unwrap-throw.mjs +28 -28
  43. package/dist/functional/optional/impl/optional-unwrap.mjs +1 -1
  44. package/dist/functional/optional/impl/optional-zip.mjs +32 -32
  45. package/dist/functional/optional/impl/tag.mjs +1 -1
  46. package/dist/functional/pipe.mjs +19 -19
  47. package/dist/functional/result/impl/result-err.mjs +25 -25
  48. package/dist/functional/result/impl/result-expect-to-be.mjs +2 -2
  49. package/dist/functional/result/impl/result-flat-map.mjs +4 -4
  50. package/dist/functional/result/impl/result-fold.mjs +4 -4
  51. package/dist/functional/result/impl/result-from-optional.mjs +2 -2
  52. package/dist/functional/result/impl/result-from-promise.mjs +34 -34
  53. package/dist/functional/result/impl/result-from-throwable.mjs +29 -29
  54. package/dist/functional/result/impl/result-is-err.mjs +19 -19
  55. package/dist/functional/result/impl/result-is-ok.mjs +19 -19
  56. package/dist/functional/result/impl/result-is-result.mjs +19 -19
  57. package/dist/functional/result/impl/result-map-err.mjs +4 -4
  58. package/dist/functional/result/impl/result-map.mjs +4 -4
  59. package/dist/functional/result/impl/result-match.mjs +3 -3
  60. package/dist/functional/result/impl/result-ok.mjs +25 -25
  61. package/dist/functional/result/impl/result-or-else.mjs +2 -2
  62. package/dist/functional/result/impl/result-safe-try.mjs +15 -15
  63. package/dist/functional/result/impl/result-safe-unwrap.mjs +33 -33
  64. package/dist/functional/result/impl/result-swap.mjs +20 -20
  65. package/dist/functional/result/impl/result-to-optional.mjs +27 -27
  66. package/dist/functional/result/impl/result-unwrap-err-or.mjs +2 -2
  67. package/dist/functional/result/impl/result-unwrap-err-throw.mjs +31 -31
  68. package/dist/functional/result/impl/result-unwrap-err.mjs +30 -30
  69. package/dist/functional/result/impl/result-unwrap-ok-or.mjs +2 -2
  70. package/dist/functional/result/impl/result-unwrap-ok.mjs +1 -1
  71. package/dist/functional/result/impl/result-unwrap-throw.mjs +31 -31
  72. package/dist/functional/result/impl/result-zip.mjs +29 -29
  73. package/dist/functional/result/impl/tag.mjs +1 -1
  74. package/dist/functional/ternary-result/impl/tag.mjs +2 -2
  75. package/dist/functional/ternary-result/impl/ternary-result-err.mjs +13 -13
  76. package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs +4 -4
  77. package/dist/functional/ternary-result/impl/ternary-result-fold.mjs +4 -4
  78. package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +18 -18
  79. package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +17 -17
  80. package/dist/functional/ternary-result/impl/ternary-result-is-err.mjs +17 -17
  81. package/dist/functional/ternary-result/impl/ternary-result-is-ok.mjs +19 -19
  82. package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +19 -19
  83. package/dist/functional/ternary-result/impl/ternary-result-is-warn.mjs +18 -18
  84. package/dist/functional/ternary-result/impl/ternary-result-map-err.mjs +2 -2
  85. package/dist/functional/ternary-result/impl/ternary-result-map-warn.mjs +3 -3
  86. package/dist/functional/ternary-result/impl/ternary-result-map.mjs +4 -4
  87. package/dist/functional/ternary-result/impl/ternary-result-ok.mjs +14 -14
  88. package/dist/functional/ternary-result/impl/ternary-result-to-optional.mjs +19 -19
  89. package/dist/functional/ternary-result/impl/ternary-result-unwrap-err-throw.mjs +19 -19
  90. package/dist/functional/ternary-result/impl/ternary-result-unwrap-err.mjs +17 -17
  91. package/dist/functional/ternary-result/impl/ternary-result-unwrap-ok.mjs +1 -1
  92. package/dist/functional/ternary-result/impl/ternary-result-unwrap-throw.mjs +20 -20
  93. package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn-throw.mjs +18 -18
  94. package/dist/functional/ternary-result/impl/ternary-result-unwrap-warn.mjs +17 -17
  95. package/dist/functional/ternary-result/impl/ternary-result-warn.mjs +16 -16
  96. package/dist/functional/ternary-result/impl/ternary-result-zip.mjs +31 -31
  97. package/dist/functional/ternary-result/impl/variant-name.mjs +1 -1
  98. package/dist/guard/has-key.mjs +41 -41
  99. package/dist/guard/is-non-empty-string.mjs +37 -37
  100. package/dist/guard/is-non-null-object.mjs +34 -34
  101. package/dist/guard/is-primitive.mjs +31 -31
  102. package/dist/guard/is-record.mjs +86 -86
  103. package/dist/guard/is-type.mjs +387 -387
  104. package/dist/guard/key-is-in.mjs +40 -40
  105. package/dist/iterator/range.mjs +1 -1
  106. package/dist/json/json.mjs +289 -289
  107. package/dist/number/branded-types/finite-number.mjs +116 -116
  108. package/dist/number/branded-types/int.mjs +223 -223
  109. package/dist/number/branded-types/int16.mjs +116 -116
  110. package/dist/number/branded-types/int32.mjs +116 -116
  111. package/dist/number/branded-types/negative-finite-number.mjs +126 -126
  112. package/dist/number/branded-types/negative-int.mjs +104 -104
  113. package/dist/number/branded-types/negative-safe-int.mjs +104 -104
  114. package/dist/number/branded-types/non-negative-finite-number.mjs +132 -132
  115. package/dist/number/branded-types/non-negative-int16.mjs +113 -113
  116. package/dist/number/branded-types/non-negative-int32.mjs +113 -113
  117. package/dist/number/branded-types/non-positive-finite-number.mjs +116 -116
  118. package/dist/number/branded-types/non-positive-int.mjs +97 -97
  119. package/dist/number/branded-types/non-positive-safe-int.mjs +98 -98
  120. package/dist/number/branded-types/non-zero-finite-number.mjs +112 -112
  121. package/dist/number/branded-types/non-zero-int.mjs +82 -82
  122. package/dist/number/branded-types/non-zero-int16.mjs +95 -95
  123. package/dist/number/branded-types/non-zero-int32.mjs +95 -95
  124. package/dist/number/branded-types/non-zero-safe-int.mjs +99 -99
  125. package/dist/number/branded-types/non-zero-uint16.mjs +95 -95
  126. package/dist/number/branded-types/non-zero-uint32.mjs +95 -95
  127. package/dist/number/branded-types/positive-finite-number.mjs +134 -134
  128. package/dist/number/branded-types/positive-int.mjs +237 -237
  129. package/dist/number/branded-types/positive-int16.mjs +110 -110
  130. package/dist/number/branded-types/positive-int32.mjs +110 -110
  131. package/dist/number/branded-types/positive-safe-int.mjs +268 -268
  132. package/dist/number/branded-types/positive-uint16.mjs +110 -110
  133. package/dist/number/branded-types/positive-uint32.mjs +110 -110
  134. package/dist/number/branded-types/safe-int.mjs +259 -259
  135. package/dist/number/branded-types/safe-uint.mjs +111 -111
  136. package/dist/number/branded-types/uint.mjs +225 -225
  137. package/dist/number/branded-types/uint16.mjs +108 -108
  138. package/dist/number/branded-types/uint32.mjs +108 -108
  139. package/dist/number/enum/int8.mjs +118 -118
  140. package/dist/number/enum/uint8.mjs +109 -109
  141. package/dist/number/num-arithmetic-types.mjs +1 -1
  142. package/dist/number/num.mjs +478 -478
  143. package/dist/number/refined-number-utils.mjs +170 -170
  144. package/dist/object/object.mjs +234 -234
  145. package/dist/others/cast-mutable.mjs +33 -33
  146. package/dist/others/cast-readonly.mjs +32 -32
  147. package/dist/others/debounce-function.mjs +13 -13
  148. package/dist/others/fast-deep-equal.mjs +14 -14
  149. package/dist/others/if-then.mjs +29 -29
  150. package/dist/others/tuple.mjs +50 -50
  151. package/dist/others/unknown-to-string.mjs +38 -38
  152. package/dist/promise/promise.mjs +47 -47
  153. package/dist/string/str.mjs +177 -177
  154. package/dist/types.mjs +1 -1
  155. package/package.json +6 -6
  156. package/src/guard/is-type.test.mts +2 -2
  157. package/src/number/branded-types/finite-number.test.mts +1 -1
  158. package/src/number/branded-types/int.test.mts +1 -1
  159. package/src/number/branded-types/int16.test.mts +1 -1
  160. package/src/number/branded-types/int32.test.mts +1 -1
  161. package/src/number/branded-types/non-negative-finite-number.test.mts +1 -1
  162. package/src/number/branded-types/non-negative-int16.test.mts +1 -1
  163. package/src/number/branded-types/non-negative-int32.test.mts +1 -1
  164. package/src/number/branded-types/non-zero-finite-number.test.mts +1 -1
  165. package/src/number/branded-types/non-zero-int.test.mts +1 -1
  166. package/src/number/branded-types/non-zero-int16.test.mts +1 -1
  167. package/src/number/branded-types/non-zero-int32.test.mts +1 -1
  168. package/src/number/branded-types/non-zero-safe-int.test.mts +1 -1
  169. package/src/number/branded-types/non-zero-uint16.test.mts +1 -1
  170. package/src/number/branded-types/non-zero-uint32.test.mts +1 -1
  171. package/src/number/branded-types/positive-finite-number.test.mts +1 -1
  172. package/src/number/branded-types/positive-int.test.mts +1 -1
  173. package/src/number/branded-types/positive-int16.test.mts +1 -1
  174. package/src/number/branded-types/positive-int32.test.mts +1 -1
  175. package/src/number/branded-types/positive-safe-int.test.mts +1 -1
  176. package/src/number/branded-types/positive-uint16.test.mts +1 -1
  177. package/src/number/branded-types/positive-uint32.test.mts +1 -1
  178. package/src/number/branded-types/safe-int.test.mts +1 -1
  179. package/src/number/branded-types/safe-uint.test.mts +1 -1
  180. package/src/number/branded-types/uint.test.mts +1 -1
  181. package/src/number/branded-types/uint16.test.mts +1 -1
  182. package/src/number/branded-types/uint32.test.mts +1 -1
@@ -1,368 +1,368 @@
1
1
 
2
2
  import { Result } from '../functional/index.mjs';
3
- /**
4
- * Namespace providing utility functions for number manipulation and validation.
5
- *
6
- * This namespace offers a comprehensive set of type-safe number utilities
7
- * including:
8
- *
9
- * - Type conversion and validation
10
- * - Type guards for numeric constraints (non-zero, non-negative, positive)
11
- * - Range checking and clamping operations
12
- * - Mathematical operations with type safety
13
- * - Rounding utilities
14
- *
15
- * Many functions in this namespace leverage TypeScript's type system to provide
16
- * compile-time guarantees about numeric constraints.
17
- */
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
18
  export var Num;
19
19
  (function (Num) {
20
- /**
21
- * Converts an unknown value to a number. Alias for the `Number` constructor.
22
- *
23
- * @example
24
- *
25
- * ```ts
26
- * const input = '123.45';
27
- *
28
- * const result = Num.from(input);
29
- *
30
- * assert.isTrue(result === 123.45);
31
- * ```
32
- *
33
- * @param n The value to convert.
34
- * @returns The numeric representation of `n`.
35
- */
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
36
  Num.from = Number;
37
- /**
38
- * Safely parses a base-10 integer from a string, returning a {@link Result}
39
- * that is `Ok<Int>` for valid input and `Err<Error>` otherwise.
40
- *
41
- * This is a stricter alternative to both `parseInt` and `Number`:
42
- *
43
- * - Unlike `parseInt('12abc', 10)` (which returns `12`), trailing
44
- * non-numeric characters make the whole input invalid and yield `Err`.
45
- * - Unlike `Number('')` / `Number(' ')` (which return `0`), empty or
46
- * whitespace-only input yields `Err`.
47
- *
48
- * The empty-string case is rejected by delegating to `parseInt` (which
49
- * returns `NaN` there) rather than hard-coding a check, while the trailing-
50
- * garbage case is rejected via `Number`. Valid input is truncated toward
51
- * zero, so `'12.9'` becomes `12` and `'-3.5'` becomes `-3`.
52
- *
53
- * Only base 10 is supported. Use `Result.unwrapOk` (optionally with a
54
- * `?? Number.NaN` fallback) or `Result.unwrapOkOr` to get a plain number
55
- * back.
56
- *
57
- * @example
58
- *
59
- * ```ts
60
- * assert.strictEqual(
61
- * Result.unwrapOkOr(Num.safeParseInt('123'), Number.NaN),
62
- * 123,
63
- * );
64
- *
65
- * assert.strictEqual(
66
- * Result.unwrapOkOr(Num.safeParseInt('12.9'), Number.NaN),
67
- * 12,
68
- * );
69
- *
70
- * assert.strictEqual(
71
- * Result.unwrapOkOr(Num.safeParseInt('-12.9'), Number.NaN),
72
- * -12,
73
- * );
74
- *
75
- * assert.strictEqual(Number.parseInt('-12.9', 10), -12);
76
- *
77
- * // Native `parseInt` ignores trailing non-numeric characters
78
- *
79
- * assert.strictEqual(Number.parseInt('123abc', 10), 123);
80
- *
81
- * assert.isTrue(Number.isNaN(Number('123abc')));
82
- *
83
- * assert.isTrue(Result.isErr(Num.safeParseInt('123abc')));
84
- *
85
- * // Whitespace is not a valid integer, so we return an error instead of coercing to 0.
86
- *
87
- * assert.isTrue(Number.isNaN(Number.parseInt(' ', 10)));
88
- *
89
- * assert.strictEqual(Number(' '), 0); // Native `Number` coerces whitespace to 0
90
- *
91
- * assert.isTrue(Result.isErr(Num.safeParseInt('')));
92
- *
93
- * assert.strictEqual(Result.unwrapOk(Num.safeParseInt(' ')), undefined);
94
- * ```
95
- *
96
- * @param s The string to parse.
97
- * @returns `Result.ok(parsedInt)` for valid input, otherwise `Result.err`
98
- * wrapping an `Error` describing the invalid input.
99
- */
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
100
  Num.safeParseInt = (s) => {
101
101
  const viaNumber = Number(s);
102
- // `Number('')` / `Number(' ')` は 0 を返すが、`parseInt` は NaN を返す。
103
- // 末尾不正文字 ('12abc' 等) は `Number` 側が NaN にするので、両者が共に
104
- // 有効な場合のみ採用することで空文字・空白のみ・末尾不正をまとめて弾く。
102
+
103
+
104
+
105
105
  return Number.isNaN(viaNumber) || Number.isNaN(Number.parseInt(s, 10))
106
106
  ? Result.err(new Error(`safeParseInt: "${s}" is not a valid base-10 integer`))
107
- : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
107
+ :
108
108
  Result.ok(Math.trunc(viaNumber));
109
109
  };
110
- /**
111
- * Safely parses a finite floating-point number from a string, returning a
112
- * {@link Result} that is `Ok<FiniteNumber>` for valid input and `Err<Error>`
113
- * otherwise.
114
- *
115
- * This is a stricter alternative to both `parseFloat` and `Number`:
116
- *
117
- * - Unlike `parseFloat('12abc')` (which returns `12`), trailing non-numeric
118
- * characters make the whole input invalid and yield `Err`.
119
- * - Unlike `Number('')` / `Number(' ')` (which return `0`), empty or
120
- * whitespace-only input yields `Err`.
121
- * - Unlike `Number('Infinity')` (which returns `Infinity`), non-finite values
122
- * yield `Err`.
123
- *
124
- * The empty-string case is rejected by delegating to `parseFloat` (which
125
- * returns `NaN` there) rather than hard-coding a check, while the trailing-
126
- * garbage case is rejected via `Number`. Decimal values are preserved as-is,
127
- * so `'12.9'` stays `12.9`.
128
- *
129
- * Use `Result.unwrapOk` (optionally with a `?? Number.NaN` fallback) or
130
- * `Result.unwrapOkOr` to get a plain number back.
131
- *
132
- * @example
133
- *
134
- * ```ts
135
- * assert.strictEqual(
136
- * Result.unwrapOkOr(Num.safeParseFloat('12.9'), Number.NaN),
137
- * 12.9,
138
- * );
139
- *
140
- * assert.strictEqual(
141
- * Result.unwrapOkOr(Num.safeParseFloat('-3.5'), Number.NaN),
142
- * -3.5,
143
- * );
144
- *
145
- * assert.strictEqual(
146
- * Result.unwrapOkOr(Num.safeParseFloat('1e3'), Number.NaN),
147
- * 1000,
148
- * );
149
- *
150
- * // Native `parseFloat` ignores trailing non-numeric characters
151
- *
152
- * assert.strictEqual(Number.parseFloat('12px'), 12);
153
- *
154
- * assert.isTrue(Result.isErr(Num.safeParseFloat('12px')));
155
- *
156
- * // Whitespace is not a valid number, so we return an error instead of coercing to 0.
157
- *
158
- * assert.isTrue(Result.isErr(Num.safeParseFloat('')));
159
- *
160
- * assert.isTrue(Result.isErr(Num.safeParseFloat(' ')));
161
- *
162
- * // Infinity and NaN are not finite, so they are rejected.
163
- *
164
- * assert.isTrue(Result.isErr(Num.safeParseFloat('Infinity')));
165
- *
166
- * assert.isTrue(Result.isErr(Num.safeParseFloat('NaN')));
167
- * ```
168
- *
169
- * @param s The string to parse.
170
- * @returns `Result.ok(parsedFloat)` for valid finite input, otherwise
171
- * `Result.err` wrapping an `Error` describing the invalid input.
172
- */
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
173
  Num.safeParseFloat = (s) => {
174
174
  const viaNumber = Number(s);
175
- // `Number('')` / `Number(' ')` は 0 を返すが、`parseFloat` は NaN を返す。
176
- // 末尾不正文字 ('12abc' 等) は `Number` 側が NaN にするので、両者が共に
177
- // 非 NaN かつ有限の場合のみ採用することで空文字・空白のみ・末尾不正・
178
- // Infinity をまとめて弾く。
175
+
176
+
177
+
178
+
179
179
  return Number.isNaN(viaNumber) ||
180
180
  !Number.isFinite(viaNumber) ||
181
181
  Number.isNaN(Number.parseFloat(s))
182
182
  ? Result.err(new Error(`safeParseFloat: "${s}" is not a valid finite number`))
183
- : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
183
+ :
184
184
  Result.ok(viaNumber);
185
185
  };
186
- /**
187
- * Type guard that checks if a number is non-zero.
188
- *
189
- * When this function returns `true`, TypeScript narrows the type to exclude
190
- * zero, providing compile-time safety for division operations and other
191
- * calculations that require non-zero values.
192
- *
193
- * @example
194
- *
195
- * ```ts
196
- * const value: number = 5;
197
- *
198
- * if (Num.isNonZero(value)) {
199
- * // Safe to divide now that we know value is non-zero
200
- * // eslint-disable-next-line total-functions/no-partial-division
201
- * const inverted = 1 / value;
202
- *
203
- * assert.isTrue(inverted === 0.2);
204
- * }
205
- *
206
- * assert.isFalse(Num.isNonZero(0));
207
- * ```
208
- *
209
- * @template N - The numeric literal type or number type to check
210
- * @param num - The number to check
211
- * @returns `true` if the number is not zero, `false` otherwise
212
- */
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
213
  Num.isNonZero = (num) => num !== 0;
214
214
 
215
- /**
216
- * Type guard that checks if a number is non-negative (greater than or equal
217
- * to zero).
218
- *
219
- * When this function returns `true`, TypeScript narrows the type to exclude
220
- * negative values, which is useful for operations that require non-negative
221
- * inputs like array indices or measurements.
222
- *
223
- * @example
224
- *
225
- * ```ts
226
- * const candidate = 10;
227
- *
228
- * if (Num.isNonNegative(candidate)) {
229
- * const index: number = candidate;
230
- *
231
- * assert.isTrue(index === 10);
232
- * }
233
- *
234
- * assert.isFalse(Num.isNonNegative(-1));
235
- * ```
236
- *
237
- * @template N - The numeric literal type or number type to check
238
- * @param num - The number to check
239
- * @returns `true` if the number is >= 0, `false` otherwise
240
- */
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
241
  Num.isNonNegative = (num) => num >= 0;
242
- /**
243
- * Type guard that checks if a number is positive (greater than zero).
244
- *
245
- * When this function returns `true`, TypeScript narrows the type to exclude
246
- * zero and negative values. This is particularly useful for validating inputs
247
- * that must be strictly positive, such as dimensions, counts, or rates.
248
- *
249
- * @example
250
- *
251
- * ```ts
252
- * const amount = 42;
253
- *
254
- * if (Num.isPositive(amount)) {
255
- * assert.isTrue(amount > 0);
256
- * }
257
- *
258
- * assert.isFalse(Num.isPositive(0));
259
- * ```
260
- *
261
- * @template N - The numeric literal type or number type to check
262
- * @param num - The number to check
263
- * @returns `true` if the number is > 0, `false` otherwise
264
- */
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
265
  Num.isPositive = (num) => num > 0;
266
- /**
267
- * Creates a function that checks if a number `x` is within the range
268
- * `lowerBound <= x < upperBound`.
269
- *
270
- * @example
271
- *
272
- * ```ts
273
- * const isGrade = Num.isInRange(0, 100);
274
- *
275
- * assert.isTrue(isGrade(50));
276
- *
277
- * assert.isFalse(isGrade(100));
278
- * ```
279
- *
280
- * @param lowerBound The lower bound (inclusive).
281
- * @param upperBound The upper bound (exclusive).
282
- * @returns A function that takes a number `x` and returns `true` if `x` is in
283
- * the range, `false` otherwise.
284
- */
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
285
  Num.isInRange = (lowerBound, upperBound) => (x) => lowerBound <= x && x < upperBound;
286
- /**
287
- * Creates a function that checks if a number `x` is within the range
288
- * `lowerBound <= x <= upperBound`.
289
- *
290
- * @example
291
- *
292
- * ```ts
293
- * const isPercentage = Num.isInRangeInclusive(0, 100);
294
- *
295
- * assert.isTrue(isPercentage(100));
296
- *
297
- * assert.isFalse(isPercentage(-1));
298
- * ```
299
- *
300
- * @param lowerBound The lower bound (inclusive).
301
- * @param upperBound The upper bound (inclusive).
302
- * @returns A function that takes a number `x` and returns `true` if `x` is in
303
- * the range, `false` otherwise.
304
- */
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
305
  Num.isInRangeInclusive = (lowerBound, upperBound) => (x) => lowerBound <= x && x <= upperBound;
306
- /**
307
- * Creates a type guard that checks if a number is an unsigned integer within
308
- * a specified range.
309
- *
310
- * This function returns a predicate that validates whether a number is:
311
- *
312
- * - A safe integer (no floating point)
313
- * - Within the range [lowerBound, upperBound)
314
- *
315
- * The returned type guard provides precise type narrowing when the bounds are
316
- * SmallUint literals, making it ideal for array index validation.
317
- *
318
- * @example
319
- *
320
- * ```ts
321
- * const indexGuard = Num.isUintInRange(0, 5);
322
- *
323
- * assert.isTrue(indexGuard(3));
324
- *
325
- * assert.isFalse(indexGuard(5));
326
- *
327
- * assert.isFalse(indexGuard(-1));
328
- * ```
329
- *
330
- * @template L - The lower bound as a SmallUint literal type
331
- * @template U - The upper bound as a SmallUint literal type
332
- * @param lowerBound - The minimum value (inclusive)
333
- * @param upperBound - The maximum value (exclusive)
334
- * @returns A type guard function that validates and narrows number types
335
- */
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
336
  Num.isUintInRange = (lowerBound, upperBound) => (x) => Number.isSafeInteger(x) && lowerBound <= x && x < upperBound;
337
- /**
338
- * Creates a type guard that checks if a number is an unsigned integer within
339
- * a specified inclusive range.
340
- *
341
- * This function returns a predicate that validates whether a number is:
342
- *
343
- * - A safe integer (no floating point)
344
- * - Within the range [lowerBound, upperBound] (both bounds inclusive)
345
- *
346
- * The returned type guard provides precise type narrowing when the bounds are
347
- * SmallUint literals, useful for validating scores, percentages, or other
348
- * bounded values.
349
- *
350
- * @example
351
- *
352
- * ```ts
353
- * const inclusiveGuard = Num.isUintInRangeInclusive(0, 5);
354
- *
355
- * assert.isTrue(inclusiveGuard(5));
356
- *
357
- * assert.isFalse(inclusiveGuard(6));
358
- * ```
359
- *
360
- * @template L - The lower bound as a SmallUint literal type
361
- * @template U - The upper bound as a SmallUint literal type
362
- * @param lowerBound - The minimum value (inclusive)
363
- * @param upperBound - The maximum value (inclusive)
364
- * @returns A type guard function that validates and narrows number types
365
- */
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+
357
+
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
366
  Num.isUintInRangeInclusive = (lowerBound, upperBound) => (x) => Number.isSafeInteger(x) && lowerBound <= x && x <= upperBound;
367
367
  function clamp(...args) {
368
368
  switch (args.length) {
@@ -379,173 +379,173 @@ export var Num;
379
379
  }
380
380
  }
381
381
  Num.clamp = clamp;
382
- /**
383
- * Adds two numbers, returning the tightest sound branded result type.
384
- *
385
- * The sign of the result is computed exactly (e.g. a non-negative plus a
386
- * non-negative is non-negative); the integer/finite level is preserved, with
387
- * `SafeInt` operands widening to `Int` because the sum may exceed the safe
388
- * range. No clamping is performed — for a result that stays inside a bounded
389
- * type, use that type's branded `add` instead.
390
- *
391
- * @param a - The first addend.
392
- * @param b - The second addend.
393
- * @returns `a + b`, typed as the tightest branded type guaranteed to contain
394
- * it.
395
- */
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
396
  Num.add = (a, b) =>
397
- // eslint-disable-next-line total-functions/no-unsafe-type-assertion
397
+
398
398
  (a + b);
399
- /**
400
- * Subtracts `b` from `a`, returning the tightest sound branded result type.
401
- *
402
- * The sign is computed exactly; the level is preserved (with `SafeInt`
403
- * widening to `Int`). No clamping — for saturating subtraction that stays in a
404
- * bounded type (e.g. `Uint.sub` clamping at `0`), use that type's branded
405
- * `sub`.
406
- *
407
- * @param a - The minuend.
408
- * @param b - The subtrahend.
409
- * @returns `a - b`, typed as the tightest branded type guaranteed to contain
410
- * it.
411
- */
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
412
  Num.sub = (a, b) =>
413
- // eslint-disable-next-line total-functions/no-unsafe-type-assertion
413
+
414
414
  (a - b);
415
- /**
416
- * Multiplies two numbers, returning the tightest sound branded result type.
417
- *
418
- * The sign follows the usual product rule (e.g. negative times negative is
419
- * positive); the level is preserved (with `SafeInt` widening to `Int`). No
420
- * clamping.
421
- *
422
- * @param a - The first factor.
423
- * @param b - The second factor.
424
- * @returns `a * b`, typed as the tightest branded type guaranteed to contain
425
- * it.
426
- */
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+
424
+
425
+
426
+
427
427
  Num.mul = (a, b) =>
428
- // eslint-disable-next-line total-functions/no-unsafe-type-assertion
428
+
429
429
  (a * b);
430
- /**
431
- * Performs type-safe exact division with a compile-time zero-check.
432
- *
433
- * The divisor must be typed as `NonZeroNumber` or a non-zero numeric literal.
434
- * The result sign follows the quotient rule; because exact division can be
435
- * fractional, the level is `finite` (or `number`).
436
- *
437
- * @param a - The dividend.
438
- * @param b - The divisor (must be non-zero, enforced by types).
439
- * @returns `a / b`, typed as the tightest branded type guaranteed to contain
440
- * it.
441
- */
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+
442
442
  Num.div = (a, b) =>
443
- // eslint-disable-next-line total-functions/no-partial-division, total-functions/no-unsafe-type-assertion
443
+
444
444
  (a / b);
445
- /**
446
- * Performs integer division using floor division.
447
- *
448
- * Computes `⌊a / b⌋` by flooring both operands before division and then
449
- * flooring the result. This ensures integer arithmetic semantics.
450
- *
451
- * Note: Unlike `div`, this function does not enforce non-zero divisor at
452
- * compile time. Division by zero returns `NaN`.
453
- *
454
- * @param a - The dividend
455
- * @param b - The divisor
456
- * @returns The integer quotient, or `NaN` if b is zero
457
- */
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+
456
+
457
+
458
458
  Num.divInt = (a, b) => {
459
- // eslint-disable-next-line total-functions/no-partial-division
459
+
460
460
  const quotient = Math.floor(Math.floor(a) / Math.floor(b));
461
- // eslint-disable-next-line total-functions/no-unsafe-type-assertion
461
+
462
462
  return quotient;
463
463
  };
464
- /**
465
- * Rounds a number to a specified number of decimal places.
466
- *
467
- * Uses the standard rounding algorithm (round half up) to round the number to
468
- * the given precision. The precision must be a positive safe integer.
469
- *
470
- * @param num - The number to round
471
- * @param precision - The number of decimal places (must be positive)
472
- * @returns The rounded number
473
- */
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
474
  Num.roundAt = (num, precision) => {
475
475
  const digit = 10 ** precision;
476
- // eslint-disable-next-line total-functions/no-partial-division
476
+
477
477
  return Math.round(num * digit) / digit;
478
478
  };
479
- /**
480
- * Rounds a number to the nearest integer using bitwise operations.
481
- *
482
- * This function uses a bitwise OR trick for potentially faster rounding.
483
- * Note: This implementation rounds half up for positive numbers but may
484
- * behave differently for negative numbers compared to Math.round.
485
- *
486
- * @param num - The number to round
487
- * @returns The rounded integer as an Int branded type
488
- */
489
- // eslint-disable-next-line total-functions/no-unsafe-type-assertion
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+
488
+
489
+
490
490
  Num.roundToInt = (num) => Math.trunc(num + 0.5);
491
- /**
492
- * Creates a reusable rounding function with a fixed precision.
493
- *
494
- * This is a curried version of roundAt that returns a function configured to
495
- * always round to the specified number of decimal places. Useful for creating
496
- * consistent rounding behavior across multiple values.
497
- *
498
- * @param digit - The number of decimal places for rounding
499
- * @returns A function that rounds numbers to the specified precision
500
- */
491
+
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
501
  Num.round = (digit) => {
502
502
  const powAmount = 10 ** digit;
503
503
  return (target) =>
504
- // eslint-disable-next-line total-functions/no-partial-division
504
+
505
505
  Num.roundToInt(powAmount * target) / powAmount;
506
506
  };
507
- /**
508
- * Converts NaN values to undefined while preserving all other numbers.
509
- *
510
- * This function is useful for handling potentially invalid numeric operations
511
- * in a type-safe way, converting NaN results to undefined for easier handling
512
- * with optional chaining or nullish coalescing.
513
- *
514
- * @template N - The numeric type (literal or number)
515
- * @param num - The number to check
516
- * @returns The original number if not NaN, otherwise undefined
517
- */
507
+
508
+
509
+
510
+
511
+
512
+
513
+
514
+
515
+
516
+
517
+
518
518
  Num.mapNaN2Undefined = (num) => Number.isNaN(num)
519
519
  ? undefined
520
- : // eslint-disable-next-line total-functions/no-unsafe-type-assertion
520
+ :
521
521
  num;
522
- /**
523
- * Type-safe increment operation for SmallUint values.
524
- *
525
- * Increments a SmallUint (0-40) by 1 with the result type computed at compile
526
- * time. This provides type-level arithmetic for small unsigned integers,
527
- * useful for type-safe counter operations.
528
- *
529
- * @template N - A SmallUint literal type (0-40)
530
- * @param n - The SmallUint value to increment
531
- * @returns The incremented value with type Increment<N>
532
- */
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
533
  Num.increment = (n) =>
534
- // eslint-disable-next-line total-functions/no-unsafe-type-assertion
534
+
535
535
  (n + 1);
536
- /**
537
- * Type-safe decrement operation for positive SmallInt values.
538
- *
539
- * Decrements a positive SmallInt (1-40) by 1 with the result type computed at
540
- * compile time. This provides type-level arithmetic for small positive
541
- * integers, useful for type-safe countdown operations.
542
- *
543
- * @template N - A positive SmallInt literal type (1-40)
544
- * @param n - The positive SmallInt value to decrement
545
- * @returns The decremented value with type Decrement<N>
546
- */
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
544
+
545
+
546
+
547
547
  Num.decrement = (n) =>
548
- // eslint-disable-next-line total-functions/no-unsafe-type-assertion
548
+
549
549
  (n - 1);
550
550
  })(Num || (Num = {}));
551
551
  //# sourceMappingURL=num.mjs.map