@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/RandomGenerator.d.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* RandomGenerator provides a collection of functions for generating random data
|
|
5
5
|
* including strings, names, content, dates, and array sampling. All functions
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* draw from `Math.random()`, so they are not seeded and produce varied output
|
|
7
|
+
* across executions, which suits testing scenarios that need arbitrary data.
|
|
8
8
|
*
|
|
9
9
|
* The namespace includes specialized generators for:
|
|
10
10
|
*
|
|
@@ -13,21 +13,33 @@
|
|
|
13
13
|
* - Date ranges and time-based data
|
|
14
14
|
* - Array sampling and element selection
|
|
15
15
|
*
|
|
16
|
+
* Processing cost: Text construction scales with generated characters, sparse
|
|
17
|
+
* sampling with selected positions, and fixed-size draws use constant work. The
|
|
18
|
+
* namespace retains only fixed character tables; each generator owns its
|
|
19
|
+
* invocation-local result.
|
|
20
|
+
*
|
|
16
21
|
* @author Jeongho Nam - https://github.com/samchon
|
|
17
22
|
* @example
|
|
18
|
-
* ```typescript
|
|
19
23
|
* // Generate test user data
|
|
20
24
|
* const testUser = {
|
|
21
25
|
* id: RandomGenerator.alphaNumeric(8),
|
|
22
26
|
* name: RandomGenerator.name(),
|
|
23
|
-
* bio: RandomGenerator.paragraph({
|
|
27
|
+
* bio: RandomGenerator.paragraph({
|
|
28
|
+
* sentences: 3,
|
|
29
|
+
* wordMin: 5,
|
|
30
|
+
* wordMax: 10,
|
|
31
|
+
* }),
|
|
24
32
|
* phone: RandomGenerator.mobile(),
|
|
25
|
-
* createdAt: RandomGenerator.date(new Date(), 1000 * 60 * 60 * 24 * 30) // 30 days
|
|
33
|
+
* createdAt: RandomGenerator.date(new Date(), 1000 * 60 * 60 * 24 * 30), // 30 days
|
|
26
34
|
* };
|
|
27
35
|
*
|
|
28
36
|
* // Sample data for testing
|
|
29
37
|
* const testSample = RandomGenerator.sample(allUsers, 5);
|
|
30
|
-
*
|
|
38
|
+
*
|
|
39
|
+
* @evidence contracts/common.md#principled-implementation The private integer helper partitions Math.random output into equally sized bins for finite ordered integer bounds, and generators compose text, dates and selections from those draws. Math.random is approximately uniform and not cryptographically secure; exact probability guarantees do not follow from its finite precision.
|
|
40
|
+
* @evidence contracts/common.md#clear-and-simple-design Small generators build on each other (`name` on `paragraph`, `content` on `paragraph`, both on `alphabets`), so one integer helper decides the randomness.
|
|
41
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The data are drawn, not taken from a fixed list of test values.
|
|
42
|
+
* @evidence contracts/common.md#meaningful-documentation The namespace prose says the functions are not seeded and lists the kinds of data, with an example.
|
|
31
43
|
*/
|
|
32
44
|
export declare namespace RandomGenerator {
|
|
33
45
|
/**
|
|
@@ -39,10 +51,12 @@ export declare namespace RandomGenerator {
|
|
|
39
51
|
* appear multiple times. Useful for generating random identifiers, test
|
|
40
52
|
* names, or placeholder text.
|
|
41
53
|
*
|
|
54
|
+
* Processing cost: Constructing L characters costs O(L) time and
|
|
55
|
+
* intermediate/result space.
|
|
56
|
+
*
|
|
42
57
|
* @example
|
|
43
|
-
*
|
|
44
|
-
* RandomGenerator.alphabets(
|
|
45
|
-
* RandomGenerator.alphabets(3); // e.g. "mzr"
|
|
58
|
+
* RandomGenerator.alphabets(5); // e.g. "kxqpw"
|
|
59
|
+
* RandomGenerator.alphabets(3); // e.g. "mzr"
|
|
46
60
|
* RandomGenerator.alphabets(10); // e.g. "qwertasdfg"
|
|
47
61
|
*
|
|
48
62
|
* // Generate random CSS class names
|
|
@@ -50,10 +64,13 @@ export declare namespace RandomGenerator {
|
|
|
50
64
|
*
|
|
51
65
|
* // Create random variable names for testing
|
|
52
66
|
* const varName = RandomGenerator.alphabets(8);
|
|
53
|
-
* ```
|
|
54
67
|
*
|
|
55
68
|
* @param length - The desired length of the generated alphabetic string
|
|
56
69
|
* @returns A string containing only lowercase letters of the specified length
|
|
70
|
+
* @evidence contracts/common.md#principled-implementation Each requested character maps a Math.random draw to one of 26 lowercase letters. Repeats are possible, and distribution inherits Math.random rather than guaranteeing exact uniformity.
|
|
71
|
+
* @evidence contracts/common.md#clear-and-simple-design One expression over the shared integer helper.
|
|
72
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The characters are drawn, not enumerated from a list of known values.
|
|
73
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the character set, the independence of the draws, and examples.
|
|
57
74
|
*/
|
|
58
75
|
const alphabets: (length: number) => string;
|
|
59
76
|
/**
|
|
@@ -65,9 +82,11 @@ export declare namespace RandomGenerator {
|
|
|
65
82
|
* character set. Ideal for generating random IDs, tokens, passwords, or
|
|
66
83
|
* unique identifiers that need both numeric and alphabetic characters.
|
|
67
84
|
*
|
|
85
|
+
* Processing cost: Constructing L characters costs O(L) time and
|
|
86
|
+
* intermediate/result space.
|
|
87
|
+
*
|
|
68
88
|
* @example
|
|
69
|
-
*
|
|
70
|
-
* RandomGenerator.alphaNumeric(8); // e.g. "a1b2c3d4"
|
|
89
|
+
* RandomGenerator.alphaNumeric(8); // e.g. "a1b2c3d4"
|
|
71
90
|
* RandomGenerator.alphaNumeric(12); // e.g. "x9y8z7w6v5u4"
|
|
72
91
|
*
|
|
73
92
|
* // Generate random API keys
|
|
@@ -78,11 +97,14 @@ export declare namespace RandomGenerator {
|
|
|
78
97
|
*
|
|
79
98
|
* // Generate test database IDs
|
|
80
99
|
* const testId = RandomGenerator.alphaNumeric(10);
|
|
81
|
-
* ```
|
|
82
100
|
*
|
|
83
101
|
* @param length - The desired length of the generated alphanumeric string
|
|
84
102
|
* @returns A string containing digits and lowercase letters of the specified
|
|
85
103
|
* length
|
|
104
|
+
* @evidence contracts/common.md#principled-implementation Each character maps a Math.random draw to one of ten digits and 26 lowercase letters. Repeated characters are allowed and exact uniformity is not guaranteed.
|
|
105
|
+
* @evidence contracts/common.md#clear-and-simple-design One expression over the shared integer helper.
|
|
106
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The characters are drawn, not enumerated from a list of known values.
|
|
107
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the character set and gives examples.
|
|
86
108
|
*/
|
|
87
109
|
const alphaNumeric: (length: number) => string;
|
|
88
110
|
/**
|
|
@@ -93,26 +115,31 @@ export declare namespace RandomGenerator {
|
|
|
93
115
|
* Each word is between 3-7 characters by default, creating realistic-looking
|
|
94
116
|
* names.
|
|
95
117
|
*
|
|
118
|
+
* Processing cost: The delegated paragraph work scales with the requested
|
|
119
|
+
* word count and total generated characters.
|
|
120
|
+
*
|
|
96
121
|
* @example
|
|
97
|
-
*
|
|
98
|
-
* RandomGenerator.name();
|
|
99
|
-
* RandomGenerator.name(
|
|
100
|
-
* RandomGenerator.name(3); // e.g. "sit amet consectetur" (3 words)
|
|
122
|
+
* RandomGenerator.name(); // e.g. "lorem ipsum" (2-3 words)
|
|
123
|
+
* RandomGenerator.name(1); // e.g. "dolor" (single word)
|
|
124
|
+
* RandomGenerator.name(3); // e.g. "sit amet consectetur" (3 words)
|
|
101
125
|
*
|
|
102
126
|
* // Generate test user names
|
|
103
127
|
* const users = Array.from({ length: 10 }, () => ({
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
128
|
+
* id: RandomGenerator.alphaNumeric(8),
|
|
129
|
+
* name: RandomGenerator.name(),
|
|
130
|
+
* email: `${RandomGenerator.name(1)}@test.com`,
|
|
107
131
|
* }));
|
|
108
132
|
*
|
|
109
133
|
* // Create random author names for blog posts
|
|
110
134
|
* const authorName = RandomGenerator.name();
|
|
111
|
-
* ```
|
|
112
135
|
*
|
|
113
136
|
* @param length - Number of words in the name (default: random between 2-3)
|
|
114
137
|
* @returns A space-separated string of random words (each 3-7 chars by
|
|
115
138
|
* default)
|
|
139
|
+
* @evidence contracts/common.md#principled-implementation A name is a short paragraph of two or three lowercase words by default, so it reads as a person or entity name without an external name list.
|
|
140
|
+
* @evidence contracts/common.md#clear-and-simple-design A one-line delegation to `paragraph` with the sentence count as the word count.
|
|
141
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The words are drawn letters, not a fixed list of names.
|
|
142
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the default length and gives examples.
|
|
116
143
|
*/
|
|
117
144
|
const name: (length?: number) => string;
|
|
118
145
|
/**
|
|
@@ -123,13 +150,16 @@ export declare namespace RandomGenerator {
|
|
|
123
150
|
* joined with spaces. Accepts an optional configuration object for fine-tuned
|
|
124
151
|
* control over the paragraph structure.
|
|
125
152
|
*
|
|
153
|
+
* Processing cost: Each requested word is generated once; time and temporary
|
|
154
|
+
* space scale with the total generated character count and the number of
|
|
155
|
+
* words.
|
|
156
|
+
*
|
|
126
157
|
* @example
|
|
127
|
-
* ```typescript
|
|
128
158
|
* // Generate with defaults (random 2-5 words, 3-7 characters each)
|
|
129
|
-
* RandomGenerator.paragraph();
|
|
159
|
+
* RandomGenerator.paragraph(); // e.g. "lorem ipsum dolor"
|
|
130
160
|
*
|
|
131
161
|
* // Specific number of sentences
|
|
132
|
-
* RandomGenerator.paragraph({ sentences: 5 });
|
|
162
|
+
* RandomGenerator.paragraph({ sentences: 5 }); // "lorem ipsum dolor sit amet"
|
|
133
163
|
*
|
|
134
164
|
* // Custom word length ranges
|
|
135
165
|
* RandomGenerator.paragraph({ sentences: 4, wordMin: 2, wordMax: 5 });
|
|
@@ -139,20 +169,23 @@ export declare namespace RandomGenerator {
|
|
|
139
169
|
* const description = RandomGenerator.paragraph({
|
|
140
170
|
* sentences: 8,
|
|
141
171
|
* wordMin: 4,
|
|
142
|
-
* wordMax: 8
|
|
172
|
+
* wordMax: 8,
|
|
143
173
|
* });
|
|
144
174
|
*
|
|
145
175
|
* // Create test content for forms
|
|
146
176
|
* const placeholder = RandomGenerator.paragraph({
|
|
147
177
|
* sentences: 3,
|
|
148
178
|
* wordMin: 5,
|
|
149
|
-
* wordMax: 10
|
|
179
|
+
* wordMax: 10,
|
|
150
180
|
* });
|
|
151
|
-
* ```;
|
|
152
181
|
*
|
|
153
182
|
* @param props - Optional configuration object with sentences count and word
|
|
154
183
|
* length ranges
|
|
155
184
|
* @returns A string containing the generated paragraph
|
|
185
|
+
* @evidence contracts/common.md#principled-implementation A paragraph is a number of words joined by spaces, each word a random lowercase string of a length between the minimum and maximum, with the defaults of two to five words of three to seven letters.
|
|
186
|
+
* @evidence contracts/common.md#clear-and-simple-design One expression over `alphabets`, with three optional numbers.
|
|
187
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The words are drawn letters, not a fixed text.
|
|
188
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the parameters and their defaults, with examples.
|
|
156
189
|
*/
|
|
157
190
|
const paragraph: (props?: Partial<{
|
|
158
191
|
sentences: number;
|
|
@@ -168,8 +201,10 @@ export declare namespace RandomGenerator {
|
|
|
168
201
|
* character lengths. Ideal for generating realistic-looking text content for
|
|
169
202
|
* testing.
|
|
170
203
|
*
|
|
204
|
+
* Processing cost: Every paragraph is generated once; time and temporary
|
|
205
|
+
* space scale with the total output characters and paragraph/word counts.
|
|
206
|
+
*
|
|
171
207
|
* @example
|
|
172
|
-
* ```typescript
|
|
173
208
|
* // Generate with all defaults
|
|
174
209
|
* const article = RandomGenerator.content();
|
|
175
210
|
*
|
|
@@ -179,7 +214,7 @@ export declare namespace RandomGenerator {
|
|
|
179
214
|
* sentenceMin: 15,
|
|
180
215
|
* sentenceMax: 25,
|
|
181
216
|
* wordMin: 4,
|
|
182
|
-
* wordMax: 8
|
|
217
|
+
* wordMax: 8,
|
|
183
218
|
* });
|
|
184
219
|
*
|
|
185
220
|
* // Short content with brief sentences
|
|
@@ -188,7 +223,7 @@ export declare namespace RandomGenerator {
|
|
|
188
223
|
* sentenceMin: 5,
|
|
189
224
|
* sentenceMax: 8,
|
|
190
225
|
* wordMin: 2,
|
|
191
|
-
* wordMax: 4
|
|
226
|
+
* wordMax: 4,
|
|
192
227
|
* });
|
|
193
228
|
*
|
|
194
229
|
* // Generate blog post content
|
|
@@ -199,9 +234,9 @@ export declare namespace RandomGenerator {
|
|
|
199
234
|
* sentenceMin: 10,
|
|
200
235
|
* sentenceMax: 20,
|
|
201
236
|
* wordMin: 3,
|
|
202
|
-
* wordMax: 7
|
|
237
|
+
* wordMax: 7,
|
|
203
238
|
* }),
|
|
204
|
-
* summary: RandomGenerator.paragraph({ sentences: 2 })
|
|
239
|
+
* summary: RandomGenerator.paragraph({ sentences: 2 }),
|
|
205
240
|
* };
|
|
206
241
|
*
|
|
207
242
|
* // Create test data for CMS
|
|
@@ -210,14 +245,17 @@ export declare namespace RandomGenerator {
|
|
|
210
245
|
* content: RandomGenerator.content({
|
|
211
246
|
* paragraphs: randint(2, 6),
|
|
212
247
|
* sentenceMin: 8,
|
|
213
|
-
* sentenceMax: 15
|
|
214
|
-
* })
|
|
248
|
+
* sentenceMax: 15,
|
|
249
|
+
* }),
|
|
215
250
|
* }));
|
|
216
|
-
* ```;
|
|
217
251
|
*
|
|
218
252
|
* @param props - Optional configuration object with paragraph, sentence, and
|
|
219
253
|
* word parameters
|
|
220
254
|
* @returns A string containing the generated multi-paragraph content
|
|
255
|
+
* @evidence contracts/common.md#principled-implementation The content has three to eight paragraphs by default, each containing ten to forty space-separated words of one to seven lowercase letters, joined by blank lines. The legacy sentence-named options count words, not grammatical sentences.
|
|
256
|
+
* @evidence contracts/common.md#clear-and-simple-design One expression over `paragraph`, with five optional numbers.
|
|
257
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The text is drawn, not a fixed corpus.
|
|
258
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the parameters and their defaults, with examples.
|
|
221
259
|
*/
|
|
222
260
|
const content: (props?: Partial<{
|
|
223
261
|
paragraphs: number;
|
|
@@ -234,13 +272,15 @@ export declare namespace RandomGenerator {
|
|
|
234
272
|
* Automatically trims whitespace from the beginning and end of the result.
|
|
235
273
|
* Useful for creating excerpts, search terms, or partial content samples.
|
|
236
274
|
*
|
|
275
|
+
* Processing cost: Two draws choose the slice bounds; substring and trimming
|
|
276
|
+
* cost at most O(N) in the input length.
|
|
277
|
+
*
|
|
237
278
|
* @example
|
|
238
|
-
* ```typescript
|
|
239
279
|
* const text = "The quick brown fox jumps over the lazy dog";
|
|
240
280
|
*
|
|
241
|
-
* RandomGenerator.substring(text);
|
|
242
|
-
* RandomGenerator.substring(text);
|
|
243
|
-
* RandomGenerator.substring(text);
|
|
281
|
+
* RandomGenerator.substring(text); // e.g. "quick brown fox"
|
|
282
|
+
* RandomGenerator.substring(text); // e.g. "jumps over"
|
|
283
|
+
* RandomGenerator.substring(text); // e.g. "fox jumps over the lazy"
|
|
244
284
|
*
|
|
245
285
|
* // Generate search terms from content
|
|
246
286
|
* const searchQuery = RandomGenerator.substring(articleContent);
|
|
@@ -253,10 +293,13 @@ export declare namespace RandomGenerator {
|
|
|
253
293
|
*
|
|
254
294
|
* // Create random selections for highlight testing
|
|
255
295
|
* const selectedText = RandomGenerator.substring(documentContent);
|
|
256
|
-
* ```;
|
|
257
296
|
*
|
|
258
297
|
* @param content - The source string to extract a substring from
|
|
259
298
|
* @returns A trimmed substring of the original content
|
|
299
|
+
* @evidence contracts/common.md#principled-implementation For nonempty input, the first index is a valid position and the last is strictly later up to the length. Trimming can empty a whitespace slice. Empty input also returns an empty string; substring clamps its drawn bounds to the empty input.
|
|
300
|
+
* @evidence contracts/common.md#clear-and-simple-design Two draws and one slice.
|
|
301
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The slice bounds are drawn, not fixed.
|
|
302
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states that the slice is random and trimmed, with examples.
|
|
260
303
|
*/
|
|
261
304
|
const substring: (content: string) => string;
|
|
262
305
|
/**
|
|
@@ -268,31 +311,39 @@ export declare namespace RandomGenerator {
|
|
|
268
311
|
* if necessary. Commonly used for generating Korean mobile phone numbers or
|
|
269
312
|
* similar formats.
|
|
270
313
|
*
|
|
314
|
+
* Processing cost: Two bounded integer draws and fixed-width decimal
|
|
315
|
+
* formatting use constant work; joining the caller prefix costs O(P) output
|
|
316
|
+
* time and space for P prefix characters.
|
|
317
|
+
*
|
|
271
318
|
* @example
|
|
272
|
-
*
|
|
273
|
-
* RandomGenerator.mobile();
|
|
274
|
-
* RandomGenerator.mobile("
|
|
275
|
-
* RandomGenerator.mobile("+82"); // e.g. "+823341234" or "+8212345678"
|
|
319
|
+
* RandomGenerator.mobile(); // e.g. "0103341234" or "01012345678"
|
|
320
|
+
* RandomGenerator.mobile("011"); // e.g. "0119876543" or "01112345678"
|
|
321
|
+
* RandomGenerator.mobile("+82"); // e.g. "+823341234" or "+8212345678"
|
|
276
322
|
*
|
|
277
323
|
* // Generate test user phone numbers
|
|
278
324
|
* const testUsers = Array.from({ length: 100 }, () => ({
|
|
279
325
|
* name: RandomGenerator.name(),
|
|
280
326
|
* phone: RandomGenerator.mobile(),
|
|
281
|
-
* altPhone: RandomGenerator.mobile("011")
|
|
327
|
+
* altPhone: RandomGenerator.mobile("011"),
|
|
282
328
|
* }));
|
|
283
329
|
*
|
|
284
330
|
* // Create international phone numbers
|
|
285
331
|
* const internationalPhone = RandomGenerator.mobile("+821");
|
|
286
332
|
*
|
|
287
333
|
* // Generate contact list for testing
|
|
288
|
-
* const contacts = ["010", "011", "016", "017", "018", "019"].map(
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
334
|
+
* const contacts = ["010", "011", "016", "017", "018", "019"].map(
|
|
335
|
+
* (prefix) => ({
|
|
336
|
+
* carrier: prefix,
|
|
337
|
+
* number: RandomGenerator.mobile(prefix),
|
|
338
|
+
* }),
|
|
339
|
+
* );
|
|
293
340
|
*
|
|
294
341
|
* @param prefix - The prefix string for the phone number (default: "010")
|
|
295
342
|
* @returns A formatted mobile phone number string
|
|
343
|
+
* @evidence contracts/common.md#principled-implementation A mobile number is the prefix, a middle group of three digits when the drawn value is below 1,000 and four digits otherwise, and a four-digit group, all zero-padded, which matches the 010-XXX-XXXX and 010-XXXX-XXXX Korean formats the default prefix implies.
|
|
344
|
+
* @evidence contracts/common.md#clear-and-simple-design One expression with two draws.
|
|
345
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The prefix is a parameter whose default is the documented Korean one, and the digits are drawn.
|
|
346
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the format, the default prefix, and examples.
|
|
296
347
|
*/
|
|
297
348
|
const mobile: (prefix?: string) => string;
|
|
298
349
|
/**
|
|
@@ -303,8 +354,10 @@ export declare namespace RandomGenerator {
|
|
|
303
354
|
* date. Useful for generating timestamps, creation dates, or scheduling test
|
|
304
355
|
* data.
|
|
305
356
|
*
|
|
357
|
+
* Processing cost: One integer draw and one Date construction use constant
|
|
358
|
+
* work.
|
|
359
|
+
*
|
|
306
360
|
* @example
|
|
307
|
-
* ```typescript
|
|
308
361
|
* const now = new Date();
|
|
309
362
|
* const oneDay = 24 * 60 * 60 * 1000;
|
|
310
363
|
* const oneMonth = 30 * oneDay;
|
|
@@ -318,38 +371,50 @@ export declare namespace RandomGenerator {
|
|
|
318
371
|
*
|
|
319
372
|
* // Generate random creation dates for test data
|
|
320
373
|
* const startOfYear = new Date(2024, 0, 1);
|
|
321
|
-
* const endOfYear =
|
|
322
|
-
*
|
|
374
|
+
* const endOfYear =
|
|
375
|
+
* new Date(2024, 11, 31).getTime() - startOfYear.getTime();
|
|
376
|
+
* const randomCreationDate = RandomGenerator.date(
|
|
377
|
+
* startOfYear,
|
|
378
|
+
* endOfYear,
|
|
379
|
+
* );
|
|
323
380
|
*
|
|
324
381
|
* // Create test events with random timestamps
|
|
325
382
|
* const events = Array.from({ length: 50 }, () => ({
|
|
326
383
|
* id: RandomGenerator.alphaNumeric(8),
|
|
327
384
|
* title: RandomGenerator.name(2),
|
|
328
385
|
* createdAt: RandomGenerator.date(new Date(), oneMonth),
|
|
329
|
-
* scheduledFor: RandomGenerator.date(new Date(), oneMonth * 3)
|
|
386
|
+
* scheduledFor: RandomGenerator.date(new Date(), oneMonth * 3),
|
|
330
387
|
* }));
|
|
331
|
-
* ```;
|
|
332
388
|
*
|
|
333
389
|
* @param from - The starting date for the random range
|
|
334
390
|
* @param range - The range in milliseconds from the starting date
|
|
335
391
|
* @returns A random date within the specified range
|
|
392
|
+
* @evidence contracts/common.md#principled-implementation For a valid start date and finite nonnegative integer range whose endpoint is Date-representable, the result adds an integer offset from zero through range. Unsupported invalid dates or bounds are not validated and can produce an Invalid Date or an out-of-range result.
|
|
393
|
+
* @evidence contracts/common.md#clear-and-simple-design One expression over the shared integer helper.
|
|
394
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The offset is drawn, not fixed.
|
|
395
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the interval and the unit of the range, with examples.
|
|
336
396
|
*/
|
|
337
397
|
const date: (from: Date, range: number) => Date;
|
|
338
398
|
/**
|
|
339
|
-
* Randomly samples a specified number of
|
|
399
|
+
* Randomly samples a specified number of positions from an array.
|
|
340
400
|
*
|
|
341
|
-
* Selects
|
|
342
|
-
*
|
|
343
|
-
* at the array length to prevent errors.
|
|
344
|
-
*
|
|
345
|
-
*
|
|
401
|
+
* Selects input positions without replacement. Equal values at different
|
|
402
|
+
* positions remain separate candidates and can both appear in the result. The
|
|
403
|
+
* sample size is automatically capped at the array length to prevent errors.
|
|
404
|
+
* Uses a partial Fisher-Yates shuffle over a sparse index map, so every draw
|
|
405
|
+
* is final and the cost grows with the sample size, not with the array
|
|
406
|
+
* length. Ideal for creating test datasets or selecting random subsets for
|
|
407
|
+
* validation.
|
|
408
|
+
*
|
|
409
|
+
* Processing cost: For K selected positions, sparse partial Fisher-Yates uses
|
|
410
|
+
* K map lookups/updates and K output entries: expected O(K) time and O(K)
|
|
411
|
+
* space, without copying the N-element input.
|
|
346
412
|
*
|
|
347
413
|
* @example
|
|
348
|
-
* ```typescript
|
|
349
414
|
* const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
350
415
|
*
|
|
351
|
-
* RandomGenerator.sample(numbers, 3);
|
|
352
|
-
* RandomGenerator.sample(numbers, 5);
|
|
416
|
+
* RandomGenerator.sample(numbers, 3); // e.g. [2, 7, 9]
|
|
417
|
+
* RandomGenerator.sample(numbers, 5); // e.g. [1, 4, 6, 8, 10]
|
|
353
418
|
* RandomGenerator.sample(numbers, 15); // returns all 10 elements (capped at array length)
|
|
354
419
|
*
|
|
355
420
|
* // Sample users for testing
|
|
@@ -364,53 +429,61 @@ export declare namespace RandomGenerator {
|
|
|
364
429
|
*
|
|
365
430
|
* // Random A/B testing groups
|
|
366
431
|
* const groupA = RandomGenerator.sample(allParticipants, 50);
|
|
367
|
-
* const remaining = allParticipants.filter(p => !groupA.includes(p));
|
|
432
|
+
* const remaining = allParticipants.filter((p) => !groupA.includes(p));
|
|
368
433
|
* const groupB = RandomGenerator.sample(remaining, 50);
|
|
369
|
-
* ```;
|
|
370
434
|
*
|
|
371
435
|
* @param array - The source array to sample from
|
|
372
436
|
* @param count - The number of elements to sample
|
|
373
437
|
* @returns An array containing the randomly selected elements
|
|
438
|
+
* @evidence contracts/common.md#principled-implementation A partial Fisher-Yates shuffle over a sparse map of moved positions draws each result element from the not-yet-chosen positions uniformly, so the sample is without replacement, approximately uniform under Math.random, and its cost grows with the sample size; the count is capped at the array length, a positive fractional count rounds upward through the loop condition, and a count that is zero, negative, or NaN yields an empty array.
|
|
439
|
+
* @evidence contracts/common.md#clear-and-simple-design One loop with one map, and no rejection of repeated draws.
|
|
440
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The array is not mutated or copied, and no element is favored or excluded.
|
|
441
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states without-replacement sampling, the cap, and the algorithm, with examples.
|
|
374
442
|
*/
|
|
375
443
|
const sample: <T>(array: T[], count: number) => T[];
|
|
376
444
|
/**
|
|
377
445
|
* Randomly selects a single element from an array.
|
|
378
446
|
*
|
|
379
|
-
* Chooses one element at random from the provided array using
|
|
380
|
-
* distribution
|
|
381
|
-
*
|
|
382
|
-
*
|
|
447
|
+
* Chooses one element at random from the provided array using approximately
|
|
448
|
+
* uniform distribution inherited from Math.random. This is a convenience
|
|
449
|
+
* function equivalent to sampling with a count of 1, but returns the element
|
|
450
|
+
* directly rather than an array containing one element.
|
|
451
|
+
*
|
|
452
|
+
* Processing cost: One bounded draw and one indexed lookup use constant work.
|
|
383
453
|
*
|
|
384
454
|
* @example
|
|
385
|
-
*
|
|
386
|
-
* const
|
|
387
|
-
* const fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
|
|
455
|
+
* const colors = ["red", "blue", "green", "yellow", "purple"];
|
|
456
|
+
* const fruits = ["apple", "banana", "orange", "grape", "kiwi"];
|
|
388
457
|
*
|
|
389
|
-
* RandomGenerator.pick(colors);
|
|
390
|
-
* RandomGenerator.pick(fruits);
|
|
458
|
+
* RandomGenerator.pick(colors); // e.g. "blue"
|
|
459
|
+
* RandomGenerator.pick(fruits); // e.g. "apple"
|
|
391
460
|
*
|
|
392
461
|
* // Select random configuration options
|
|
393
|
-
* const randomTheme = RandomGenerator.pick([
|
|
394
|
-
* const randomLocale = RandomGenerator.pick([
|
|
462
|
+
* const randomTheme = RandomGenerator.pick(["light", "dark", "auto"]);
|
|
463
|
+
* const randomLocale = RandomGenerator.pick(["en", "ko", "ja", "zh"]);
|
|
395
464
|
*
|
|
396
465
|
* // Choose random test scenarios
|
|
397
466
|
* const testScenario = RandomGenerator.pick([
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
467
|
+
* "happy_path",
|
|
468
|
+
* "edge_case",
|
|
469
|
+
* "error_condition",
|
|
470
|
+
* "boundary_test",
|
|
402
471
|
* ]);
|
|
403
472
|
*
|
|
404
473
|
* // Random user role assignment
|
|
405
|
-
* const userRole = RandomGenerator.pick([
|
|
474
|
+
* const userRole = RandomGenerator.pick(["admin", "user", "moderator"]);
|
|
406
475
|
*
|
|
407
476
|
* // Select random API endpoints for testing
|
|
408
|
-
* const endpoints = [
|
|
477
|
+
* const endpoints = ["/users", "/posts", "/comments", "/categories"];
|
|
409
478
|
* const randomEndpoint = RandomGenerator.pick(endpoints);
|
|
410
|
-
* ```;
|
|
411
479
|
*
|
|
412
480
|
* @param array - The source array to pick an element from
|
|
413
481
|
* @returns A randomly selected element from the array
|
|
482
|
+
* @throws RangeError when the array is empty
|
|
483
|
+
* @evidence contracts/common.md#principled-implementation One index maps a Math.random draw to a valid position, giving approximately uniform selection; an empty array has no element to return, so it raises a `RangeError` instead of returning `undefined` under the element type.
|
|
484
|
+
* @evidence contracts/common.md#clear-and-simple-design One guard and one draw.
|
|
485
|
+
* @evidence contracts/common.md#prohibited-implementation-shortcuts The guard states the contract for the empty array and no default element is invented.
|
|
486
|
+
* @evidence contracts/common.md#meaningful-documentation The comment states the approximately uniform selection, the relation to sampling one element, and the `RangeError` for an empty array, with examples.
|
|
414
487
|
*/
|
|
415
488
|
const pick: <T>(array: readonly T[]) => T;
|
|
416
489
|
}
|