ts-data-forge 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +534 -0
  3. package/package.json +101 -0
  4. package/src/array/array-utils-creation.test.mts +443 -0
  5. package/src/array/array-utils-modification.test.mts +197 -0
  6. package/src/array/array-utils-overload-type-error.test.mts +149 -0
  7. package/src/array/array-utils-reducing-value.test.mts +425 -0
  8. package/src/array/array-utils-search.test.mts +169 -0
  9. package/src/array/array-utils-set-op.test.mts +335 -0
  10. package/src/array/array-utils-slice-clamped.test.mts +113 -0
  11. package/src/array/array-utils-slicing.test.mts +316 -0
  12. package/src/array/array-utils-transformation.test.mts +790 -0
  13. package/src/array/array-utils-validation.test.mts +492 -0
  14. package/src/array/array-utils.mts +4000 -0
  15. package/src/array/array.test.mts +146 -0
  16. package/src/array/index.mts +2 -0
  17. package/src/array/tuple-utils.mts +519 -0
  18. package/src/array/tuple-utils.test.mts +518 -0
  19. package/src/collections/imap-mapped.mts +801 -0
  20. package/src/collections/imap-mapped.test.mts +860 -0
  21. package/src/collections/imap.mts +651 -0
  22. package/src/collections/imap.test.mts +932 -0
  23. package/src/collections/index.mts +6 -0
  24. package/src/collections/iset-mapped.mts +889 -0
  25. package/src/collections/iset-mapped.test.mts +1187 -0
  26. package/src/collections/iset.mts +682 -0
  27. package/src/collections/iset.test.mts +1084 -0
  28. package/src/collections/queue.mts +390 -0
  29. package/src/collections/queue.test.mts +282 -0
  30. package/src/collections/stack.mts +423 -0
  31. package/src/collections/stack.test.mts +225 -0
  32. package/src/expect-type.mts +206 -0
  33. package/src/functional/index.mts +4 -0
  34. package/src/functional/match.mts +300 -0
  35. package/src/functional/match.test.mts +177 -0
  36. package/src/functional/optional.mts +733 -0
  37. package/src/functional/optional.test.mts +619 -0
  38. package/src/functional/pipe.mts +212 -0
  39. package/src/functional/pipe.test.mts +85 -0
  40. package/src/functional/result.mts +1134 -0
  41. package/src/functional/result.test.mts +777 -0
  42. package/src/globals.d.mts +38 -0
  43. package/src/guard/has-key.mts +119 -0
  44. package/src/guard/has-key.test.mts +219 -0
  45. package/src/guard/index.mts +7 -0
  46. package/src/guard/is-non-empty-string.mts +108 -0
  47. package/src/guard/is-non-empty-string.test.mts +91 -0
  48. package/src/guard/is-non-null-object.mts +106 -0
  49. package/src/guard/is-non-null-object.test.mts +90 -0
  50. package/src/guard/is-primitive.mts +165 -0
  51. package/src/guard/is-primitive.test.mts +102 -0
  52. package/src/guard/is-record.mts +153 -0
  53. package/src/guard/is-record.test.mts +112 -0
  54. package/src/guard/is-type.mts +450 -0
  55. package/src/guard/is-type.test.mts +496 -0
  56. package/src/guard/key-is-in.mts +163 -0
  57. package/src/guard/key-is-in.test.mts +19 -0
  58. package/src/index.mts +10 -0
  59. package/src/iterator/index.mts +1 -0
  60. package/src/iterator/range.mts +120 -0
  61. package/src/iterator/range.test.mts +33 -0
  62. package/src/json/index.mts +1 -0
  63. package/src/json/json.mts +711 -0
  64. package/src/json/json.test.mts +628 -0
  65. package/src/number/branded-types/finite-number.mts +354 -0
  66. package/src/number/branded-types/finite-number.test.mts +135 -0
  67. package/src/number/branded-types/index.mts +26 -0
  68. package/src/number/branded-types/int.mts +278 -0
  69. package/src/number/branded-types/int.test.mts +140 -0
  70. package/src/number/branded-types/int16.mts +192 -0
  71. package/src/number/branded-types/int16.test.mts +170 -0
  72. package/src/number/branded-types/int32.mts +193 -0
  73. package/src/number/branded-types/int32.test.mts +170 -0
  74. package/src/number/branded-types/non-negative-finite-number.mts +223 -0
  75. package/src/number/branded-types/non-negative-finite-number.test.mts +188 -0
  76. package/src/number/branded-types/non-negative-int16.mts +187 -0
  77. package/src/number/branded-types/non-negative-int16.test.mts +201 -0
  78. package/src/number/branded-types/non-negative-int32.mts +187 -0
  79. package/src/number/branded-types/non-negative-int32.test.mts +204 -0
  80. package/src/number/branded-types/non-zero-finite-number.mts +229 -0
  81. package/src/number/branded-types/non-zero-finite-number.test.mts +198 -0
  82. package/src/number/branded-types/non-zero-int.mts +167 -0
  83. package/src/number/branded-types/non-zero-int.test.mts +177 -0
  84. package/src/number/branded-types/non-zero-int16.mts +196 -0
  85. package/src/number/branded-types/non-zero-int16.test.mts +195 -0
  86. package/src/number/branded-types/non-zero-int32.mts +196 -0
  87. package/src/number/branded-types/non-zero-int32.test.mts +197 -0
  88. package/src/number/branded-types/non-zero-safe-int.mts +196 -0
  89. package/src/number/branded-types/non-zero-safe-int.test.mts +232 -0
  90. package/src/number/branded-types/non-zero-uint16.mts +189 -0
  91. package/src/number/branded-types/non-zero-uint16.test.mts +199 -0
  92. package/src/number/branded-types/non-zero-uint32.mts +189 -0
  93. package/src/number/branded-types/non-zero-uint32.test.mts +199 -0
  94. package/src/number/branded-types/positive-finite-number.mts +241 -0
  95. package/src/number/branded-types/positive-finite-number.test.mts +204 -0
  96. package/src/number/branded-types/positive-int.mts +304 -0
  97. package/src/number/branded-types/positive-int.test.mts +176 -0
  98. package/src/number/branded-types/positive-int16.mts +188 -0
  99. package/src/number/branded-types/positive-int16.test.mts +197 -0
  100. package/src/number/branded-types/positive-int32.mts +188 -0
  101. package/src/number/branded-types/positive-int32.test.mts +197 -0
  102. package/src/number/branded-types/positive-safe-int.mts +187 -0
  103. package/src/number/branded-types/positive-safe-int.test.mts +210 -0
  104. package/src/number/branded-types/positive-uint16.mts +188 -0
  105. package/src/number/branded-types/positive-uint16.test.mts +203 -0
  106. package/src/number/branded-types/positive-uint32.mts +188 -0
  107. package/src/number/branded-types/positive-uint32.test.mts +203 -0
  108. package/src/number/branded-types/safe-int.mts +291 -0
  109. package/src/number/branded-types/safe-int.test.mts +170 -0
  110. package/src/number/branded-types/safe-uint.mts +187 -0
  111. package/src/number/branded-types/safe-uint.test.mts +176 -0
  112. package/src/number/branded-types/uint.mts +179 -0
  113. package/src/number/branded-types/uint.test.mts +158 -0
  114. package/src/number/branded-types/uint16.mts +186 -0
  115. package/src/number/branded-types/uint16.test.mts +170 -0
  116. package/src/number/branded-types/uint32.mts +218 -0
  117. package/src/number/branded-types/uint32.test.mts +170 -0
  118. package/src/number/enum/index.mts +2 -0
  119. package/src/number/enum/int8.mts +344 -0
  120. package/src/number/enum/int8.test.mts +180 -0
  121. package/src/number/enum/uint8.mts +293 -0
  122. package/src/number/enum/uint8.test.mts +164 -0
  123. package/src/number/index.mts +4 -0
  124. package/src/number/num.mts +604 -0
  125. package/src/number/num.test.mts +242 -0
  126. package/src/number/refined-number-utils.mts +566 -0
  127. package/src/object/index.mts +1 -0
  128. package/src/object/object.mts +447 -0
  129. package/src/object/object.test.mts +124 -0
  130. package/src/others/cast-mutable.mts +113 -0
  131. package/src/others/cast-readonly.mts +192 -0
  132. package/src/others/cast-readonly.test.mts +89 -0
  133. package/src/others/if-then.mts +98 -0
  134. package/src/others/if-then.test.mts +75 -0
  135. package/src/others/index.mts +7 -0
  136. package/src/others/map-nullable.mts +172 -0
  137. package/src/others/map-nullable.test.mts +297 -0
  138. package/src/others/memoize-function.mts +196 -0
  139. package/src/others/memoize-function.test.mts +168 -0
  140. package/src/others/tuple.mts +160 -0
  141. package/src/others/tuple.test.mts +11 -0
  142. package/src/others/unknown-to-string.mts +215 -0
  143. package/src/others/unknown-to-string.test.mts +114 -0
@@ -0,0 +1,120 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-type-assertion, func-names */
2
+ import { SafeInt, asSafeInt } from '../number/index.mjs';
3
+
4
+ /**
5
+ * Generates a sequence of numbers within a specified range using a generator function.
6
+ *
7
+ * This function creates a generator that yields numbers from `start` (inclusive) to `end` (exclusive)
8
+ * with the specified `step` increment/decrement. The function implements the JavaScript iterator protocol,
9
+ * making it compatible with for-of loops, spread operator, Array.from(), and other iterator consumers.
10
+ *
11
+ * The function has two overloaded signatures:
12
+ * 1. For non-negative ranges: accepts SafeUint parameters and optional positive step
13
+ * 2. For general ranges: accepts SafeInt parameters and optional non-zero step
14
+ *
15
+ * **Generator Behavior:**
16
+ * - Yields values lazily (computed on-demand)
17
+ * - Returns `void` when iteration completes
18
+ * - Does not accept sent values (next parameter is ignored)
19
+ * - Automatically handles direction based on step sign and start/end relationship
20
+ *
21
+ * **Step Parameter Behavior:**
22
+ * - Positive step: iterates from start toward end (start < end expected)
23
+ * - Negative step: iterates from start toward end (start > end expected)
24
+ * - Zero step: not allowed (NonZeroSafeInt constraint)
25
+ * - Default step: 1 (positive direction)
26
+ *
27
+ * **Edge Cases:**
28
+ * - When start equals end: yields no values (empty sequence)
29
+ * - When step direction conflicts with start/end relationship: yields no values
30
+ * - All parameters must be safe integers (within JavaScript's safe integer range)
31
+ *
32
+ * @template T - The specific SafeInt or SafeUint type being generated
33
+ * @param start - The starting number of the sequence (inclusive). Must be a safe integer.
34
+ * @param end - The end number of the sequence (exclusive). Must be a safe integer.
35
+ * @param step - The increment or decrement value. Defaults to 1. Must be non-zero safe integer.
36
+ * @returns A Generator object that yields safe integers in the specified range.
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * // Basic ascending range
41
+ * for (const n of range(0, 5)) {
42
+ * console.log(n); // Outputs: 0, 1, 2, 3, 4
43
+ * }
44
+ *
45
+ * // Range with custom step
46
+ * for (const n of range(0, 10, 2)) {
47
+ * console.log(n); // Outputs: 0, 2, 4, 6, 8
48
+ * }
49
+ *
50
+ * // Descending range with negative step
51
+ * for (const n of range(10, 0, -1)) {
52
+ * console.log(n); // Outputs: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
53
+ * }
54
+ *
55
+ * // Negative numbers with negative step
56
+ * for (const n of range(0, -10, -1)) {
57
+ * console.log(n); // Outputs: 0, -1, -2, -3, -4, -5, -6, -7, -8, -9
58
+ * }
59
+ *
60
+ * // Convert generator to array
61
+ * const numbers = Array.from(range(1, 4)); // [1, 2, 3]
62
+ * const evens = [...range(0, 11, 2)]; // [0, 2, 4, 6, 8, 10]
63
+ *
64
+ * // Empty ranges
65
+ * Array.from(range(5, 5)); // [] (start equals end)
66
+ * Array.from(range(5, 3)); // [] (positive step, start > end)
67
+ * Array.from(range(3, 5, -1)); // [] (negative step, start < end)
68
+ *
69
+ * // Using with iterator protocol manually
70
+ * const gen = range(1, 4);
71
+ * console.log(gen.next()); // { value: 1, done: false }
72
+ * console.log(gen.next()); // { value: 2, done: false }
73
+ * console.log(gen.next()); // { value: 3, done: false }
74
+ * console.log(gen.next()); // { value: undefined, done: true }
75
+ *
76
+ * // Practical usage patterns
77
+ * // Create index sequences
78
+ * const indices = Array.from(range(0, items.length));
79
+ *
80
+ * // Generate test data
81
+ * const testIds = [...range(1, 101)]; // [1, 2, ..., 100]
82
+ *
83
+ * // Iterate with step intervals
84
+ * for (const minute of range(0, 60, 5)) {
85
+ * scheduleTask(minute); // Every 5 minutes
86
+ * }
87
+ *
88
+ * // Countdown sequences
89
+ * for (const count of range(10, 0, -1)) {
90
+ * console.log(`T-minus ${count}`);
91
+ * }
92
+ * ```
93
+ */
94
+ export const range: RangeFnOverload = function* (
95
+ start: SafeIntWithSmallInt,
96
+ end: SafeIntWithSmallInt,
97
+ step: NonZeroSafeIntWithSmallInt = 1,
98
+ ): Generator<SafeInt, void, unknown> {
99
+ for (
100
+ let mut_i: SafeInt = asSafeInt(start);
101
+ step > 0 ? mut_i < end : mut_i > end;
102
+ mut_i = SafeInt.add(mut_i, step)
103
+ ) {
104
+ yield mut_i;
105
+ }
106
+ } as RangeFnOverload;
107
+
108
+ type RangeFnOverload = {
109
+ (
110
+ start: SafeUintWithSmallInt,
111
+ end: SafeUintWithSmallInt,
112
+ step?: PositiveSafeIntWithSmallInt,
113
+ ): Generator<SafeUint, void, unknown>;
114
+
115
+ (
116
+ start: SafeIntWithSmallInt,
117
+ end: SafeIntWithSmallInt,
118
+ step?: NonZeroSafeIntWithSmallInt,
119
+ ): Generator<SafeInt, void, unknown>;
120
+ };
@@ -0,0 +1,33 @@
1
+ import { range } from './range.mjs';
2
+
3
+ describe('range', () => {
4
+ test('range(0, 10)', () => {
5
+ expect(Array.from(range(0, 10))).toStrictEqual([
6
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
7
+ ]);
8
+ });
9
+
10
+ test('range(10, 0, -1)', () => {
11
+ expect(Array.from(range(10, 0, -1))).toStrictEqual([
12
+ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
13
+ ]);
14
+ });
15
+
16
+ test('range(0, -10, -1)', () => {
17
+ expect(Array.from(range(0, -10, -1))).toStrictEqual([
18
+ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
19
+ ]);
20
+ });
21
+
22
+ test('range(0, 0)', () => {
23
+ expect(Array.from(range(0, 0))).toStrictEqual([]);
24
+ });
25
+
26
+ test('range(0, 11, 2)', () => {
27
+ expect(Array.from(range(0, 11, 2))).toStrictEqual([0, 2, 4, 6, 8, 10]);
28
+ });
29
+
30
+ test('range(1, 12, 2)', () => {
31
+ expect(Array.from(range(1, 12, 2))).toStrictEqual([1, 3, 5, 7, 9, 11]);
32
+ });
33
+ });
@@ -0,0 +1 @@
1
+ export * from './json.mjs';