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.
- package/LICENSE +201 -0
- package/README.md +534 -0
- package/package.json +101 -0
- package/src/array/array-utils-creation.test.mts +443 -0
- package/src/array/array-utils-modification.test.mts +197 -0
- package/src/array/array-utils-overload-type-error.test.mts +149 -0
- package/src/array/array-utils-reducing-value.test.mts +425 -0
- package/src/array/array-utils-search.test.mts +169 -0
- package/src/array/array-utils-set-op.test.mts +335 -0
- package/src/array/array-utils-slice-clamped.test.mts +113 -0
- package/src/array/array-utils-slicing.test.mts +316 -0
- package/src/array/array-utils-transformation.test.mts +790 -0
- package/src/array/array-utils-validation.test.mts +492 -0
- package/src/array/array-utils.mts +4000 -0
- package/src/array/array.test.mts +146 -0
- package/src/array/index.mts +2 -0
- package/src/array/tuple-utils.mts +519 -0
- package/src/array/tuple-utils.test.mts +518 -0
- package/src/collections/imap-mapped.mts +801 -0
- package/src/collections/imap-mapped.test.mts +860 -0
- package/src/collections/imap.mts +651 -0
- package/src/collections/imap.test.mts +932 -0
- package/src/collections/index.mts +6 -0
- package/src/collections/iset-mapped.mts +889 -0
- package/src/collections/iset-mapped.test.mts +1187 -0
- package/src/collections/iset.mts +682 -0
- package/src/collections/iset.test.mts +1084 -0
- package/src/collections/queue.mts +390 -0
- package/src/collections/queue.test.mts +282 -0
- package/src/collections/stack.mts +423 -0
- package/src/collections/stack.test.mts +225 -0
- package/src/expect-type.mts +206 -0
- package/src/functional/index.mts +4 -0
- package/src/functional/match.mts +300 -0
- package/src/functional/match.test.mts +177 -0
- package/src/functional/optional.mts +733 -0
- package/src/functional/optional.test.mts +619 -0
- package/src/functional/pipe.mts +212 -0
- package/src/functional/pipe.test.mts +85 -0
- package/src/functional/result.mts +1134 -0
- package/src/functional/result.test.mts +777 -0
- package/src/globals.d.mts +38 -0
- package/src/guard/has-key.mts +119 -0
- package/src/guard/has-key.test.mts +219 -0
- package/src/guard/index.mts +7 -0
- package/src/guard/is-non-empty-string.mts +108 -0
- package/src/guard/is-non-empty-string.test.mts +91 -0
- package/src/guard/is-non-null-object.mts +106 -0
- package/src/guard/is-non-null-object.test.mts +90 -0
- package/src/guard/is-primitive.mts +165 -0
- package/src/guard/is-primitive.test.mts +102 -0
- package/src/guard/is-record.mts +153 -0
- package/src/guard/is-record.test.mts +112 -0
- package/src/guard/is-type.mts +450 -0
- package/src/guard/is-type.test.mts +496 -0
- package/src/guard/key-is-in.mts +163 -0
- package/src/guard/key-is-in.test.mts +19 -0
- package/src/index.mts +10 -0
- package/src/iterator/index.mts +1 -0
- package/src/iterator/range.mts +120 -0
- package/src/iterator/range.test.mts +33 -0
- package/src/json/index.mts +1 -0
- package/src/json/json.mts +711 -0
- package/src/json/json.test.mts +628 -0
- package/src/number/branded-types/finite-number.mts +354 -0
- package/src/number/branded-types/finite-number.test.mts +135 -0
- package/src/number/branded-types/index.mts +26 -0
- package/src/number/branded-types/int.mts +278 -0
- package/src/number/branded-types/int.test.mts +140 -0
- package/src/number/branded-types/int16.mts +192 -0
- package/src/number/branded-types/int16.test.mts +170 -0
- package/src/number/branded-types/int32.mts +193 -0
- package/src/number/branded-types/int32.test.mts +170 -0
- package/src/number/branded-types/non-negative-finite-number.mts +223 -0
- package/src/number/branded-types/non-negative-finite-number.test.mts +188 -0
- package/src/number/branded-types/non-negative-int16.mts +187 -0
- package/src/number/branded-types/non-negative-int16.test.mts +201 -0
- package/src/number/branded-types/non-negative-int32.mts +187 -0
- package/src/number/branded-types/non-negative-int32.test.mts +204 -0
- package/src/number/branded-types/non-zero-finite-number.mts +229 -0
- package/src/number/branded-types/non-zero-finite-number.test.mts +198 -0
- package/src/number/branded-types/non-zero-int.mts +167 -0
- package/src/number/branded-types/non-zero-int.test.mts +177 -0
- package/src/number/branded-types/non-zero-int16.mts +196 -0
- package/src/number/branded-types/non-zero-int16.test.mts +195 -0
- package/src/number/branded-types/non-zero-int32.mts +196 -0
- package/src/number/branded-types/non-zero-int32.test.mts +197 -0
- package/src/number/branded-types/non-zero-safe-int.mts +196 -0
- package/src/number/branded-types/non-zero-safe-int.test.mts +232 -0
- package/src/number/branded-types/non-zero-uint16.mts +189 -0
- package/src/number/branded-types/non-zero-uint16.test.mts +199 -0
- package/src/number/branded-types/non-zero-uint32.mts +189 -0
- package/src/number/branded-types/non-zero-uint32.test.mts +199 -0
- package/src/number/branded-types/positive-finite-number.mts +241 -0
- package/src/number/branded-types/positive-finite-number.test.mts +204 -0
- package/src/number/branded-types/positive-int.mts +304 -0
- package/src/number/branded-types/positive-int.test.mts +176 -0
- package/src/number/branded-types/positive-int16.mts +188 -0
- package/src/number/branded-types/positive-int16.test.mts +197 -0
- package/src/number/branded-types/positive-int32.mts +188 -0
- package/src/number/branded-types/positive-int32.test.mts +197 -0
- package/src/number/branded-types/positive-safe-int.mts +187 -0
- package/src/number/branded-types/positive-safe-int.test.mts +210 -0
- package/src/number/branded-types/positive-uint16.mts +188 -0
- package/src/number/branded-types/positive-uint16.test.mts +203 -0
- package/src/number/branded-types/positive-uint32.mts +188 -0
- package/src/number/branded-types/positive-uint32.test.mts +203 -0
- package/src/number/branded-types/safe-int.mts +291 -0
- package/src/number/branded-types/safe-int.test.mts +170 -0
- package/src/number/branded-types/safe-uint.mts +187 -0
- package/src/number/branded-types/safe-uint.test.mts +176 -0
- package/src/number/branded-types/uint.mts +179 -0
- package/src/number/branded-types/uint.test.mts +158 -0
- package/src/number/branded-types/uint16.mts +186 -0
- package/src/number/branded-types/uint16.test.mts +170 -0
- package/src/number/branded-types/uint32.mts +218 -0
- package/src/number/branded-types/uint32.test.mts +170 -0
- package/src/number/enum/index.mts +2 -0
- package/src/number/enum/int8.mts +344 -0
- package/src/number/enum/int8.test.mts +180 -0
- package/src/number/enum/uint8.mts +293 -0
- package/src/number/enum/uint8.test.mts +164 -0
- package/src/number/index.mts +4 -0
- package/src/number/num.mts +604 -0
- package/src/number/num.test.mts +242 -0
- package/src/number/refined-number-utils.mts +566 -0
- package/src/object/index.mts +1 -0
- package/src/object/object.mts +447 -0
- package/src/object/object.test.mts +124 -0
- package/src/others/cast-mutable.mts +113 -0
- package/src/others/cast-readonly.mts +192 -0
- package/src/others/cast-readonly.test.mts +89 -0
- package/src/others/if-then.mts +98 -0
- package/src/others/if-then.test.mts +75 -0
- package/src/others/index.mts +7 -0
- package/src/others/map-nullable.mts +172 -0
- package/src/others/map-nullable.test.mts +297 -0
- package/src/others/memoize-function.mts +196 -0
- package/src/others/memoize-function.test.mts +168 -0
- package/src/others/tuple.mts +160 -0
- package/src/others/tuple.test.mts +11 -0
- package/src/others/unknown-to-string.mts +215 -0
- 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';
|