@nestia/e2e 7.1.1-dev.20250714 → 7.2.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 +21 -21
- package/README.md +93 -93
- package/package.json +1 -1
- package/src/ArrayUtil.ts +322 -322
- package/src/DynamicExecutor.ts +296 -296
- package/src/GaffComparator.ts +280 -280
- package/src/RandomGenerator.ts +451 -451
- package/src/TestValidator.ts +678 -678
- package/src/index.ts +4 -4
- package/src/internal/json_equal_to.ts +35 -35
- package/src/module.ts +5 -5
package/src/ArrayUtil.ts
CHANGED
|
@@ -1,322 +1,322 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A namespace providing utility functions for array manipulation.
|
|
3
|
-
*
|
|
4
|
-
* This namespace contains utility functions for array operations including
|
|
5
|
-
* asynchronous processing, filtering, mapping, and repetition tasks implemented
|
|
6
|
-
* in functional programming style. All functions are implemented using currying
|
|
7
|
-
* to enhance reusability and composability.
|
|
8
|
-
*
|
|
9
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* // Asynchronous filtering example
|
|
13
|
-
* const numbers = [1, 2, 3, 4, 5];
|
|
14
|
-
* const evenNumbers = await ArrayUtil.asyncFilter(numbers)(
|
|
15
|
-
* async (num) => num % 2 === 0
|
|
16
|
-
* );
|
|
17
|
-
* console.log(evenNumbers); // [2, 4]
|
|
18
|
-
* ```;
|
|
19
|
-
*/
|
|
20
|
-
export namespace ArrayUtil {
|
|
21
|
-
/**
|
|
22
|
-
* Filters an array by applying an asynchronous predicate function to each
|
|
23
|
-
* element.
|
|
24
|
-
*
|
|
25
|
-
* This function is implemented in curried form, first taking an array and
|
|
26
|
-
* then a predicate function. Elements are processed sequentially, ensuring
|
|
27
|
-
* order is maintained.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* ```typescript
|
|
31
|
-
* const users = [
|
|
32
|
-
* { id: 1, name: 'Alice', active: true },
|
|
33
|
-
* { id: 2, name: 'Bob', active: false },
|
|
34
|
-
* { id: 3, name: 'Charlie', active: true }
|
|
35
|
-
* ];
|
|
36
|
-
*
|
|
37
|
-
* const activeUsers = await ArrayUtil.asyncFilter(users)(
|
|
38
|
-
* async (user) => {
|
|
39
|
-
* // Async validation logic (e.g., API call)
|
|
40
|
-
* await new Promise(resolve => setTimeout(resolve, 100));
|
|
41
|
-
* return user.active;
|
|
42
|
-
* }
|
|
43
|
-
* );
|
|
44
|
-
* console.log(activeUsers); // [{ id: 1, name: 'Alice', active: true }, { id: 3, name: 'Charlie', active: true }]
|
|
45
|
-
* ```;
|
|
46
|
-
*
|
|
47
|
-
* @template Input - The type of elements in the input array
|
|
48
|
-
* @param elements - The readonly array to filter
|
|
49
|
-
* @returns A function that takes a predicate and returns a Promise resolving
|
|
50
|
-
* to the filtered array
|
|
51
|
-
*/
|
|
52
|
-
export const asyncFilter =
|
|
53
|
-
<Input>(elements: readonly Input[]) =>
|
|
54
|
-
async (
|
|
55
|
-
pred: (
|
|
56
|
-
elem: Input,
|
|
57
|
-
index: number,
|
|
58
|
-
array: readonly Input[],
|
|
59
|
-
) => Promise<boolean>,
|
|
60
|
-
): Promise<Input[]> => {
|
|
61
|
-
const ret: Input[] = [];
|
|
62
|
-
await asyncForEach(elements)(async (elem, index, array) => {
|
|
63
|
-
const flag: boolean = await pred(elem, index, array);
|
|
64
|
-
if (flag === true) ret.push(elem);
|
|
65
|
-
});
|
|
66
|
-
return ret;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Executes an asynchronous function for each element in an array
|
|
71
|
-
* sequentially.
|
|
72
|
-
*
|
|
73
|
-
* Unlike JavaScript's native forEach, this function processes asynchronous
|
|
74
|
-
* functions sequentially and waits for all operations to complete. It
|
|
75
|
-
* performs sequential processing rather than parallel processing, making it
|
|
76
|
-
* suitable for operations where order matters.
|
|
77
|
-
*
|
|
78
|
-
* @example
|
|
79
|
-
* ```typescript
|
|
80
|
-
* const urls = ['url1', 'url2', 'url3'];
|
|
81
|
-
*
|
|
82
|
-
* await ArrayUtil.asyncForEach(urls)(async (url, index) => {
|
|
83
|
-
* console.log(`Processing ${index}: ${url}`);
|
|
84
|
-
* const data = await fetch(url);
|
|
85
|
-
* await processData(data);
|
|
86
|
-
* console.log(`Completed ${index}: ${url}`);
|
|
87
|
-
* });
|
|
88
|
-
* console.log('All URLs processed sequentially');
|
|
89
|
-
* ```
|
|
90
|
-
*
|
|
91
|
-
* @template Input - The type of elements in the input array
|
|
92
|
-
* @param elements - The readonly array to process
|
|
93
|
-
* @returns A function that takes an async closure and returns a Promise<void>
|
|
94
|
-
*/
|
|
95
|
-
export const asyncForEach =
|
|
96
|
-
<Input>(elements: readonly Input[]) =>
|
|
97
|
-
async (
|
|
98
|
-
closure: (
|
|
99
|
-
elem: Input,
|
|
100
|
-
index: number,
|
|
101
|
-
array: readonly Input[],
|
|
102
|
-
) => Promise<any>,
|
|
103
|
-
): Promise<void> => {
|
|
104
|
-
await asyncRepeat(elements.length)((index) =>
|
|
105
|
-
closure(elements[index], index, elements),
|
|
106
|
-
);
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Transforms each element of an array using an asynchronous function to
|
|
111
|
-
* create a new array.
|
|
112
|
-
*
|
|
113
|
-
* Similar to JavaScript's native map but processes asynchronous functions
|
|
114
|
-
* sequentially. Each element's transformation is completed before proceeding
|
|
115
|
-
* to the next element, ensuring order is maintained.
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* ```typescript
|
|
119
|
-
* const userIds = [1, 2, 3, 4, 5];
|
|
120
|
-
*
|
|
121
|
-
* const userDetails = await ArrayUtil.asyncMap(userIds)(
|
|
122
|
-
* async (id, index) => {
|
|
123
|
-
* console.log(`Fetching user ${id} (${index + 1}/${userIds.length})`);
|
|
124
|
-
* const response = await fetch(`/api/users/${id}`);
|
|
125
|
-
* return await response.json();
|
|
126
|
-
* }
|
|
127
|
-
* );
|
|
128
|
-
* console.log('All users fetched:', userDetails);
|
|
129
|
-
* ```
|
|
130
|
-
*
|
|
131
|
-
* @template Input - The type of elements in the input array
|
|
132
|
-
* @template Output - The type of elements in the output array
|
|
133
|
-
* @param elements - The readonly array to transform
|
|
134
|
-
* @returns A function that takes a transformation function and returns a
|
|
135
|
-
* Promise resolving to the transformed array
|
|
136
|
-
*/
|
|
137
|
-
export const asyncMap =
|
|
138
|
-
<Input>(elements: readonly Input[]) =>
|
|
139
|
-
async <Output>(
|
|
140
|
-
closure: (
|
|
141
|
-
elem: Input,
|
|
142
|
-
index: number,
|
|
143
|
-
array: readonly Input[],
|
|
144
|
-
) => Promise<Output>,
|
|
145
|
-
): Promise<Output[]> => {
|
|
146
|
-
const ret: Output[] = [];
|
|
147
|
-
await asyncForEach(elements)(async (elem, index, array) => {
|
|
148
|
-
const output: Output = await closure(elem, index, array);
|
|
149
|
-
ret.push(output);
|
|
150
|
-
});
|
|
151
|
-
return ret;
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Executes an asynchronous function a specified number of times sequentially.
|
|
156
|
-
*
|
|
157
|
-
* Executes the function with indices from 0 to count-1 incrementally. Each
|
|
158
|
-
* execution is performed sequentially, and all results are collected into an
|
|
159
|
-
* array.
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* ```typescript
|
|
163
|
-
* // Generate random data 5 times
|
|
164
|
-
* const randomData = await ArrayUtil.asyncRepeat(5)(async (index) => {
|
|
165
|
-
* await new Promise(resolve => setTimeout(resolve, 100)); // Wait 0.1 seconds
|
|
166
|
-
* return {
|
|
167
|
-
* id: index,
|
|
168
|
-
* value: Math.random(),
|
|
169
|
-
* timestamp: new Date().toISOString()
|
|
170
|
-
* };
|
|
171
|
-
* });
|
|
172
|
-
* console.log('Generated data:', randomData);
|
|
173
|
-
* ```;
|
|
174
|
-
*
|
|
175
|
-
* @param count - The number of times to repeat (non-negative integer)
|
|
176
|
-
* @returns A function that takes an async closure and returns a Promise
|
|
177
|
-
* resolving to an array of results
|
|
178
|
-
*/
|
|
179
|
-
export const asyncRepeat =
|
|
180
|
-
(count: number) =>
|
|
181
|
-
async <T>(closure: (index: number) => Promise<T>): Promise<T[]> => {
|
|
182
|
-
const indexes: number[] = new Array(count)
|
|
183
|
-
.fill(1)
|
|
184
|
-
.map((_, index) => index);
|
|
185
|
-
|
|
186
|
-
const output: T[] = [];
|
|
187
|
-
for (const index of indexes) output.push(await closure(index));
|
|
188
|
-
|
|
189
|
-
return output;
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Checks if at least one element in the array satisfies the given condition.
|
|
194
|
-
*
|
|
195
|
-
* Similar to JavaScript's native some() method but implemented in curried
|
|
196
|
-
* form for better compatibility with functional programming style. Returns
|
|
197
|
-
* true immediately when the first element satisfying the condition is found.
|
|
198
|
-
*
|
|
199
|
-
* @example
|
|
200
|
-
* ```typescript
|
|
201
|
-
* const numbers = [1, 3, 5, 7, 8, 9];
|
|
202
|
-
* const products = [
|
|
203
|
-
* { name: 'Apple', price: 100, inStock: true },
|
|
204
|
-
* { name: 'Banana', price: 50, inStock: false },
|
|
205
|
-
* { name: 'Orange', price: 80, inStock: true }
|
|
206
|
-
* ];
|
|
207
|
-
*
|
|
208
|
-
* const hasEvenNumber = ArrayUtil.has(numbers)(num => num % 2 === 0);
|
|
209
|
-
* console.log(hasEvenNumber); // true (8 exists)
|
|
210
|
-
*
|
|
211
|
-
* const hasExpensiveItem = ArrayUtil.has(products)(product => product.price > 90);
|
|
212
|
-
* console.log(hasExpensiveItem); // true (Apple costs 100)
|
|
213
|
-
*
|
|
214
|
-
* const hasOutOfStock = ArrayUtil.has(products)(product => !product.inStock);
|
|
215
|
-
* console.log(hasOutOfStock); // true (Banana is out of stock)
|
|
216
|
-
* ```;
|
|
217
|
-
*
|
|
218
|
-
* @template T - The type of elements in the array
|
|
219
|
-
* @param elements - The readonly array to check
|
|
220
|
-
* @returns A function that takes a predicate and returns a boolean
|
|
221
|
-
*/
|
|
222
|
-
export const has =
|
|
223
|
-
<T>(elements: readonly T[]) =>
|
|
224
|
-
(pred: (elem: T) => boolean): boolean =>
|
|
225
|
-
elements.find(pred) !== undefined;
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Executes a function a specified number of times and collects the results
|
|
229
|
-
* into an array.
|
|
230
|
-
*
|
|
231
|
-
* A synchronous repetition function that executes the given function for each
|
|
232
|
-
* index (from 0 to count-1) and collects the results into an array.
|
|
233
|
-
*
|
|
234
|
-
* @example
|
|
235
|
-
* ```typescript
|
|
236
|
-
* // Generate an array of squares from 1 to 5
|
|
237
|
-
* const squares = ArrayUtil.repeat(5)(index => (index + 1) ** 2);
|
|
238
|
-
* console.log(squares); // [1, 4, 9, 16, 25]
|
|
239
|
-
*
|
|
240
|
-
* // Generate an array of default user objects
|
|
241
|
-
* const users = ArrayUtil.repeat(3)(index => ({
|
|
242
|
-
* id: index + 1,
|
|
243
|
-
* name: `User${index + 1}`,
|
|
244
|
-
* email: `user${index + 1}@example.com`
|
|
245
|
-
* }));
|
|
246
|
-
* console.log(users);
|
|
247
|
-
* // [
|
|
248
|
-
* // { id: 1, name: 'User1', email: 'user1@example.com' },
|
|
249
|
-
* // { id: 2, name: 'User2', email: 'user2@example.com' },
|
|
250
|
-
* // { id: 3, name: 'User3', email: 'user3@example.com' }
|
|
251
|
-
* // ]
|
|
252
|
-
* ```
|
|
253
|
-
*
|
|
254
|
-
* @param count - The number of times to repeat (non-negative integer)
|
|
255
|
-
* @returns A function that takes a closure and returns an array of results
|
|
256
|
-
*/
|
|
257
|
-
export const repeat =
|
|
258
|
-
(count: number) =>
|
|
259
|
-
<T>(closure: (index: number) => T): T[] =>
|
|
260
|
-
new Array(count).fill("").map((_, index) => closure(index));
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Generates all possible subsets of a given array.
|
|
264
|
-
*
|
|
265
|
-
* Implements the mathematical concept of power set, generating 2^n subsets
|
|
266
|
-
* from an array of n elements. Uses depth-first search (DFS) algorithm to
|
|
267
|
-
* calculate all possible combinations of including or excluding each
|
|
268
|
-
* element.
|
|
269
|
-
*
|
|
270
|
-
* @example
|
|
271
|
-
* ```typescript
|
|
272
|
-
* const numbers = [1, 2, 3];
|
|
273
|
-
* const allSubsets = ArrayUtil.subsets(numbers);
|
|
274
|
-
* console.log(allSubsets);
|
|
275
|
-
* // [
|
|
276
|
-
* // [], // empty set
|
|
277
|
-
* // [3], // {3}
|
|
278
|
-
* // [2], // {2}
|
|
279
|
-
* // [2, 3], // {2, 3}
|
|
280
|
-
* // [1], // {1}
|
|
281
|
-
* // [1, 3], // {1, 3}
|
|
282
|
-
* // [1, 2], // {1, 2}
|
|
283
|
-
* // [1, 2, 3] // {1, 2, 3}
|
|
284
|
-
* // ]
|
|
285
|
-
*
|
|
286
|
-
* const colors = ['red', 'blue'];
|
|
287
|
-
* const colorSubsets = ArrayUtil.subsets(colors);
|
|
288
|
-
* console.log(colorSubsets);
|
|
289
|
-
* // [
|
|
290
|
-
* // [],
|
|
291
|
-
* // ['blue'],
|
|
292
|
-
* // ['red'],
|
|
293
|
-
* // ['red', 'blue']
|
|
294
|
-
* // ]
|
|
295
|
-
*
|
|
296
|
-
* // Warning: Result size grows exponentially with array size
|
|
297
|
-
* // Example: 10 elements → 1,024 subsets, 20 elements → 1,048,576 subsets
|
|
298
|
-
* ```;
|
|
299
|
-
*
|
|
300
|
-
* @template T - The type of elements in the array
|
|
301
|
-
* @param array - The array to generate subsets from
|
|
302
|
-
* @returns An array containing all possible subsets
|
|
303
|
-
*/
|
|
304
|
-
export const subsets = <T>(array: T[]): T[][] => {
|
|
305
|
-
const check: boolean[] = new Array(array.length).fill(false);
|
|
306
|
-
const output: T[][] = [];
|
|
307
|
-
|
|
308
|
-
const dfs = (depth: number): void => {
|
|
309
|
-
if (depth === check.length)
|
|
310
|
-
output.push(array.filter((_v, idx) => check[idx]));
|
|
311
|
-
else {
|
|
312
|
-
check[depth] = true;
|
|
313
|
-
dfs(depth + 1);
|
|
314
|
-
|
|
315
|
-
check[depth] = false;
|
|
316
|
-
dfs(depth + 1);
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
dfs(0);
|
|
320
|
-
return output;
|
|
321
|
-
};
|
|
322
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* A namespace providing utility functions for array manipulation.
|
|
3
|
+
*
|
|
4
|
+
* This namespace contains utility functions for array operations including
|
|
5
|
+
* asynchronous processing, filtering, mapping, and repetition tasks implemented
|
|
6
|
+
* in functional programming style. All functions are implemented using currying
|
|
7
|
+
* to enhance reusability and composability.
|
|
8
|
+
*
|
|
9
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Asynchronous filtering example
|
|
13
|
+
* const numbers = [1, 2, 3, 4, 5];
|
|
14
|
+
* const evenNumbers = await ArrayUtil.asyncFilter(numbers)(
|
|
15
|
+
* async (num) => num % 2 === 0
|
|
16
|
+
* );
|
|
17
|
+
* console.log(evenNumbers); // [2, 4]
|
|
18
|
+
* ```;
|
|
19
|
+
*/
|
|
20
|
+
export namespace ArrayUtil {
|
|
21
|
+
/**
|
|
22
|
+
* Filters an array by applying an asynchronous predicate function to each
|
|
23
|
+
* element.
|
|
24
|
+
*
|
|
25
|
+
* This function is implemented in curried form, first taking an array and
|
|
26
|
+
* then a predicate function. Elements are processed sequentially, ensuring
|
|
27
|
+
* order is maintained.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const users = [
|
|
32
|
+
* { id: 1, name: 'Alice', active: true },
|
|
33
|
+
* { id: 2, name: 'Bob', active: false },
|
|
34
|
+
* { id: 3, name: 'Charlie', active: true }
|
|
35
|
+
* ];
|
|
36
|
+
*
|
|
37
|
+
* const activeUsers = await ArrayUtil.asyncFilter(users)(
|
|
38
|
+
* async (user) => {
|
|
39
|
+
* // Async validation logic (e.g., API call)
|
|
40
|
+
* await new Promise(resolve => setTimeout(resolve, 100));
|
|
41
|
+
* return user.active;
|
|
42
|
+
* }
|
|
43
|
+
* );
|
|
44
|
+
* console.log(activeUsers); // [{ id: 1, name: 'Alice', active: true }, { id: 3, name: 'Charlie', active: true }]
|
|
45
|
+
* ```;
|
|
46
|
+
*
|
|
47
|
+
* @template Input - The type of elements in the input array
|
|
48
|
+
* @param elements - The readonly array to filter
|
|
49
|
+
* @returns A function that takes a predicate and returns a Promise resolving
|
|
50
|
+
* to the filtered array
|
|
51
|
+
*/
|
|
52
|
+
export const asyncFilter =
|
|
53
|
+
<Input>(elements: readonly Input[]) =>
|
|
54
|
+
async (
|
|
55
|
+
pred: (
|
|
56
|
+
elem: Input,
|
|
57
|
+
index: number,
|
|
58
|
+
array: readonly Input[],
|
|
59
|
+
) => Promise<boolean>,
|
|
60
|
+
): Promise<Input[]> => {
|
|
61
|
+
const ret: Input[] = [];
|
|
62
|
+
await asyncForEach(elements)(async (elem, index, array) => {
|
|
63
|
+
const flag: boolean = await pred(elem, index, array);
|
|
64
|
+
if (flag === true) ret.push(elem);
|
|
65
|
+
});
|
|
66
|
+
return ret;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Executes an asynchronous function for each element in an array
|
|
71
|
+
* sequentially.
|
|
72
|
+
*
|
|
73
|
+
* Unlike JavaScript's native forEach, this function processes asynchronous
|
|
74
|
+
* functions sequentially and waits for all operations to complete. It
|
|
75
|
+
* performs sequential processing rather than parallel processing, making it
|
|
76
|
+
* suitable for operations where order matters.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const urls = ['url1', 'url2', 'url3'];
|
|
81
|
+
*
|
|
82
|
+
* await ArrayUtil.asyncForEach(urls)(async (url, index) => {
|
|
83
|
+
* console.log(`Processing ${index}: ${url}`);
|
|
84
|
+
* const data = await fetch(url);
|
|
85
|
+
* await processData(data);
|
|
86
|
+
* console.log(`Completed ${index}: ${url}`);
|
|
87
|
+
* });
|
|
88
|
+
* console.log('All URLs processed sequentially');
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @template Input - The type of elements in the input array
|
|
92
|
+
* @param elements - The readonly array to process
|
|
93
|
+
* @returns A function that takes an async closure and returns a Promise<void>
|
|
94
|
+
*/
|
|
95
|
+
export const asyncForEach =
|
|
96
|
+
<Input>(elements: readonly Input[]) =>
|
|
97
|
+
async (
|
|
98
|
+
closure: (
|
|
99
|
+
elem: Input,
|
|
100
|
+
index: number,
|
|
101
|
+
array: readonly Input[],
|
|
102
|
+
) => Promise<any>,
|
|
103
|
+
): Promise<void> => {
|
|
104
|
+
await asyncRepeat(elements.length)((index) =>
|
|
105
|
+
closure(elements[index], index, elements),
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Transforms each element of an array using an asynchronous function to
|
|
111
|
+
* create a new array.
|
|
112
|
+
*
|
|
113
|
+
* Similar to JavaScript's native map but processes asynchronous functions
|
|
114
|
+
* sequentially. Each element's transformation is completed before proceeding
|
|
115
|
+
* to the next element, ensuring order is maintained.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const userIds = [1, 2, 3, 4, 5];
|
|
120
|
+
*
|
|
121
|
+
* const userDetails = await ArrayUtil.asyncMap(userIds)(
|
|
122
|
+
* async (id, index) => {
|
|
123
|
+
* console.log(`Fetching user ${id} (${index + 1}/${userIds.length})`);
|
|
124
|
+
* const response = await fetch(`/api/users/${id}`);
|
|
125
|
+
* return await response.json();
|
|
126
|
+
* }
|
|
127
|
+
* );
|
|
128
|
+
* console.log('All users fetched:', userDetails);
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @template Input - The type of elements in the input array
|
|
132
|
+
* @template Output - The type of elements in the output array
|
|
133
|
+
* @param elements - The readonly array to transform
|
|
134
|
+
* @returns A function that takes a transformation function and returns a
|
|
135
|
+
* Promise resolving to the transformed array
|
|
136
|
+
*/
|
|
137
|
+
export const asyncMap =
|
|
138
|
+
<Input>(elements: readonly Input[]) =>
|
|
139
|
+
async <Output>(
|
|
140
|
+
closure: (
|
|
141
|
+
elem: Input,
|
|
142
|
+
index: number,
|
|
143
|
+
array: readonly Input[],
|
|
144
|
+
) => Promise<Output>,
|
|
145
|
+
): Promise<Output[]> => {
|
|
146
|
+
const ret: Output[] = [];
|
|
147
|
+
await asyncForEach(elements)(async (elem, index, array) => {
|
|
148
|
+
const output: Output = await closure(elem, index, array);
|
|
149
|
+
ret.push(output);
|
|
150
|
+
});
|
|
151
|
+
return ret;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Executes an asynchronous function a specified number of times sequentially.
|
|
156
|
+
*
|
|
157
|
+
* Executes the function with indices from 0 to count-1 incrementally. Each
|
|
158
|
+
* execution is performed sequentially, and all results are collected into an
|
|
159
|
+
* array.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* // Generate random data 5 times
|
|
164
|
+
* const randomData = await ArrayUtil.asyncRepeat(5)(async (index) => {
|
|
165
|
+
* await new Promise(resolve => setTimeout(resolve, 100)); // Wait 0.1 seconds
|
|
166
|
+
* return {
|
|
167
|
+
* id: index,
|
|
168
|
+
* value: Math.random(),
|
|
169
|
+
* timestamp: new Date().toISOString()
|
|
170
|
+
* };
|
|
171
|
+
* });
|
|
172
|
+
* console.log('Generated data:', randomData);
|
|
173
|
+
* ```;
|
|
174
|
+
*
|
|
175
|
+
* @param count - The number of times to repeat (non-negative integer)
|
|
176
|
+
* @returns A function that takes an async closure and returns a Promise
|
|
177
|
+
* resolving to an array of results
|
|
178
|
+
*/
|
|
179
|
+
export const asyncRepeat =
|
|
180
|
+
(count: number) =>
|
|
181
|
+
async <T>(closure: (index: number) => Promise<T>): Promise<T[]> => {
|
|
182
|
+
const indexes: number[] = new Array(count)
|
|
183
|
+
.fill(1)
|
|
184
|
+
.map((_, index) => index);
|
|
185
|
+
|
|
186
|
+
const output: T[] = [];
|
|
187
|
+
for (const index of indexes) output.push(await closure(index));
|
|
188
|
+
|
|
189
|
+
return output;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Checks if at least one element in the array satisfies the given condition.
|
|
194
|
+
*
|
|
195
|
+
* Similar to JavaScript's native some() method but implemented in curried
|
|
196
|
+
* form for better compatibility with functional programming style. Returns
|
|
197
|
+
* true immediately when the first element satisfying the condition is found.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* const numbers = [1, 3, 5, 7, 8, 9];
|
|
202
|
+
* const products = [
|
|
203
|
+
* { name: 'Apple', price: 100, inStock: true },
|
|
204
|
+
* { name: 'Banana', price: 50, inStock: false },
|
|
205
|
+
* { name: 'Orange', price: 80, inStock: true }
|
|
206
|
+
* ];
|
|
207
|
+
*
|
|
208
|
+
* const hasEvenNumber = ArrayUtil.has(numbers)(num => num % 2 === 0);
|
|
209
|
+
* console.log(hasEvenNumber); // true (8 exists)
|
|
210
|
+
*
|
|
211
|
+
* const hasExpensiveItem = ArrayUtil.has(products)(product => product.price > 90);
|
|
212
|
+
* console.log(hasExpensiveItem); // true (Apple costs 100)
|
|
213
|
+
*
|
|
214
|
+
* const hasOutOfStock = ArrayUtil.has(products)(product => !product.inStock);
|
|
215
|
+
* console.log(hasOutOfStock); // true (Banana is out of stock)
|
|
216
|
+
* ```;
|
|
217
|
+
*
|
|
218
|
+
* @template T - The type of elements in the array
|
|
219
|
+
* @param elements - The readonly array to check
|
|
220
|
+
* @returns A function that takes a predicate and returns a boolean
|
|
221
|
+
*/
|
|
222
|
+
export const has =
|
|
223
|
+
<T>(elements: readonly T[]) =>
|
|
224
|
+
(pred: (elem: T) => boolean): boolean =>
|
|
225
|
+
elements.find(pred) !== undefined;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Executes a function a specified number of times and collects the results
|
|
229
|
+
* into an array.
|
|
230
|
+
*
|
|
231
|
+
* A synchronous repetition function that executes the given function for each
|
|
232
|
+
* index (from 0 to count-1) and collects the results into an array.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```typescript
|
|
236
|
+
* // Generate an array of squares from 1 to 5
|
|
237
|
+
* const squares = ArrayUtil.repeat(5)(index => (index + 1) ** 2);
|
|
238
|
+
* console.log(squares); // [1, 4, 9, 16, 25]
|
|
239
|
+
*
|
|
240
|
+
* // Generate an array of default user objects
|
|
241
|
+
* const users = ArrayUtil.repeat(3)(index => ({
|
|
242
|
+
* id: index + 1,
|
|
243
|
+
* name: `User${index + 1}`,
|
|
244
|
+
* email: `user${index + 1}@example.com`
|
|
245
|
+
* }));
|
|
246
|
+
* console.log(users);
|
|
247
|
+
* // [
|
|
248
|
+
* // { id: 1, name: 'User1', email: 'user1@example.com' },
|
|
249
|
+
* // { id: 2, name: 'User2', email: 'user2@example.com' },
|
|
250
|
+
* // { id: 3, name: 'User3', email: 'user3@example.com' }
|
|
251
|
+
* // ]
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* @param count - The number of times to repeat (non-negative integer)
|
|
255
|
+
* @returns A function that takes a closure and returns an array of results
|
|
256
|
+
*/
|
|
257
|
+
export const repeat =
|
|
258
|
+
(count: number) =>
|
|
259
|
+
<T>(closure: (index: number) => T): T[] =>
|
|
260
|
+
new Array(count).fill("").map((_, index) => closure(index));
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Generates all possible subsets of a given array.
|
|
264
|
+
*
|
|
265
|
+
* Implements the mathematical concept of power set, generating 2^n subsets
|
|
266
|
+
* from an array of n elements. Uses depth-first search (DFS) algorithm to
|
|
267
|
+
* calculate all possible combinations of including or excluding each
|
|
268
|
+
* element.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* const numbers = [1, 2, 3];
|
|
273
|
+
* const allSubsets = ArrayUtil.subsets(numbers);
|
|
274
|
+
* console.log(allSubsets);
|
|
275
|
+
* // [
|
|
276
|
+
* // [], // empty set
|
|
277
|
+
* // [3], // {3}
|
|
278
|
+
* // [2], // {2}
|
|
279
|
+
* // [2, 3], // {2, 3}
|
|
280
|
+
* // [1], // {1}
|
|
281
|
+
* // [1, 3], // {1, 3}
|
|
282
|
+
* // [1, 2], // {1, 2}
|
|
283
|
+
* // [1, 2, 3] // {1, 2, 3}
|
|
284
|
+
* // ]
|
|
285
|
+
*
|
|
286
|
+
* const colors = ['red', 'blue'];
|
|
287
|
+
* const colorSubsets = ArrayUtil.subsets(colors);
|
|
288
|
+
* console.log(colorSubsets);
|
|
289
|
+
* // [
|
|
290
|
+
* // [],
|
|
291
|
+
* // ['blue'],
|
|
292
|
+
* // ['red'],
|
|
293
|
+
* // ['red', 'blue']
|
|
294
|
+
* // ]
|
|
295
|
+
*
|
|
296
|
+
* // Warning: Result size grows exponentially with array size
|
|
297
|
+
* // Example: 10 elements → 1,024 subsets, 20 elements → 1,048,576 subsets
|
|
298
|
+
* ```;
|
|
299
|
+
*
|
|
300
|
+
* @template T - The type of elements in the array
|
|
301
|
+
* @param array - The array to generate subsets from
|
|
302
|
+
* @returns An array containing all possible subsets
|
|
303
|
+
*/
|
|
304
|
+
export const subsets = <T>(array: T[]): T[][] => {
|
|
305
|
+
const check: boolean[] = new Array(array.length).fill(false);
|
|
306
|
+
const output: T[][] = [];
|
|
307
|
+
|
|
308
|
+
const dfs = (depth: number): void => {
|
|
309
|
+
if (depth === check.length)
|
|
310
|
+
output.push(array.filter((_v, idx) => check[idx]));
|
|
311
|
+
else {
|
|
312
|
+
check[depth] = true;
|
|
313
|
+
dfs(depth + 1);
|
|
314
|
+
|
|
315
|
+
check[depth] = false;
|
|
316
|
+
dfs(depth + 1);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
dfs(0);
|
|
320
|
+
return output;
|
|
321
|
+
};
|
|
322
|
+
}
|