@nestia/e2e 7.0.0 → 7.0.2

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/src/ArrayUtil.ts CHANGED
@@ -1,9 +1,54 @@
1
1
  /**
2
- * Utility functions for arrays.
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.
3
8
  *
4
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
+ * ```;
5
19
  */
6
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
+ */
7
52
  export const asyncFilter =
8
53
  <Input>(elements: readonly Input[]) =>
9
54
  async (
@@ -21,6 +66,32 @@ export namespace ArrayUtil {
21
66
  return ret;
22
67
  };
23
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
+ */
24
95
  export const asyncForEach =
25
96
  <Input>(elements: readonly Input[]) =>
26
97
  async (
@@ -35,6 +106,34 @@ export namespace ArrayUtil {
35
106
  );
36
107
  };
37
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
+ */
38
137
  export const asyncMap =
39
138
  <Input>(elements: readonly Input[]) =>
40
139
  async <Output>(
@@ -52,6 +151,31 @@ export namespace ArrayUtil {
52
151
  return ret;
53
152
  };
54
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
+ */
55
179
  export const asyncRepeat =
56
180
  (count: number) =>
57
181
  async <T>(closure: (index: number) => Promise<T>): Promise<T[]> => {
@@ -65,18 +189,118 @@ export namespace ArrayUtil {
65
189
  return output;
66
190
  };
67
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
+ */
68
222
  export const has =
69
223
  <T>(elements: readonly T[]) =>
70
224
  (pred: (elem: T) => boolean): boolean =>
71
225
  elements.find(pred) !== undefined;
72
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
+ */
73
257
  export const repeat =
74
258
  (count: number) =>
75
259
  <T>(closure: (index: number) => T): T[] =>
76
260
  new Array(count).fill("").map((_, index) => closure(index));
77
261
 
78
- export const flat = <T>(matrix: T[][]): T[] => ([] as T[]).concat(...matrix);
79
-
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
+ */
80
304
  export const subsets = <T>(array: T[]): T[][] => {
81
305
  const check: boolean[] = new Array(array.length).fill(false);
82
306
  const output: T[][] = [];
@@ -1,19 +1,103 @@
1
1
  /**
2
- * Gaff comparator.
2
+ * Type-safe comparator functions for Array.sort() operations with advanced
3
+ * field access.
3
4
  *
4
- * `GaffComparator` is a set of comparator functions for `Array.sort()` function,
5
- * which can be used with {@link TestValidator.sort} function. If you want to see
6
- * how to use them, see the below example link.
5
+ * GaffComparator provides a collection of specialized comparator functions
6
+ * designed to work seamlessly with Array.sort() and testing frameworks like
7
+ * TestValidator.sort(). Each comparator supports both single values and arrays
8
+ * of values, enabling complex multi-field sorting scenarios with lexicographic
9
+ * ordering.
10
+ *
11
+ * Key features:
12
+ *
13
+ * - Generic type safety for any object structure
14
+ * - Support for single values or arrays of values per field
15
+ * - Lexicographic comparison for multi-value scenarios
16
+ * - Locale-aware string comparison
17
+ * - Automatic type conversion for dates and numbers
18
+ *
19
+ * The comparators follow the standard JavaScript sort contract:
20
+ *
21
+ * - Return < 0 if first element should come before second
22
+ * - Return > 0 if first element should come after second
23
+ * - Return 0 if elements are equal
7
24
  *
8
- * @example https://github.com/samchon/nestia-template/blob/master/src/test/features/api/bbs/test_api_bbs_article_index_sort.ts
9
25
  * @author Jeongho Nam - https://github.com/samchon
26
+ * @example
27
+ * ```typescript
28
+ * // Basic usage with single fields
29
+ * users.sort(GaffComparator.strings(user => user.name));
30
+ * posts.sort(GaffComparator.dates(post => post.createdAt));
31
+ * products.sort(GaffComparator.numbers(product => product.price));
32
+ *
33
+ * // Multi-field sorting with arrays
34
+ * users.sort(GaffComparator.strings(user => [user.lastName, user.firstName]));
35
+ * events.sort(GaffComparator.dates(event => [event.startDate, event.endDate]));
36
+ *
37
+ * // Integration with TestValidator
38
+ * await TestValidator.sort("user sorting")(
39
+ * (sortable) => api.getUsers({ sort: sortable })
40
+ * )("name", "email")(
41
+ * GaffComparator.strings(user => [user.name, user.email])
42
+ * )("+");
43
+ * ```;
10
44
  */
11
45
  export namespace GaffComparator {
12
46
  /**
13
- * String(s) comparator.
47
+ * Creates a comparator function for string-based sorting with locale-aware
48
+ * comparison.
49
+ *
50
+ * Generates a comparator that extracts string values from objects and
51
+ * performs lexicographic comparison using locale-sensitive string comparison.
52
+ * Supports both single strings and arrays of strings for multi-field sorting
53
+ * scenarios.
54
+ *
55
+ * When comparing arrays, performs lexicographic ordering: compares the first
56
+ * elements, then the second elements if the first are equal, and so on. This
57
+ * enables complex sorting like "sort by last name, then by first name".
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * interface User {
62
+ * id: string;
63
+ * firstName: string;
64
+ * lastName: string;
65
+ * email: string;
66
+ * status: 'active' | 'inactive';
67
+ * }
68
+ *
69
+ * const users: User[] = [
70
+ * { id: '1', firstName: 'John', lastName: 'Doe', email: 'john@example.com', status: 'active' },
71
+ * { id: '2', firstName: 'Jane', lastName: 'Doe', email: 'jane@example.com', status: 'inactive' },
72
+ * { id: '3', firstName: 'Bob', lastName: 'Smith', email: 'bob@example.com', status: 'active' }
73
+ * ];
14
74
  *
15
- * @param getter Getter of string(s) from input
16
- * @returns Comparator function
75
+ * // Single field sorting
76
+ * users.sort(GaffComparator.strings(user => user.lastName));
77
+ * // Result: Doe, Doe, Smith
78
+ *
79
+ * // Multi-field sorting: last name, then first name
80
+ * users.sort(GaffComparator.strings(user => [user.lastName, user.firstName]));
81
+ * // Result: Doe Jane, Doe John, Smith Bob
82
+ *
83
+ * // Status-based sorting
84
+ * users.sort(GaffComparator.strings(user => user.status));
85
+ * // Result: active users first, then inactive
86
+ *
87
+ * // Complex multi-field: status, then last name, then first name
88
+ * users.sort(GaffComparator.strings(user => [user.status, user.lastName, user.firstName]));
89
+ *
90
+ * // Integration with API sorting validation
91
+ * await TestValidator.sort("user name sorting")(
92
+ * (sortFields) => userApi.getUsers({ sort: sortFields })
93
+ * )("lastName", "firstName")(
94
+ * GaffComparator.strings(user => [user.lastName, user.firstName])
95
+ * )("+");
96
+ * ```;
97
+ *
98
+ * @template T - The type of objects being compared
99
+ * @param getter - Function that extracts string value(s) from input objects
100
+ * @returns A comparator function suitable for Array.sort()
17
101
  */
18
102
  export const strings =
19
103
  <T>(getter: (input: T) => string | string[]) =>
@@ -26,10 +110,78 @@ export namespace GaffComparator {
26
110
  };
27
111
 
28
112
  /**
29
- * Date(s) comparator.
113
+ * Creates a comparator function for date-based sorting with automatic string
114
+ * parsing.
115
+ *
116
+ * Generates a comparator that extracts date values from objects,
117
+ * automatically converting string representations to Date objects for
118
+ * numerical comparison. Supports both single dates and arrays of dates for
119
+ * complex temporal sorting.
120
+ *
121
+ * Date strings are parsed using the standard Date constructor, which supports
122
+ * ISO 8601 format, RFC 2822 format, and other common date representations.
123
+ * The comparison is performed on millisecond timestamps for precise
124
+ * ordering.
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * interface Event {
129
+ * id: string;
130
+ * title: string;
131
+ * startDate: string;
132
+ * endDate: string;
133
+ * createdAt: string;
134
+ * updatedAt: string;
135
+ * }
136
+ *
137
+ * const events: Event[] = [
138
+ * {
139
+ * id: '1',
140
+ * title: 'Conference',
141
+ * startDate: '2024-03-15T09:00:00Z',
142
+ * endDate: '2024-03-15T17:00:00Z',
143
+ * createdAt: '2024-01-10T10:00:00Z',
144
+ * updatedAt: '2024-02-01T15:30:00Z'
145
+ * },
146
+ * {
147
+ * id: '2',
148
+ * title: 'Workshop',
149
+ * startDate: '2024-03-10T14:00:00Z',
150
+ * endDate: '2024-03-10T16:00:00Z',
151
+ * createdAt: '2024-01-15T11:00:00Z',
152
+ * updatedAt: '2024-01-20T09:15:00Z'
153
+ * }
154
+ * ];
30
155
  *
31
- * @param getter Getter of date(s) from input
32
- * @returns Comparator function
156
+ * // Sort by start date (chronological order)
157
+ * events.sort(GaffComparator.dates(event => event.startDate));
158
+ *
159
+ * // Sort by creation date (oldest first)
160
+ * events.sort(GaffComparator.dates(event => event.createdAt));
161
+ *
162
+ * // Multi-field: start date, then end date
163
+ * events.sort(GaffComparator.dates(event => [event.startDate, event.endDate]));
164
+ *
165
+ * // Sort by modification history: created date, then updated date
166
+ * events.sort(GaffComparator.dates(event => [event.createdAt, event.updatedAt]));
167
+ *
168
+ * // Validate API date sorting
169
+ * await TestValidator.sort("event chronological sorting")(
170
+ * (sortFields) => eventApi.getEvents({ sort: sortFields })
171
+ * )("startDate")(
172
+ * GaffComparator.dates(event => event.startDate)
173
+ * )("+");
174
+ *
175
+ * // Test complex date-based sorting
176
+ * const sortByEventSchedule = GaffComparator.dates(event => [
177
+ * event.startDate,
178
+ * event.endDate
179
+ * ]);
180
+ * ```;
181
+ *
182
+ * @template T - The type of objects being compared
183
+ * @param getter - Function that extracts date string(s) from input objects
184
+ * @returns A comparator function suitable for Array.sort()
33
185
  */
34
186
  export const dates =
35
187
  <T>(getter: (input: T) => string | string[]) =>
@@ -44,10 +196,73 @@ export namespace GaffComparator {
44
196
  };
45
197
 
46
198
  /**
47
- * Number(s) comparator.
199
+ * Creates a comparator function for numerical sorting with multi-value
200
+ * support.
201
+ *
202
+ * Generates a comparator that extracts numerical values from objects and
203
+ * performs mathematical comparison. Supports both single numbers and arrays
204
+ * of numbers for complex numerical sorting scenarios like sorting by price
205
+ * then by rating.
206
+ *
207
+ * When comparing arrays, performs lexicographic numerical ordering: compares
208
+ * the first numbers, then the second numbers if the first are equal, and so
209
+ * on. This enables sophisticated sorting like "sort by price ascending, then
210
+ * by rating descending".
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * interface Product {
215
+ * id: string;
216
+ * name: string;
217
+ * price: number;
218
+ * rating: number;
219
+ * stock: number;
220
+ * categoryId: number;
221
+ * salesCount: number;
222
+ * }
48
223
  *
49
- * @param closure Getter of number(s) from input
50
- * @returns Comparator function
224
+ * const products: Product[] = [
225
+ * { id: '1', name: 'Laptop', price: 999.99, rating: 4.5, stock: 15, categoryId: 1, salesCount: 150 },
226
+ * { id: '2', name: 'Mouse', price: 29.99, rating: 4.2, stock: 50, categoryId: 1, salesCount: 300 },
227
+ * { id: '3', name: 'Keyboard', price: 79.99, rating: 4.8, stock: 25, categoryId: 1, salesCount: 200 }
228
+ * ];
229
+ *
230
+ * // Sort by price (ascending)
231
+ * products.sort(GaffComparator.numbers(product => product.price));
232
+ * // Result: Mouse ($29.99), Keyboard ($79.99), Laptop ($999.99)
233
+ *
234
+ * // Sort by rating (descending requires negation)
235
+ * products.sort(GaffComparator.numbers(product => -product.rating));
236
+ * // Result: Keyboard (4.8), Laptop (4.5), Mouse (4.2)
237
+ *
238
+ * // Multi-field: category, then price
239
+ * products.sort(GaffComparator.numbers(product => [product.categoryId, product.price]));
240
+ *
241
+ * // Complex business logic: popularity (sales) then rating
242
+ * products.sort(GaffComparator.numbers(product => [-product.salesCount, -product.rating]));
243
+ * // Negative values for descending order
244
+ *
245
+ * // Sort by inventory priority: low stock first, then by sales
246
+ * products.sort(GaffComparator.numbers(product => [product.stock, -product.salesCount]));
247
+ *
248
+ * // Validate API numerical sorting
249
+ * await TestValidator.sort("product price sorting")(
250
+ * (sortFields) => productApi.getProducts({ sort: sortFields })
251
+ * )("price")(
252
+ * GaffComparator.numbers(product => product.price)
253
+ * )("+");
254
+ *
255
+ * // Test multi-criteria sorting
256
+ * const sortByBusinessValue = GaffComparator.numbers(product => [
257
+ * -product.salesCount, // High sales first
258
+ * -product.rating, // High rating first
259
+ * product.price // Low price first (for tie-breaking)
260
+ * ]);
261
+ * ```;
262
+ *
263
+ * @template T - The type of objects being compared
264
+ * @param closure - Function that extracts number value(s) from input objects
265
+ * @returns A comparator function suitable for Array.sort()
51
266
  */
52
267
  export const numbers =
53
268
  <T>(closure: (input: T) => number | number[]) =>
@@ -60,5 +275,6 @@ export namespace GaffComparator {
60
275
  };
61
276
 
62
277
  const compare = (x: string, y: string) => x.localeCompare(y);
278
+
63
279
  const wrap = <T>(elem: T | T[]): T[] => (Array.isArray(elem) ? elem : [elem]);
64
280
  }