ts-data-forge 6.13.1 → 7.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.
Files changed (257) hide show
  1. package/README.md +2 -2
  2. package/dist/array/impl/array-utils-creation.d.mts.map +1 -1
  3. package/dist/array/impl/array-utils-creation.mjs +1 -1
  4. package/dist/array/impl/array-utils-creation.mjs.map +1 -1
  5. package/dist/array/impl/array-utils-iterators.mjs +7 -1
  6. package/dist/array/impl/array-utils-iterators.mjs.map +1 -1
  7. package/dist/array/impl/array-utils-modification.mjs +6 -0
  8. package/dist/array/impl/array-utils-modification.mjs.map +1 -1
  9. package/dist/array/impl/array-utils-reducing-value.d.mts +1 -1
  10. package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
  11. package/dist/array/impl/array-utils-reducing-value.mjs +6 -1
  12. package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
  13. package/dist/array/impl/array-utils-search.d.mts +0 -2
  14. package/dist/array/impl/array-utils-search.d.mts.map +1 -1
  15. package/dist/array/impl/array-utils-search.mjs +6 -0
  16. package/dist/array/impl/array-utils-search.mjs.map +1 -1
  17. package/dist/array/impl/array-utils-set-op.d.mts.map +1 -1
  18. package/dist/array/impl/array-utils-set-op.mjs +0 -1
  19. package/dist/array/impl/array-utils-set-op.mjs.map +1 -1
  20. package/dist/array/impl/array-utils-slice-clamped.mjs +6 -0
  21. package/dist/array/impl/array-utils-slice-clamped.mjs.map +1 -1
  22. package/dist/array/impl/array-utils-slicing.mjs +6 -0
  23. package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
  24. package/dist/array/impl/array-utils-transformation.d.mts +1 -1
  25. package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
  26. package/dist/array/impl/array-utils-transformation.mjs +8 -4
  27. package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
  28. package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
  29. package/dist/array/impl/array-utils-validation.mjs +9 -9
  30. package/dist/array/impl/array-utils-validation.mjs.map +1 -1
  31. package/dist/collections/imap-mapped.d.mts +622 -3
  32. package/dist/collections/imap-mapped.d.mts.map +1 -1
  33. package/dist/collections/imap-mapped.mjs +117 -18
  34. package/dist/collections/imap-mapped.mjs.map +1 -1
  35. package/dist/collections/imap.d.mts +3 -3
  36. package/dist/collections/imap.d.mts.map +1 -1
  37. package/dist/collections/imap.mjs +12 -5
  38. package/dist/collections/imap.mjs.map +1 -1
  39. package/dist/collections/iset-mapped.d.mts +4 -4
  40. package/dist/collections/iset-mapped.d.mts.map +1 -1
  41. package/dist/collections/iset-mapped.mjs +14 -4
  42. package/dist/collections/iset-mapped.mjs.map +1 -1
  43. package/dist/collections/iset.d.mts +2 -2
  44. package/dist/collections/iset.d.mts.map +1 -1
  45. package/dist/collections/iset.mjs +11 -2
  46. package/dist/collections/iset.mjs.map +1 -1
  47. package/dist/collections/queue.mjs +26 -20
  48. package/dist/collections/queue.mjs.map +1 -1
  49. package/dist/collections/stack.mjs +23 -17
  50. package/dist/collections/stack.mjs.map +1 -1
  51. package/dist/entry-point.mjs +6 -0
  52. package/dist/entry-point.mjs.map +1 -1
  53. package/dist/functional/optional/impl/optional-flat-map.d.mts +2 -2
  54. package/dist/functional/pipe.d.mts.map +1 -1
  55. package/dist/functional/pipe.mjs.map +1 -1
  56. package/dist/functional/result/impl/result-flat-map.d.mts +2 -2
  57. package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts +5 -3
  58. package/dist/functional/ternary-result/impl/ternary-result-flat-map.d.mts.map +1 -1
  59. package/dist/functional/ternary-result/impl/ternary-result-flat-map.mjs.map +1 -1
  60. package/dist/guard/has-key.d.mts.map +1 -1
  61. package/dist/guard/has-key.mjs +1 -3
  62. package/dist/guard/has-key.mjs.map +1 -1
  63. package/dist/guard/is-non-null-object.d.mts.map +1 -1
  64. package/dist/guard/is-non-null-object.mjs +1 -3
  65. package/dist/guard/is-non-null-object.mjs.map +1 -1
  66. package/dist/guard/key-is-in.d.mts.map +1 -1
  67. package/dist/guard/key-is-in.mjs +1 -3
  68. package/dist/guard/key-is-in.mjs.map +1 -1
  69. package/dist/index.mjs +6 -0
  70. package/dist/index.mjs.map +1 -1
  71. package/dist/iterator/range.mjs +6 -0
  72. package/dist/iterator/range.mjs.map +1 -1
  73. package/dist/json/json.mjs +11 -7
  74. package/dist/json/json.mjs.map +1 -1
  75. package/dist/number/branded-types/finite-number.d.mts +1 -1
  76. package/dist/number/branded-types/index.d.mts +6 -0
  77. package/dist/number/branded-types/index.d.mts.map +1 -1
  78. package/dist/number/branded-types/index.mjs +6 -0
  79. package/dist/number/branded-types/index.mjs.map +1 -1
  80. package/dist/number/branded-types/int.d.mts +1 -2
  81. package/dist/number/branded-types/int.d.mts.map +1 -1
  82. package/dist/number/branded-types/int16.d.mts +2 -3
  83. package/dist/number/branded-types/int16.d.mts.map +1 -1
  84. package/dist/number/branded-types/int32.d.mts +2 -3
  85. package/dist/number/branded-types/int32.d.mts.map +1 -1
  86. package/dist/number/branded-types/negative-finite-number.d.mts +143 -0
  87. package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -0
  88. package/dist/number/branded-types/negative-finite-number.mjs +156 -0
  89. package/dist/number/branded-types/negative-finite-number.mjs.map +1 -0
  90. package/dist/number/branded-types/negative-int.d.mts +120 -0
  91. package/dist/number/branded-types/negative-int.d.mts.map +1 -0
  92. package/dist/number/branded-types/negative-int.mjs +125 -0
  93. package/dist/number/branded-types/negative-int.mjs.map +1 -0
  94. package/dist/number/branded-types/negative-safe-int.d.mts +132 -0
  95. package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -0
  96. package/dist/number/branded-types/negative-safe-int.mjs +136 -0
  97. package/dist/number/branded-types/negative-safe-int.mjs.map +1 -0
  98. package/dist/number/branded-types/non-negative-finite-number.d.mts +1 -1
  99. package/dist/number/branded-types/non-negative-int16.d.mts +1 -1
  100. package/dist/number/branded-types/non-negative-int32.d.mts +1 -1
  101. package/dist/number/branded-types/non-positive-finite-number.d.mts +130 -0
  102. package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -0
  103. package/dist/number/branded-types/non-positive-finite-number.mjs +134 -0
  104. package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -0
  105. package/dist/number/branded-types/non-positive-int.d.mts +224 -0
  106. package/dist/number/branded-types/non-positive-int.d.mts.map +1 -0
  107. package/dist/number/branded-types/non-positive-int.mjs +222 -0
  108. package/dist/number/branded-types/non-positive-int.mjs.map +1 -0
  109. package/dist/number/branded-types/non-positive-safe-int.d.mts +119 -0
  110. package/dist/number/branded-types/non-positive-safe-int.d.mts.map +1 -0
  111. package/dist/number/branded-types/non-positive-safe-int.mjs +116 -0
  112. package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -0
  113. package/dist/number/branded-types/non-zero-finite-number.d.mts +5 -17
  114. package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
  115. package/dist/number/branded-types/non-zero-finite-number.mjs +5 -17
  116. package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
  117. package/dist/number/branded-types/non-zero-int.d.mts +5 -33
  118. package/dist/number/branded-types/non-zero-int.d.mts.map +1 -1
  119. package/dist/number/branded-types/non-zero-int.mjs +5 -25
  120. package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
  121. package/dist/number/branded-types/non-zero-int16.d.mts +6 -43
  122. package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
  123. package/dist/number/branded-types/non-zero-int16.mjs +5 -25
  124. package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
  125. package/dist/number/branded-types/non-zero-int32.d.mts +6 -39
  126. package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
  127. package/dist/number/branded-types/non-zero-int32.mjs +5 -25
  128. package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
  129. package/dist/number/branded-types/non-zero-safe-int.d.mts +5 -37
  130. package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
  131. package/dist/number/branded-types/non-zero-safe-int.mjs +7 -30
  132. package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
  133. package/dist/number/branded-types/non-zero-uint16.d.mts +6 -33
  134. package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
  135. package/dist/number/branded-types/non-zero-uint16.mjs +5 -17
  136. package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
  137. package/dist/number/branded-types/non-zero-uint32.d.mts +6 -31
  138. package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
  139. package/dist/number/branded-types/non-zero-uint32.mjs +5 -17
  140. package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
  141. package/dist/number/branded-types/positive-finite-number.d.mts +4 -2
  142. package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
  143. package/dist/number/branded-types/positive-int.d.mts +4 -2
  144. package/dist/number/branded-types/positive-int.d.mts.map +1 -1
  145. package/dist/number/branded-types/positive-int16.d.mts +4 -2
  146. package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
  147. package/dist/number/branded-types/positive-int32.d.mts +4 -2
  148. package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
  149. package/dist/number/branded-types/positive-safe-int.d.mts +4 -2
  150. package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
  151. package/dist/number/branded-types/positive-safe-int.mjs +1 -1
  152. package/dist/number/branded-types/positive-uint16.d.mts +4 -2
  153. package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
  154. package/dist/number/branded-types/positive-uint32.d.mts +4 -2
  155. package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
  156. package/dist/number/branded-types/safe-int.d.mts +1 -2
  157. package/dist/number/branded-types/safe-int.d.mts.map +1 -1
  158. package/dist/number/branded-types/safe-int.mjs +2 -2
  159. package/dist/number/branded-types/safe-uint.d.mts +1 -1
  160. package/dist/number/branded-types/safe-uint.mjs +1 -1
  161. package/dist/number/branded-types/uint.d.mts +1 -1
  162. package/dist/number/branded-types/uint16.d.mts +1 -1
  163. package/dist/number/branded-types/uint32.d.mts +1 -1
  164. package/dist/number/index.d.mts +1 -0
  165. package/dist/number/index.d.mts.map +1 -1
  166. package/dist/number/index.mjs +6 -0
  167. package/dist/number/index.mjs.map +1 -1
  168. package/dist/number/num-arithmetic-types.d.mts +74 -0
  169. package/dist/number/num-arithmetic-types.d.mts.map +1 -0
  170. package/dist/number/num-arithmetic-types.mjs +2 -0
  171. package/dist/number/num-arithmetic-types.mjs.map +1 -0
  172. package/dist/number/num.d.mts +53 -9
  173. package/dist/number/num.d.mts.map +1 -1
  174. package/dist/number/num.mjs +67 -15
  175. package/dist/number/num.mjs.map +1 -1
  176. package/dist/number/refined-number-utils.d.mts +12 -9
  177. package/dist/number/refined-number-utils.d.mts.map +1 -1
  178. package/dist/number/refined-number-utils.mjs.map +1 -1
  179. package/dist/object/object.mjs +7 -1
  180. package/dist/object/object.mjs.map +1 -1
  181. package/dist/others/fast-deep-equal.d.mts.map +1 -1
  182. package/dist/others/fast-deep-equal.mjs +0 -1
  183. package/dist/others/fast-deep-equal.mjs.map +1 -1
  184. package/dist/others/unknown-to-string.mjs +2 -2
  185. package/dist/others/unknown-to-string.mjs.map +1 -1
  186. package/package.json +6 -6
  187. package/src/array/impl/array-utils-creation.mts +1 -1
  188. package/src/array/impl/array-utils-iterators.mts +1 -1
  189. package/src/array/impl/array-utils-reducing-value.mts +1 -2
  190. package/src/array/impl/array-utils-search.mts +0 -2
  191. package/src/array/impl/array-utils-set-op.mts +0 -1
  192. package/src/array/impl/array-utils-transformation.mts +8 -6
  193. package/src/array/impl/array-utils-validation.mts +1 -5
  194. package/src/collections/imap-mapped.mts +696 -34
  195. package/src/collections/imap-mapped.test.mts +61 -13
  196. package/src/collections/imap.mts +20 -9
  197. package/src/collections/imap.test.mts +9 -8
  198. package/src/collections/iset-mapped.mts +18 -10
  199. package/src/collections/iset-mapped.test.mts +6 -1
  200. package/src/collections/iset.mts +11 -4
  201. package/src/collections/iset.test.mts +6 -8
  202. package/src/collections/queue.mts +32 -32
  203. package/src/collections/stack.mts +26 -26
  204. package/src/functional/optional/impl/optional-flat-map.mts +2 -2
  205. package/src/functional/optional.test.mts +6 -4
  206. package/src/functional/pipe.mts +0 -1
  207. package/src/functional/pipe.test.mts +2 -1
  208. package/src/functional/result/impl/result-flat-map.mts +2 -2
  209. package/src/functional/result.test.mts +3 -2
  210. package/src/functional/ternary-result/impl/ternary-result-flat-map.mts +5 -3
  211. package/src/functional/ternary-result.test.mts +8 -3
  212. package/src/guard/has-key.mts +1 -3
  213. package/src/guard/is-non-null-object.mts +0 -1
  214. package/src/guard/key-is-in.mts +1 -3
  215. package/src/json/json.mts +5 -8
  216. package/src/json/json.test.mts +2 -2
  217. package/src/number/branded-types/index.mts +6 -0
  218. package/src/number/branded-types/negative-finite-number.mts +210 -0
  219. package/src/number/branded-types/negative-finite-number.test.mts +172 -0
  220. package/src/number/branded-types/negative-int.mts +160 -0
  221. package/src/number/branded-types/negative-int.test.mts +146 -0
  222. package/src/number/branded-types/negative-safe-int.mts +173 -0
  223. package/src/number/branded-types/negative-safe-int.test.mts +120 -0
  224. package/src/number/branded-types/non-positive-finite-number.mts +191 -0
  225. package/src/number/branded-types/non-positive-finite-number.test.mts +286 -0
  226. package/src/number/branded-types/non-positive-int.mts +251 -0
  227. package/src/number/branded-types/non-positive-int.test.mts +197 -0
  228. package/src/number/branded-types/non-positive-safe-int.mts +147 -0
  229. package/src/number/branded-types/non-positive-safe-int.test.mts +223 -0
  230. package/src/number/branded-types/non-zero-finite-number.mts +14 -22
  231. package/src/number/branded-types/non-zero-finite-number.test.mts +0 -12
  232. package/src/number/branded-types/non-zero-int.mts +10 -32
  233. package/src/number/branded-types/non-zero-int.test.mts +0 -20
  234. package/src/number/branded-types/non-zero-int16.mts +8 -32
  235. package/src/number/branded-types/non-zero-int16.test.mts +0 -30
  236. package/src/number/branded-types/non-zero-int32.mts +8 -32
  237. package/src/number/branded-types/non-zero-int32.test.mts +0 -30
  238. package/src/number/branded-types/non-zero-safe-int.mts +10 -37
  239. package/src/number/branded-types/non-zero-safe-int.test.mts +0 -32
  240. package/src/number/branded-types/non-zero-uint16.mts +8 -22
  241. package/src/number/branded-types/non-zero-uint16.test.mts +0 -16
  242. package/src/number/branded-types/non-zero-uint32.mts +8 -22
  243. package/src/number/branded-types/non-zero-uint32.test.mts +0 -16
  244. package/src/number/branded-types/positive-safe-int.mts +1 -1
  245. package/src/number/branded-types/safe-int.mts +2 -2
  246. package/src/number/branded-types/safe-uint.mts +1 -1
  247. package/src/number/index.mts +1 -0
  248. package/src/number/num-arithmetic-types.mts +265 -0
  249. package/src/number/num-arithmetic-types.test.mts +130 -0
  250. package/src/number/num-arithmetic.test.mts +102 -0
  251. package/src/number/num.mts +99 -18
  252. package/src/number/num.test.mts +2 -2
  253. package/src/number/refined-number-utils.mts +19 -9
  254. package/src/object/object.mts +2 -2
  255. package/src/others/fast-deep-equal.mts +0 -2
  256. package/src/others/map-nullable.test.mts +4 -3
  257. package/src/others/unknown-to-string.mts +2 -2
@@ -0,0 +1,160 @@
1
+ import {
2
+ type NegativeInt as TtfImported_NegativeInt,
3
+ type WithSmallInt,
4
+ } from 'ts-type-forge';
5
+ import { TsDataForgeInternals } from '../refined-number-utils.mjs';
6
+ import { PositiveInt } from './positive-int.mjs';
7
+
8
+ export type NegativeInt = TtfImported_NegativeInt;
9
+
10
+ type ElementType = NegativeInt;
11
+
12
+ const typeNameInMessage = 'a negative integer';
13
+
14
+ const {
15
+ MAX_VALUE,
16
+ add,
17
+ castType,
18
+ clamp,
19
+ is,
20
+ max: max_,
21
+ min: min_,
22
+ pow,
23
+ random,
24
+ sub,
25
+ } = TsDataForgeInternals.RefinedNumberUtils.operatorsForInteger<
26
+ ElementType,
27
+ number,
28
+ -1
29
+ >({
30
+ integerOrSafeInteger: 'Integer',
31
+ nonZero: true,
32
+ MIN_VALUE: -Number.MAX_VALUE,
33
+ MAX_VALUE: -1,
34
+ typeNameInMessage,
35
+ } as const);
36
+
37
+ /**
38
+ * Returns the absolute value of a negative integer.
39
+ *
40
+ * The absolute value of a negative integer is always a positive integer
41
+ * (`>= 1`), so the result is typed as a {@link PositiveInt}.
42
+ *
43
+ * @param x - The negative integer.
44
+ * @returns `|x|` as a PositiveInt.
45
+ */
46
+ const abs = (x: WithSmallInt<ElementType>): PositiveInt =>
47
+ PositiveInt.clamp(Math.abs(x));
48
+
49
+ /**
50
+ * Checks if a number is a NegativeInt (a negative integer `<= -1`).
51
+ *
52
+ * @param value The value to check.
53
+ * @returns `true` if the value is a NegativeInt, `false` otherwise.
54
+ */
55
+ export const isNegativeInt = is;
56
+
57
+ /**
58
+ * Casts a number to a NegativeInt type.
59
+ *
60
+ * @param value The value to cast.
61
+ * @returns The value as a NegativeInt type.
62
+ * @throws {TypeError} If the value is not a negative integer.
63
+ */
64
+ export const asNegativeInt = castType;
65
+
66
+ /**
67
+ * Namespace providing type-safe arithmetic operations for negative integers.
68
+ *
69
+ * NegativeInt represents integers that are strictly less than zero (`<= -1`).
70
+ * Operations that stay within the negative integers (such as `add`) clamp their
71
+ * results to the valid range, while operations whose result leaves the set
72
+ * (`mul`, `div`, `abs`) are typed to reflect the actual sign of the result.
73
+ */
74
+ export const NegativeInt = {
75
+ /**
76
+ * Type guard to check if a value is a NegativeInt.
77
+ *
78
+ * @param value The value to check.
79
+ * @returns `true` if the value is a negative integer, `false` otherwise.
80
+ */
81
+ is,
82
+
83
+ /**
84
+ * The maximum value for a negative integer.
85
+ *
86
+ * @readonly
87
+ */
88
+ MAX_VALUE,
89
+
90
+ /**
91
+ * Returns the absolute value of a negative integer.
92
+ *
93
+ * @param a The negative integer.
94
+ * @returns The absolute value as a PositiveInt.
95
+ */
96
+ abs,
97
+
98
+ /**
99
+ * Returns the smaller of two negative integers.
100
+ *
101
+ * @param a The first negative integer.
102
+ * @param b The second negative integer.
103
+ * @returns The minimum value as a NegativeInt.
104
+ */
105
+ min: min_,
106
+
107
+ /**
108
+ * Returns the larger of two negative integers.
109
+ *
110
+ * @param a The first negative integer.
111
+ * @param b The second negative integer.
112
+ * @returns The maximum value as a NegativeInt.
113
+ */
114
+ max: max_,
115
+
116
+ /**
117
+ * Clamps a number to the negative integer range.
118
+ *
119
+ * @param value The number to clamp.
120
+ * @returns The value clamped to `[-∞, -1]` as a NegativeInt.
121
+ */
122
+ clamp,
123
+
124
+ /**
125
+ * Generates a random NegativeInt value within the valid range.
126
+ *
127
+ * @returns A random negative integer.
128
+ */
129
+ random,
130
+
131
+ /**
132
+ * Raises a negative integer to the power of another negative integer.
133
+ *
134
+ * @param a The base negative integer.
135
+ * @param b The exponent negative integer.
136
+ * @returns `a ** b` clamped to the negative integer range.
137
+ */
138
+ pow,
139
+
140
+ /**
141
+ * Adds two negative integers.
142
+ *
143
+ * @param a The first negative integer.
144
+ * @param b The second negative integer.
145
+ * @returns `a + b` as a NegativeInt.
146
+ */
147
+ add,
148
+
149
+ /**
150
+ * Subtracts one negative integer from another.
151
+ *
152
+ * The mathematical result can be positive, so it is clamped to the negative
153
+ * integer range (maximum `-1`).
154
+ *
155
+ * @param a The minuend negative integer.
156
+ * @param b The subtrahend negative integer.
157
+ * @returns `a - b` clamped to the negative integer range.
158
+ */
159
+ sub,
160
+ } as const;
@@ -0,0 +1,146 @@
1
+ import { type PositiveInt } from 'ts-type-forge';
2
+ import { expectType } from '../../expect-type.mjs';
3
+ import { range } from '../../iterator/index.mjs';
4
+ import { asNegativeInt, isNegativeInt, NegativeInt } from './negative-int.mjs';
5
+
6
+ describe('NegativeInt test', () => {
7
+ describe(asNegativeInt, () => {
8
+ test('accepts valid negative integers', () => {
9
+ expect(() => asNegativeInt(-1)).not.toThrow();
10
+
11
+ expect(() => asNegativeInt(-1000)).not.toThrow();
12
+
13
+ expect(() => asNegativeInt(Number.MIN_SAFE_INTEGER)).not.toThrow();
14
+ });
15
+
16
+ test('rejects zero and positive integers', () => {
17
+ expect(() => asNegativeInt(0)).toThrow(TypeError);
18
+
19
+ expect(() => asNegativeInt(1)).toThrow(TypeError);
20
+
21
+ expect(() => asNegativeInt(Number.MAX_SAFE_INTEGER)).toThrow(TypeError);
22
+ });
23
+
24
+ test('rejects non-integers', () => {
25
+ expect(() => asNegativeInt(Number.NaN)).toThrow(TypeError);
26
+
27
+ expect(() => asNegativeInt(Number.NEGATIVE_INFINITY)).toThrow(TypeError);
28
+
29
+ expect(() => asNegativeInt(-1.5)).toThrow(TypeError);
30
+ });
31
+
32
+ test('returns the same value for valid inputs', () => {
33
+ expect(asNegativeInt(-5)).toBe(-5);
34
+
35
+ expect(asNegativeInt(Number.MIN_SAFE_INTEGER)).toBe(
36
+ Number.MIN_SAFE_INTEGER,
37
+ );
38
+ });
39
+ });
40
+
41
+ describe(isNegativeInt, () => {
42
+ test('correctly identifies negative integers', () => {
43
+ assert.isTrue(isNegativeInt(-1));
44
+
45
+ assert.isTrue(isNegativeInt(-1000));
46
+
47
+ assert.isFalse(isNegativeInt(0));
48
+
49
+ assert.isFalse(isNegativeInt(1));
50
+
51
+ assert.isFalse(isNegativeInt(-1.5));
52
+ });
53
+ });
54
+
55
+ describe('NegativeInt.is', () => {
56
+ test('works as a type guard', () => {
57
+ const value: number = -42;
58
+
59
+ if (NegativeInt.is(value)) {
60
+ expectType<typeof value, typeof value & NegativeInt>('=');
61
+ }
62
+ });
63
+ });
64
+
65
+ describe('NegativeInt.MAX_VALUE', () => {
66
+ test('is -1', () => {
67
+ expect(NegativeInt.MAX_VALUE).toBe(-1);
68
+ });
69
+ });
70
+
71
+ describe('NegativeInt.abs', () => {
72
+ test('returns the absolute value as a positive integer', () => {
73
+ expect(NegativeInt.abs(asNegativeInt(-5))).toBe(5);
74
+
75
+ expect(NegativeInt.abs(asNegativeInt(-1))).toBe(1);
76
+ });
77
+
78
+ test('result type is PositiveInt', () => {
79
+ expectType<ReturnType<typeof NegativeInt.abs>, PositiveInt>('=');
80
+ });
81
+ });
82
+
83
+ describe('NegativeInt.min', () => {
84
+ test('returns the smaller value', () => {
85
+ expect(NegativeInt.min(-5, -10)).toBe(-10);
86
+
87
+ expect(NegativeInt.min(-1, -2)).toBe(-2);
88
+ });
89
+ });
90
+
91
+ describe('NegativeInt.max', () => {
92
+ test('returns the larger value', () => {
93
+ expect(NegativeInt.max(-5, -10)).toBe(-5);
94
+
95
+ expect(NegativeInt.max(-1, -2)).toBe(-1);
96
+ });
97
+ });
98
+
99
+ describe('NegativeInt.clamp', () => {
100
+ test('returns the same value if within range', () => {
101
+ expect(NegativeInt.clamp(-5)).toBe(-5);
102
+
103
+ expect(NegativeInt.clamp(-1000)).toBe(-1000);
104
+ });
105
+
106
+ test('clamps non-negative values to MAX_VALUE (-1)', () => {
107
+ expect(NegativeInt.clamp(0)).toBe(-1);
108
+
109
+ expect(NegativeInt.clamp(5)).toBe(-1);
110
+ });
111
+ });
112
+
113
+ describe('NegativeInt.add', () => {
114
+ test('adds two negative integers', () => {
115
+ expect(NegativeInt.add(-5, -3)).toBe(-8);
116
+
117
+ expect(NegativeInt.add(-1, -1)).toBe(-2);
118
+ });
119
+ });
120
+
121
+ describe('NegativeInt.sub', () => {
122
+ test('subtracts two negative integers', () => {
123
+ expect(NegativeInt.sub(-10, -3)).toBe(-7);
124
+ });
125
+
126
+ test('clamps result to MAX_VALUE (-1) when result is non-negative', () => {
127
+ expect(NegativeInt.sub(-3, -10)).toBe(-1);
128
+ });
129
+ });
130
+
131
+ describe('NegativeInt.pow', () => {
132
+ test('raises to power', () => {
133
+ expect(NegativeInt.pow(-2, -1)).toBeDefined();
134
+ });
135
+ });
136
+
137
+ describe('NegativeInt.random', () => {
138
+ test('generates values within range', () => {
139
+ for (const _ of range(100)) {
140
+ const value = NegativeInt.random();
141
+
142
+ assert.isTrue(isNegativeInt(value));
143
+ }
144
+ });
145
+ });
146
+ });
@@ -0,0 +1,173 @@
1
+ import {
2
+ type NegativeSafeInt as TtfImported_NegativeSafeInt,
3
+ type WithSmallInt,
4
+ } from 'ts-type-forge';
5
+ import { TsDataForgeInternals } from '../refined-number-utils.mjs';
6
+ import { PositiveSafeInt } from './positive-safe-int.mjs';
7
+
8
+ export type NegativeSafeInt = TtfImported_NegativeSafeInt;
9
+
10
+ type ElementType = NegativeSafeInt;
11
+
12
+ const typeNameInMessage = 'a negative safe integer';
13
+
14
+ const {
15
+ MAX_VALUE,
16
+ MIN_VALUE,
17
+ add,
18
+ castType,
19
+ clamp,
20
+ is,
21
+ max: max_,
22
+ min: min_,
23
+ pow,
24
+ random,
25
+ sub,
26
+ } = TsDataForgeInternals.RefinedNumberUtils.operatorsForInteger<
27
+ ElementType,
28
+ number,
29
+ -1
30
+ >({
31
+ integerOrSafeInteger: 'SafeInteger',
32
+ nonZero: true,
33
+ MIN_VALUE: Number.MIN_SAFE_INTEGER,
34
+ MAX_VALUE: -1,
35
+ typeNameInMessage,
36
+ } as const);
37
+
38
+ /**
39
+ * Returns the absolute value of a negative safe integer.
40
+ *
41
+ * The absolute value of a negative safe integer is always a positive safe
42
+ * integer (`>= 1`), so the result is typed as a {@link PositiveSafeInt}.
43
+ *
44
+ * @param x - The negative safe integer.
45
+ * @returns `|x|` as a PositiveSafeInt.
46
+ */
47
+ const abs = (x: WithSmallInt<ElementType>): PositiveSafeInt =>
48
+ PositiveSafeInt.clamp(Math.abs(x));
49
+
50
+ /**
51
+ * Checks if a number is a NegativeSafeInt (a negative safe integer in the range
52
+ * `[Number.MIN_SAFE_INTEGER, -1]`).
53
+ *
54
+ * @param value The value to check.
55
+ * @returns `true` if the value is a NegativeSafeInt, `false` otherwise.
56
+ */
57
+ export const isNegativeSafeInt = is;
58
+
59
+ /**
60
+ * Casts a number to a NegativeSafeInt type.
61
+ *
62
+ * @param value The value to cast.
63
+ * @returns The value as a NegativeSafeInt type.
64
+ * @throws {TypeError} If the value is not a negative safe integer.
65
+ */
66
+ export const asNegativeSafeInt = castType;
67
+
68
+ /**
69
+ * Namespace providing type-safe arithmetic operations for negative safe
70
+ * integers.
71
+ *
72
+ * NegativeSafeInt represents safe integers that are strictly less than zero
73
+ * (`[Number.MIN_SAFE_INTEGER, -1]`). Operations that stay within the negative
74
+ * safe integers clamp their results to the valid range, while operations whose
75
+ * result leaves the set (`mul`, `div`, `abs`) are typed to reflect the actual
76
+ * sign of the result.
77
+ */
78
+ export const NegativeSafeInt = {
79
+ /**
80
+ * Type guard to check if a value is a NegativeSafeInt.
81
+ *
82
+ * @param value The value to check.
83
+ * @returns `true` if the value is a negative safe integer, `false` otherwise.
84
+ */
85
+ is,
86
+
87
+ /**
88
+ * The minimum value for a negative safe integer.
89
+ *
90
+ * @readonly
91
+ */
92
+ MIN_VALUE,
93
+
94
+ /**
95
+ * The maximum value for a negative safe integer.
96
+ *
97
+ * @readonly
98
+ */
99
+ MAX_VALUE,
100
+
101
+ /**
102
+ * Returns the absolute value of a negative safe integer.
103
+ *
104
+ * @param a The negative safe integer.
105
+ * @returns The absolute value as a PositiveSafeInt.
106
+ */
107
+ abs,
108
+
109
+ /**
110
+ * Returns the smaller of two negative safe integers.
111
+ *
112
+ * @param a The first negative safe integer.
113
+ * @param b The second negative safe integer.
114
+ * @returns The minimum value as a NegativeSafeInt.
115
+ */
116
+ min: min_,
117
+
118
+ /**
119
+ * Returns the larger of two negative safe integers.
120
+ *
121
+ * @param a The first negative safe integer.
122
+ * @param b The second negative safe integer.
123
+ * @returns The maximum value as a NegativeSafeInt.
124
+ */
125
+ max: max_,
126
+
127
+ /**
128
+ * Clamps a number to the NegativeSafeInt range.
129
+ *
130
+ * @param value The number to clamp.
131
+ * @returns The value clamped to `[Number.MIN_SAFE_INTEGER, -1]` as a
132
+ * NegativeSafeInt.
133
+ */
134
+ clamp,
135
+
136
+ /**
137
+ * Generates a random NegativeSafeInt value within the valid range.
138
+ *
139
+ * @returns A random NegativeSafeInt between Number.MIN_SAFE_INTEGER and -1.
140
+ */
141
+ random,
142
+
143
+ /**
144
+ * Raises a negative safe integer to the power of another negative safe
145
+ * integer.
146
+ *
147
+ * @param a The base negative safe integer.
148
+ * @param b The exponent negative safe integer.
149
+ * @returns `a ** b` clamped to the NegativeSafeInt range.
150
+ */
151
+ pow,
152
+
153
+ /**
154
+ * Adds two negative safe integers.
155
+ *
156
+ * @param a The first negative safe integer.
157
+ * @param b The second negative safe integer.
158
+ * @returns `a + b` clamped to the NegativeSafeInt range.
159
+ */
160
+ add,
161
+
162
+ /**
163
+ * Subtracts one negative safe integer from another.
164
+ *
165
+ * The mathematical result can be positive, so it is clamped to the
166
+ * NegativeSafeInt range (maximum `-1`).
167
+ *
168
+ * @param a The minuend negative safe integer.
169
+ * @param b The subtrahend negative safe integer.
170
+ * @returns `a - b` clamped to the NegativeSafeInt range.
171
+ */
172
+ sub,
173
+ } as const;
@@ -0,0 +1,120 @@
1
+ import { type PositiveSafeInt } from 'ts-type-forge';
2
+ import { expectType } from '../../expect-type.mjs';
3
+ import { range } from '../../iterator/index.mjs';
4
+ import {
5
+ asNegativeSafeInt,
6
+ isNegativeSafeInt,
7
+ NegativeSafeInt,
8
+ } from './negative-safe-int.mjs';
9
+
10
+ describe('NegativeSafeInt test', () => {
11
+ describe(asNegativeSafeInt, () => {
12
+ test('accepts valid negative safe integers', () => {
13
+ expect(() => asNegativeSafeInt(-1)).not.toThrow();
14
+
15
+ expect(() => asNegativeSafeInt(-1000)).not.toThrow();
16
+
17
+ expect(() => asNegativeSafeInt(Number.MIN_SAFE_INTEGER)).not.toThrow();
18
+ });
19
+
20
+ test('rejects zero and positive integers', () => {
21
+ expect(() => asNegativeSafeInt(0)).toThrow(TypeError);
22
+
23
+ expect(() => asNegativeSafeInt(1)).toThrow(TypeError);
24
+ });
25
+
26
+ test('rejects unsafe and non-integers', () => {
27
+ expect(() => asNegativeSafeInt(Number.MIN_SAFE_INTEGER - 1)).toThrow(
28
+ TypeError,
29
+ );
30
+
31
+ expect(() => asNegativeSafeInt(Number.NEGATIVE_INFINITY)).toThrow(
32
+ TypeError,
33
+ );
34
+
35
+ expect(() => asNegativeSafeInt(-1.5)).toThrow(TypeError);
36
+ });
37
+ });
38
+
39
+ describe(isNegativeSafeInt, () => {
40
+ test('correctly identifies negative safe integers', () => {
41
+ assert.isTrue(isNegativeSafeInt(-1));
42
+
43
+ assert.isTrue(isNegativeSafeInt(Number.MIN_SAFE_INTEGER));
44
+
45
+ assert.isFalse(isNegativeSafeInt(0));
46
+
47
+ assert.isFalse(isNegativeSafeInt(1));
48
+
49
+ assert.isFalse(isNegativeSafeInt(Number.MIN_SAFE_INTEGER - 1));
50
+ });
51
+ });
52
+
53
+ describe('NegativeSafeInt.is', () => {
54
+ test('works as a type guard', () => {
55
+ const value: number = -42;
56
+
57
+ if (NegativeSafeInt.is(value)) {
58
+ expectType<typeof value, typeof value & NegativeSafeInt>('=');
59
+ }
60
+ });
61
+ });
62
+
63
+ describe('NegativeSafeInt.MAX_VALUE / MIN_VALUE', () => {
64
+ test('has the expected bounds', () => {
65
+ expect(NegativeSafeInt.MAX_VALUE).toBe(-1);
66
+
67
+ expect(NegativeSafeInt.MIN_VALUE).toBe(Number.MIN_SAFE_INTEGER);
68
+ });
69
+ });
70
+
71
+ describe('NegativeSafeInt.abs', () => {
72
+ test('returns the absolute value as a positive safe integer', () => {
73
+ expect(NegativeSafeInt.abs(asNegativeSafeInt(-5))).toBe(5);
74
+ });
75
+
76
+ test('result type is PositiveSafeInt', () => {
77
+ expectType<ReturnType<typeof NegativeSafeInt.abs>, PositiveSafeInt>('=');
78
+ });
79
+ });
80
+
81
+ describe('NegativeSafeInt.min / max', () => {
82
+ test('returns the expected extremum', () => {
83
+ expect(NegativeSafeInt.min(-5, -10)).toBe(-10);
84
+
85
+ expect(NegativeSafeInt.max(-5, -10)).toBe(-5);
86
+ });
87
+ });
88
+
89
+ describe('NegativeSafeInt.clamp', () => {
90
+ test('clamps to the valid range', () => {
91
+ expect(NegativeSafeInt.clamp(-5)).toBe(-5);
92
+
93
+ expect(NegativeSafeInt.clamp(0)).toBe(-1);
94
+
95
+ expect(NegativeSafeInt.clamp(Number.MIN_SAFE_INTEGER - 100)).toBe(
96
+ Number.MIN_SAFE_INTEGER,
97
+ );
98
+ });
99
+ });
100
+
101
+ describe('NegativeSafeInt.add / sub', () => {
102
+ test('adds and subtracts within the range', () => {
103
+ expect(NegativeSafeInt.add(-5, -3)).toBe(-8);
104
+
105
+ expect(NegativeSafeInt.sub(-10, -3)).toBe(-7);
106
+
107
+ expect(NegativeSafeInt.sub(-3, -10)).toBe(-1);
108
+ });
109
+ });
110
+
111
+ describe('NegativeSafeInt.random', () => {
112
+ test('generates values within range', () => {
113
+ for (const _ of range(100)) {
114
+ const value = NegativeSafeInt.random();
115
+
116
+ assert.isTrue(isNegativeSafeInt(value));
117
+ }
118
+ });
119
+ });
120
+ });