@nestia/e2e 14.0.1 → 14.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/lib/ArrayUtil.d.ts +152 -70
- package/lib/ArrayUtil.js +168 -74
- package/lib/ArrayUtil.js.map +1 -1
- package/lib/DynamicExecutor.d.ts +139 -11
- package/lib/DynamicExecutor.js +98 -10
- package/lib/DynamicExecutor.js.map +1 -1
- package/lib/GaffComparator.d.ts +176 -73
- package/lib/GaffComparator.js +189 -76
- package/lib/GaffComparator.js.map +1 -1
- package/lib/MapUtil.d.ts +37 -6
- package/lib/MapUtil.js +37 -6
- package/lib/MapUtil.js.map +1 -1
- package/lib/RandomGenerator.d.ts +157 -84
- package/lib/RandomGenerator.js +173 -89
- package/lib/RandomGenerator.js.map +1 -1
- package/lib/TestValidator.d.ts +248 -74
- package/lib/TestValidator.js +252 -89
- package/lib/TestValidator.js.map +1 -1
- package/lib/internal/json_equal_to.d.ts +21 -0
- package/lib/internal/json_equal_to.js +26 -6
- package/lib/internal/json_equal_to.js.map +1 -1
- package/package.json +3 -2
- package/src/ArrayUtil.ts +172 -76
- package/src/DynamicExecutor.ts +158 -15
- package/src/GaffComparator.ts +187 -77
- package/src/MapUtil.ts +37 -6
- package/src/RandomGenerator.ts +172 -89
- package/src/TestValidator.ts +276 -98
- package/src/internal/json_equal_to.ts +25 -4
package/lib/ArrayUtil.js
CHANGED
|
@@ -18,16 +18,30 @@ exports.ArrayUtil = void 0;
|
|
|
18
18
|
* in functional programming style. Functions use direct parameter passing for
|
|
19
19
|
* simplicity while maintaining functional programming principles.
|
|
20
20
|
*
|
|
21
|
+
* Processing cost: Sequential array operations visit each required position
|
|
22
|
+
* once; has short-circuits, and subsets materializes the exponential power-set
|
|
23
|
+
* output. Async helpers await one user callback per position and preserve its
|
|
24
|
+
* ordering rather than assuming callbacks are independent.
|
|
25
|
+
*
|
|
26
|
+
* Resource ownership: Async helpers await one callback at a time and keep their
|
|
27
|
+
* input and partial output until settlement; results transfer to the caller.
|
|
28
|
+
* Synchronous helpers retain only their local arrays and recursion state.
|
|
29
|
+
* Callback resources and cancellation belong to the caller.
|
|
30
|
+
*
|
|
21
31
|
* @author Jeongho Nam - https://github.com/samchon
|
|
22
32
|
* @example
|
|
23
|
-
* ```typescript
|
|
24
33
|
* // Asynchronous filtering example
|
|
25
34
|
* const numbers = [1, 2, 3, 4, 5];
|
|
26
|
-
* const evenNumbers = await ArrayUtil.asyncFilter(
|
|
27
|
-
*
|
|
35
|
+
* const evenNumbers = await ArrayUtil.asyncFilter(
|
|
36
|
+
* numbers,
|
|
37
|
+
* async (num) => num % 2 === 0,
|
|
28
38
|
* );
|
|
29
39
|
* console.log(evenNumbers); // [2, 4]
|
|
30
|
-
*
|
|
40
|
+
*
|
|
41
|
+
* @evidence contracts/common.md#principled-implementation Asynchronous filtering and mapping share sequential `asyncForEach`; repetition awaits each index and collects its results. Synchronous helpers use loops and native methods, and `subsets` enumerates the include or exclude choice per position.
|
|
42
|
+
* @evidence contracts/common.md#clear-and-simple-design Filtering and mapping share iteration without allocating unused results; repetition separately collects its results, with one private count validator.
|
|
43
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts No function special-cases a value, a fixture, or a caller; ordering and short-circuiting follow from the loop and the native `some`.
|
|
44
|
+
* @evidence contracts/common.md#meaningful-documentation The namespace prose states the sequential, functional style, and each function documents its parameters, its result, and an example.
|
|
31
45
|
*/
|
|
32
46
|
var ArrayUtil;
|
|
33
47
|
(function (ArrayUtil) {
|
|
@@ -39,28 +53,39 @@ var ArrayUtil;
|
|
|
39
53
|
* predicate function receives the element, index, and the full array as
|
|
40
54
|
* parameters.
|
|
41
55
|
*
|
|
56
|
+
* Processing cost: Each of N elements invokes its predicate once in sequence;
|
|
57
|
+
* array bookkeeping is O(N), with O(M) output for M accepted elements.
|
|
58
|
+
*
|
|
59
|
+
* Resource ownership: The invocation awaits one callback at a time and
|
|
60
|
+
* retains only its partial output and current call; success transfers the
|
|
61
|
+
* output to the caller and rejection releases local state. Callback-owned
|
|
62
|
+
* external resources remain the callback owner’s responsibility.
|
|
63
|
+
*
|
|
42
64
|
* @example
|
|
43
|
-
* ```typescript
|
|
44
65
|
* const users = [
|
|
45
|
-
* { id: 1, name:
|
|
46
|
-
* { id: 2, name:
|
|
47
|
-
* { id: 3, name:
|
|
66
|
+
* { id: 1, name: "Alice", active: true },
|
|
67
|
+
* { id: 2, name: "Bob", active: false },
|
|
68
|
+
* { id: 3, name: "Charlie", active: true },
|
|
48
69
|
* ];
|
|
49
70
|
*
|
|
50
|
-
* const activeUsers = await ArrayUtil.asyncFilter(
|
|
71
|
+
* const activeUsers = await ArrayUtil.asyncFilter(
|
|
72
|
+
* users,
|
|
51
73
|
* async (user) => {
|
|
52
74
|
* // Async validation logic (e.g., API call)
|
|
53
|
-
* await new Promise(resolve => setTimeout(resolve, 100));
|
|
75
|
+
* await new Promise((resolve) => setTimeout(resolve, 100));
|
|
54
76
|
* return user.active;
|
|
55
|
-
* }
|
|
77
|
+
* },
|
|
56
78
|
* );
|
|
57
79
|
* console.log(activeUsers); // [{ id: 1, name: 'Alice', active: true }, { id: 3, name: 'Charlie', active: true }]
|
|
58
|
-
* ```;
|
|
59
80
|
*
|
|
60
81
|
* @template Input - The type of elements in the input array
|
|
61
82
|
* @param elements - The readonly array to filter
|
|
62
83
|
* @param pred - The asynchronous predicate function to test each element
|
|
63
84
|
* @returns A Promise resolving to the filtered array
|
|
85
|
+
* @evidence contracts/common.md#principled-implementation The predicate runs on each element in order and the next call starts after the previous promise settles; an element is kept only when the awaited result is exactly `true`, which the `Promise<boolean>` type guarantees for well-typed callers, and a rejection stops the iteration and rejects the result.
|
|
86
|
+
* @evidence contracts/common.md#clear-and-simple-design It composes `asyncForEach` with one push instead of reimplementing the loop.
|
|
87
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The predicate alone decides inclusion; no element is skipped or retried.
|
|
88
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the sequential order, the predicate arguments, and the result, with an example.
|
|
64
89
|
*/
|
|
65
90
|
ArrayUtil.asyncFilter = (elements, pred) => __awaiter(this, void 0, void 0, function* () {
|
|
66
91
|
const ret = [];
|
|
@@ -80,26 +105,38 @@ var ArrayUtil;
|
|
|
80
105
|
* performs sequential processing rather than parallel processing, making it
|
|
81
106
|
* suitable for operations where order matters.
|
|
82
107
|
*
|
|
108
|
+
* Processing cost: Each of N elements invokes its callback once in sequence:
|
|
109
|
+
* O(N) bookkeeping with no result array.
|
|
110
|
+
*
|
|
111
|
+
* Resource ownership: The invocation retains its input while awaiting one
|
|
112
|
+
* callback at a time, discards callback results and releases local state on
|
|
113
|
+
* completion or rejection. Callback-owned external resources remain the
|
|
114
|
+
* callback owner’s responsibility.
|
|
115
|
+
*
|
|
83
116
|
* @example
|
|
84
|
-
*
|
|
85
|
-
* const urls = ['url1', 'url2', 'url3'];
|
|
117
|
+
* const urls = ["url1", "url2", "url3"];
|
|
86
118
|
*
|
|
87
119
|
* await ArrayUtil.asyncForEach(urls, async (url, index) => {
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
120
|
+
* console.log(`Processing ${index}: ${url}`);
|
|
121
|
+
* const data = await fetch(url);
|
|
122
|
+
* await processData(data);
|
|
123
|
+
* console.log(`Completed ${index}: ${url}`);
|
|
92
124
|
* });
|
|
93
|
-
* console.log(
|
|
94
|
-
* ```
|
|
125
|
+
* console.log("All URLs processed sequentially");
|
|
95
126
|
*
|
|
96
127
|
* @template Input - The type of elements in the input array
|
|
97
128
|
* @param elements - The readonly array to process
|
|
98
129
|
* @param closure - The asynchronous function to execute for each element
|
|
99
130
|
* @returns A Promise<void> that resolves when all operations complete
|
|
131
|
+
* @evidence contracts/common.md#principled-implementation The initial array length determines the visited indices, and each callback receives the current element, index and original array. Each call is awaited before advancing, so rejection ends iteration and propagates.
|
|
132
|
+
* @evidence contracts/common.md#clear-and-simple-design One sequential loop discards callback results without collecting an unused array.
|
|
133
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The closure receives every element exactly once and no error is swallowed.
|
|
134
|
+
* @evidence contracts/common.md#meaningful-documentation The comment contrasts it with the native forEach, states the sequential semantics, and gives an example.
|
|
100
135
|
*/
|
|
101
136
|
ArrayUtil.asyncForEach = (elements, closure) => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
|
|
137
|
+
const length = elements.length;
|
|
138
|
+
for (let index = 0; index < length; ++index)
|
|
139
|
+
yield closure(elements[index], index, elements);
|
|
103
140
|
});
|
|
104
141
|
/**
|
|
105
142
|
* Transforms each element of an array using an asynchronous function to
|
|
@@ -107,27 +144,40 @@ var ArrayUtil;
|
|
|
107
144
|
*
|
|
108
145
|
* Similar to JavaScript's native map but processes asynchronous functions
|
|
109
146
|
* sequentially. Each element's transformation is completed before proceeding
|
|
110
|
-
* to the next element, ensuring order is maintained.
|
|
111
|
-
*
|
|
147
|
+
* to the next element, ensuring order is maintained.
|
|
148
|
+
*
|
|
149
|
+
* Processing cost: Each of N elements invokes its mapper once in sequence and
|
|
150
|
+
* stores one result: O(N) bookkeeping and output space.
|
|
151
|
+
*
|
|
152
|
+
* Resource ownership: The invocation awaits one callback at a time and
|
|
153
|
+
* retains only its partial output and current call; success transfers the
|
|
154
|
+
* output to the caller and rejection releases local state. Callback-owned
|
|
155
|
+
* external resources remain the callback owner’s responsibility.
|
|
112
156
|
*
|
|
113
157
|
* @example
|
|
114
|
-
* ```typescript
|
|
115
158
|
* const userIds = [1, 2, 3, 4, 5];
|
|
116
159
|
*
|
|
117
|
-
* const userDetails = await ArrayUtil.asyncMap(
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
160
|
+
* const userDetails = await ArrayUtil.asyncMap(
|
|
161
|
+
* userIds,
|
|
162
|
+
* async (id, index) => {
|
|
163
|
+
* console.log(
|
|
164
|
+
* `Fetching user ${id} (${index + 1}/${userIds.length})`,
|
|
165
|
+
* );
|
|
166
|
+
* const response = await fetch(`/api/users/${id}`);
|
|
167
|
+
* return await response.json();
|
|
168
|
+
* },
|
|
123
169
|
* );
|
|
124
|
-
* console.log(
|
|
125
|
-
* ```
|
|
170
|
+
* console.log("All users fetched:", userDetails);
|
|
126
171
|
*
|
|
127
172
|
* @template Input - The type of elements in the input array
|
|
173
|
+
* @template Output - The type of elements in the resulting array
|
|
128
174
|
* @param elements - The readonly array to transform
|
|
129
|
-
* @
|
|
130
|
-
*
|
|
175
|
+
* @param closure - The asynchronous function that transforms each element
|
|
176
|
+
* @returns A Promise resolving to the transformed array
|
|
177
|
+
* @evidence contracts/common.md#principled-implementation The closure result of each element is pushed in iteration order, so the output order equals the input order, which holds because the calls are sequential.
|
|
178
|
+
* @evidence contracts/common.md#clear-and-simple-design It composes `asyncForEach` with one push.
|
|
179
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts Every element is transformed by the caller's closure, with no memoization or skipping.
|
|
180
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the sequential order and the parameters, and the example calls the two-argument form the function has.
|
|
131
181
|
*/
|
|
132
182
|
ArrayUtil.asyncMap = (elements, closure) => __awaiter(this, void 0, void 0, function* () {
|
|
133
183
|
const ret = [];
|
|
@@ -144,29 +194,39 @@ var ArrayUtil;
|
|
|
144
194
|
* execution is performed sequentially, and all results are collected into an
|
|
145
195
|
* array.
|
|
146
196
|
*
|
|
197
|
+
* Processing cost: The callback runs once for each of N indices, in sequence,
|
|
198
|
+
* with O(N) result storage.
|
|
199
|
+
*
|
|
200
|
+
* Resource ownership: The invocation awaits one callback at a time and
|
|
201
|
+
* retains only its partial output and current call; success transfers the
|
|
202
|
+
* output to the caller and rejection releases local state. Callback-owned
|
|
203
|
+
* external resources remain the callback owner’s responsibility.
|
|
204
|
+
*
|
|
147
205
|
* @example
|
|
148
|
-
* ```typescript
|
|
149
206
|
* // Generate random data 5 times
|
|
150
207
|
* const randomData = await ArrayUtil.asyncRepeat(5, async (index) => {
|
|
151
|
-
* await new Promise(resolve => setTimeout(resolve, 100)); // Wait 0.1 seconds
|
|
208
|
+
* await new Promise((resolve) => setTimeout(resolve, 100)); // Wait 0.1 seconds
|
|
152
209
|
* return {
|
|
153
210
|
* id: index,
|
|
154
211
|
* value: Math.random(),
|
|
155
|
-
* timestamp: new Date().toISOString()
|
|
212
|
+
* timestamp: new Date().toISOString(),
|
|
156
213
|
* };
|
|
157
214
|
* });
|
|
158
|
-
* console.log(
|
|
159
|
-
* ```;
|
|
215
|
+
* console.log("Generated data:", randomData);
|
|
160
216
|
*
|
|
161
217
|
* @template T - The type of the result from each execution
|
|
162
|
-
* @param count - The number of times to repeat (
|
|
218
|
+
* @param count - The number of times to repeat (integer from 0 to 2^32 - 1)
|
|
163
219
|
* @param closure - The asynchronous function to execute repeatedly
|
|
164
220
|
* @returns A Promise resolving to an array of results
|
|
221
|
+
* @evidence contracts/common.md#principled-implementation A count outside the integer range 0 through 2^32 - 1 is refused with a `RangeError` before any callback, because JavaScript arrays cannot represent a larger length; otherwise the closure is awaited for each index from 0 to count minus one and the results are collected in order, so the cost is linear in the count with no index array.
|
|
222
|
+
* @evidence contracts/common.md#clear-and-simple-design One validation and one loop; the validator is shared with `repeat`.
|
|
223
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The loop is the direct implementation of the documented behavior, and an invalid count is an error, not a coerced number.
|
|
224
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the index range, the sequential execution, and the result, and the count must be a non-negative integer as documented.
|
|
165
225
|
*/
|
|
166
226
|
ArrayUtil.asyncRepeat = (count, closure) => __awaiter(this, void 0, void 0, function* () {
|
|
167
|
-
|
|
227
|
+
validate("asyncRepeat", count);
|
|
168
228
|
const output = [];
|
|
169
|
-
for (
|
|
229
|
+
for (let index = 0; index < count; ++index)
|
|
170
230
|
output.push(yield closure(index));
|
|
171
231
|
return output;
|
|
172
232
|
});
|
|
@@ -176,29 +236,40 @@ var ArrayUtil;
|
|
|
176
236
|
* Similar to JavaScript's native some() method. Returns true immediately when
|
|
177
237
|
* the first element satisfying the condition is found.
|
|
178
238
|
*
|
|
239
|
+
* Processing cost: Native some stops at the first matching element:
|
|
240
|
+
* worst-case N predicate calls and constant auxiliary storage.
|
|
241
|
+
*
|
|
179
242
|
* @example
|
|
180
|
-
* ```typescript
|
|
181
243
|
* const numbers = [1, 3, 5, 7, 8, 9];
|
|
182
244
|
* const products = [
|
|
183
|
-
* { name:
|
|
184
|
-
* { name:
|
|
185
|
-
* { name:
|
|
245
|
+
* { name: "Apple", price: 100, inStock: true },
|
|
246
|
+
* { name: "Banana", price: 50, inStock: false },
|
|
247
|
+
* { name: "Orange", price: 80, inStock: true },
|
|
186
248
|
* ];
|
|
187
249
|
*
|
|
188
|
-
* const hasEvenNumber = ArrayUtil.has(numbers, num => num % 2 === 0);
|
|
250
|
+
* const hasEvenNumber = ArrayUtil.has(numbers, (num) => num % 2 === 0);
|
|
189
251
|
* console.log(hasEvenNumber); // true (8 exists)
|
|
190
252
|
*
|
|
191
|
-
* const hasExpensiveItem = ArrayUtil.has(
|
|
253
|
+
* const hasExpensiveItem = ArrayUtil.has(
|
|
254
|
+
* products,
|
|
255
|
+
* (product) => product.price > 90,
|
|
256
|
+
* );
|
|
192
257
|
* console.log(hasExpensiveItem); // true (Apple costs 100)
|
|
193
258
|
*
|
|
194
|
-
* const hasOutOfStock = ArrayUtil.has(
|
|
259
|
+
* const hasOutOfStock = ArrayUtil.has(
|
|
260
|
+
* products,
|
|
261
|
+
* (product) => !product.inStock,
|
|
262
|
+
* );
|
|
195
263
|
* console.log(hasOutOfStock); // true (Banana is out of stock)
|
|
196
|
-
* ```;
|
|
197
264
|
*
|
|
198
265
|
* @template T - The type of elements in the array
|
|
199
266
|
* @param elements - The readonly array to check
|
|
200
267
|
* @param pred - The predicate function to test elements
|
|
201
268
|
* @returns Boolean indicating if any element satisfies the condition
|
|
269
|
+
* @evidence contracts/common.md#principled-implementation The native `some` implements the existential search and returns at the first satisfying element, and the predicate receives only the element.
|
|
270
|
+
* @evidence contracts/common.md#clear-and-simple-design A one-expression delegation that keeps the native short-circuit visible.
|
|
271
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The predicate alone decides membership.
|
|
272
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the short-circuit and gives examples.
|
|
202
273
|
*/
|
|
203
274
|
ArrayUtil.has = (elements, pred) => elements.some((elem) => pred(elem));
|
|
204
275
|
/**
|
|
@@ -208,17 +279,19 @@ var ArrayUtil;
|
|
|
208
279
|
* A synchronous repetition function that executes the given function for each
|
|
209
280
|
* index (from 0 to count-1) and collects the results into an array.
|
|
210
281
|
*
|
|
282
|
+
* Processing cost: The callback runs once for each of N indices, with O(N)
|
|
283
|
+
* output storage.
|
|
284
|
+
*
|
|
211
285
|
* @example
|
|
212
|
-
* ```typescript
|
|
213
286
|
* // Generate an array of squares from 1 to 5
|
|
214
|
-
* const squares = ArrayUtil.repeat(5, index => (index + 1) ** 2);
|
|
287
|
+
* const squares = ArrayUtil.repeat(5, (index) => (index + 1) ** 2);
|
|
215
288
|
* console.log(squares); // [1, 4, 9, 16, 25]
|
|
216
289
|
*
|
|
217
290
|
* // Generate an array of default user objects
|
|
218
|
-
* const users = ArrayUtil.repeat(3, index => ({
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
291
|
+
* const users = ArrayUtil.repeat(3, (index) => ({
|
|
292
|
+
* id: index + 1,
|
|
293
|
+
* name: `User${index + 1}`,
|
|
294
|
+
* email: `user${index + 1}@example.com`,
|
|
222
295
|
* }));
|
|
223
296
|
* console.log(users);
|
|
224
297
|
* // [
|
|
@@ -226,55 +299,72 @@ var ArrayUtil;
|
|
|
226
299
|
* // { id: 2, name: 'User2', email: 'user2@example.com' },
|
|
227
300
|
* // { id: 3, name: 'User3', email: 'user3@example.com' }
|
|
228
301
|
* // ]
|
|
229
|
-
* ```
|
|
230
302
|
*
|
|
231
303
|
* @template T - The type of the result from each execution
|
|
232
|
-
* @param count - The number of times to repeat (
|
|
304
|
+
* @param count - The number of times to repeat (integer from 0 to 2^32 - 1)
|
|
233
305
|
* @param closure - The function to execute repeatedly
|
|
234
306
|
* @returns An array of results
|
|
307
|
+
* @evidence contracts/common.md#principled-implementation An invalid count is refused with a `RangeError`, and otherwise the closure is called for each index from 0 to count minus one and the results are collected in order, with cost linear in the count.
|
|
308
|
+
* @evidence contracts/common.md#clear-and-simple-design The synchronous counterpart of `asyncRepeat`, sharing its validator.
|
|
309
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The loop implements the documented behavior directly.
|
|
310
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the index range and the result, with examples.
|
|
235
311
|
*/
|
|
236
|
-
ArrayUtil.repeat = (count, closure) =>
|
|
312
|
+
ArrayUtil.repeat = (count, closure) => {
|
|
313
|
+
validate("repeat", count);
|
|
314
|
+
const output = [];
|
|
315
|
+
for (let index = 0; index < count; ++index)
|
|
316
|
+
output.push(closure(index));
|
|
317
|
+
return output;
|
|
318
|
+
};
|
|
237
319
|
/**
|
|
238
320
|
* Generates all possible subsets of a given array.
|
|
239
321
|
*
|
|
240
322
|
* Implements the mathematical concept of power set, generating 2^n subsets
|
|
241
323
|
* from an array of n elements. Uses depth-first search (DFS) algorithm to
|
|
242
|
-
* calculate all possible combinations of including or excluding each
|
|
243
|
-
* element
|
|
324
|
+
* calculate all possible combinations of including or excluding each element.
|
|
325
|
+
* Each element is included before it is excluded, so the first subset is the
|
|
326
|
+
* whole array and the last one is empty.
|
|
327
|
+
*
|
|
328
|
+
* Processing cost: The DFS visits the include/exclude tree and filters N
|
|
329
|
+
* input positions at each of 2^N leaves. This is O(N 2^N) time and
|
|
330
|
+
* materialized output space, the scale required by returning every subset,
|
|
331
|
+
* with O(N) recursion/mask state.
|
|
244
332
|
*
|
|
245
333
|
* @example
|
|
246
|
-
* ```typescript
|
|
247
334
|
* const numbers = [1, 2, 3];
|
|
248
335
|
* const allSubsets = ArrayUtil.subsets(numbers);
|
|
249
336
|
* console.log(allSubsets);
|
|
250
337
|
* // [
|
|
251
|
-
* // [],
|
|
252
|
-
* // [3], // {3}
|
|
253
|
-
* // [2], // {2}
|
|
254
|
-
* // [2, 3], // {2, 3}
|
|
255
|
-
* // [1], // {1}
|
|
256
|
-
* // [1, 3], // {1, 3}
|
|
338
|
+
* // [1, 2, 3], // {1, 2, 3}
|
|
257
339
|
* // [1, 2], // {1, 2}
|
|
258
|
-
* // [1,
|
|
340
|
+
* // [1, 3], // {1, 3}
|
|
341
|
+
* // [1], // {1}
|
|
342
|
+
* // [2, 3], // {2, 3}
|
|
343
|
+
* // [2], // {2}
|
|
344
|
+
* // [3], // {3}
|
|
345
|
+
* // [] // empty set
|
|
259
346
|
* // ]
|
|
260
347
|
*
|
|
261
|
-
* const colors = [
|
|
348
|
+
* const colors = ["red", "blue"];
|
|
262
349
|
* const colorSubsets = ArrayUtil.subsets(colors);
|
|
263
350
|
* console.log(colorSubsets);
|
|
264
351
|
* // [
|
|
265
|
-
* // [],
|
|
266
|
-
* // ['blue'],
|
|
352
|
+
* // ['red', 'blue'],
|
|
267
353
|
* // ['red'],
|
|
268
|
-
* // ['
|
|
354
|
+
* // ['blue'],
|
|
355
|
+
* // []
|
|
269
356
|
* // ]
|
|
270
357
|
*
|
|
271
358
|
* // Warning: Result size grows exponentially with array size
|
|
272
359
|
* // Example: 10 elements → 1,024 subsets, 20 elements → 1,048,576 subsets
|
|
273
|
-
* ```;
|
|
274
360
|
*
|
|
275
361
|
* @template T - The type of elements in the array
|
|
276
362
|
* @param array - The array to generate subsets from
|
|
277
363
|
* @returns An array containing all possible subsets
|
|
364
|
+
* @evidence contracts/common.md#principled-implementation A depth-first search decides for each position whether the element is in the subset, including it first, and each leaf materializes the chosen elements with `filter`, so the 2^n subsets appear in a fixed order, the whole array first and the empty array last; the recursion depth is the array length and the output size is exponential, which bounds the useful input size.
|
|
365
|
+
* @evidence contracts/common.md#clear-and-simple-design One recursive function over a mask, with no options.
|
|
366
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts It enumerates every subset without sampling or a size cap, and the array is not mutated.
|
|
367
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the power-set meaning, the exact enumeration order, and the exponential growth warning, and the examples show that order.
|
|
278
368
|
*/
|
|
279
369
|
ArrayUtil.subsets = (array) => {
|
|
280
370
|
const check = new Array(array.length).fill(false);
|
|
@@ -292,5 +382,9 @@ var ArrayUtil;
|
|
|
292
382
|
dfs(0);
|
|
293
383
|
return output;
|
|
294
384
|
};
|
|
385
|
+
const validate = (method, count) => {
|
|
386
|
+
if (!Number.isSafeInteger(count) || count < 0 || count > 0xffffffff)
|
|
387
|
+
throw new RangeError(`ArrayUtil.${method}(): count must be a non-negative integer.`);
|
|
388
|
+
};
|
|
295
389
|
})(ArrayUtil || (exports.ArrayUtil = ArrayUtil = {}));
|
|
296
390
|
//# sourceMappingURL=ArrayUtil.js.map
|
package/lib/ArrayUtil.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ArrayUtil.js","sourceRoot":"","sources":["../src/ArrayUtil.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA
|
|
1
|
+
{"version":3,"file":"ArrayUtil.js","sourceRoot":"","sources":["../src/ArrayUtil.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,IAAiB,SAAS,CA8XzB;AA9XD,WAAiB,SAAS;IACxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAyCG;IACU,qBAAW,GAAG,CACzB,QAA0B,EAC1B,IAIqB,EACH,EAAE;QACpB,MAAM,GAAG,GAAY,EAAE,CAAC;QACxB,MAAM,sBAAY,CAAC,QAAQ,EAAE,CAAO,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;YACxD,MAAM,IAAI,GAAY,MAAM,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK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