@nestia/e2e 11.0.0-dev.20260313 → 11.0.0-dev.20260313-3
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 +244 -0
- package/lib/ArrayUtil.js +296 -0
- package/lib/ArrayUtil.js.map +1 -0
- package/lib/DynamicExecutor.d.ts +144 -0
- package/lib/DynamicExecutor.js +176 -0
- package/lib/DynamicExecutor.js.map +1 -0
- package/lib/GaffComparator.d.ts +255 -0
- package/lib/GaffComparator.js +278 -0
- package/lib/GaffComparator.js.map +1 -0
- package/lib/MapUtil.d.ts +79 -0
- package/lib/MapUtil.js +92 -0
- package/lib/MapUtil.js.map +1 -0
- package/lib/RandomGenerator.d.ts +416 -0
- package/lib/RandomGenerator.js +464 -0
- package/lib/RandomGenerator.js.map +1 -0
- package/lib/TestValidator.d.ts +389 -0
- package/lib/TestValidator.js +495 -0
- package/lib/TestValidator.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +42 -0
- package/lib/index.js.map +1 -0
- package/lib/internal/json_equal_to.d.ts +1 -0
- package/lib/internal/json_equal_to.js +33 -0
- package/lib/internal/json_equal_to.js.map +1 -0
- package/lib/module.d.ts +6 -0
- package/lib/module.js +23 -0
- package/lib/module.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RandomGenerator = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Comprehensive random data generation utilities for testing and development.
|
|
6
|
+
*
|
|
7
|
+
* RandomGenerator provides a collection of functions for generating random data
|
|
8
|
+
* including strings, names, content, dates, and array sampling. All functions
|
|
9
|
+
* are designed to be deterministic within a single execution but produce varied
|
|
10
|
+
* output across different runs, making them ideal for testing scenarios.
|
|
11
|
+
*
|
|
12
|
+
* The namespace includes specialized generators for:
|
|
13
|
+
*
|
|
14
|
+
* - Text content (alphabets, alphanumeric, names, paragraphs)
|
|
15
|
+
* - Phone numbers and contact information
|
|
16
|
+
* - Date ranges and time-based data
|
|
17
|
+
* - Array sampling and element selection
|
|
18
|
+
*
|
|
19
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Generate test user data
|
|
23
|
+
* const testUser = {
|
|
24
|
+
* id: RandomGenerator.alphaNumeric(8),
|
|
25
|
+
* name: RandomGenerator.name(),
|
|
26
|
+
* bio: RandomGenerator.paragraph({ sentences: 3, wordMin: 5, wordMax: 10 }),
|
|
27
|
+
* phone: RandomGenerator.mobile(),
|
|
28
|
+
* createdAt: RandomGenerator.date(new Date(), 1000 * 60 * 60 * 24 * 30) // 30 days
|
|
29
|
+
* };
|
|
30
|
+
*
|
|
31
|
+
* // Sample data for testing
|
|
32
|
+
* const testSample = RandomGenerator.sample(allUsers, 5);
|
|
33
|
+
* ```;
|
|
34
|
+
*/
|
|
35
|
+
var RandomGenerator;
|
|
36
|
+
(function (RandomGenerator) {
|
|
37
|
+
/** Character set containing lowercase alphabetical characters a-z */
|
|
38
|
+
const CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
|
|
39
|
+
/**
|
|
40
|
+
* Character set containing digits (0-9) and lowercase alphabetical characters
|
|
41
|
+
* (a-z)
|
|
42
|
+
*/
|
|
43
|
+
const LETTERS = "0123456789" + CHARACTERS;
|
|
44
|
+
/**
|
|
45
|
+
* Generates a random string containing only lowercase alphabetical
|
|
46
|
+
* characters.
|
|
47
|
+
*
|
|
48
|
+
* Creates a string of specified length using only characters a-z. Each
|
|
49
|
+
* character is independently randomly selected, so the same character may
|
|
50
|
+
* appear multiple times. Useful for generating random identifiers, test
|
|
51
|
+
* names, or placeholder text.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* RandomGenerator.alphabets(5); // e.g. "kxqpw"
|
|
56
|
+
* RandomGenerator.alphabets(3); // e.g. "mzr"
|
|
57
|
+
* RandomGenerator.alphabets(10); // e.g. "qwertasdfg"
|
|
58
|
+
*
|
|
59
|
+
* // Generate random CSS class names
|
|
60
|
+
* const className = `test-${RandomGenerator.alphabets(6)}`;
|
|
61
|
+
*
|
|
62
|
+
* // Create random variable names for testing
|
|
63
|
+
* const varName = RandomGenerator.alphabets(8);
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @param length - The desired length of the generated alphabetic string
|
|
67
|
+
* @returns A string containing only lowercase letters of the specified length
|
|
68
|
+
*/
|
|
69
|
+
RandomGenerator.alphabets = (length) => new Array(length)
|
|
70
|
+
.fill("")
|
|
71
|
+
.map(() => CHARACTERS[randint(0, CHARACTERS.length - 1)])
|
|
72
|
+
.join("");
|
|
73
|
+
/**
|
|
74
|
+
* Generates a random alphanumeric string containing digits and lowercase
|
|
75
|
+
* letters.
|
|
76
|
+
*
|
|
77
|
+
* Creates a string of specified length using characters from 0-9 and a-z.
|
|
78
|
+
* Each position is independently randomly selected from the combined
|
|
79
|
+
* character set. Ideal for generating random IDs, tokens, passwords, or
|
|
80
|
+
* unique identifiers that need both numeric and alphabetic characters.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* RandomGenerator.alphaNumeric(8); // e.g. "a1b2c3d4"
|
|
85
|
+
* RandomGenerator.alphaNumeric(12); // e.g. "x9y8z7w6v5u4"
|
|
86
|
+
*
|
|
87
|
+
* // Generate random API keys
|
|
88
|
+
* const apiKey = RandomGenerator.alphaNumeric(32);
|
|
89
|
+
*
|
|
90
|
+
* // Create session tokens
|
|
91
|
+
* const sessionId = `sess_${RandomGenerator.alphaNumeric(16)}`;
|
|
92
|
+
*
|
|
93
|
+
* // Generate test database IDs
|
|
94
|
+
* const testId = RandomGenerator.alphaNumeric(10);
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @param length - The desired length of the generated alphanumeric string
|
|
98
|
+
* @returns A string containing digits and lowercase letters of the specified
|
|
99
|
+
* length
|
|
100
|
+
*/
|
|
101
|
+
RandomGenerator.alphaNumeric = (length) => new Array(length)
|
|
102
|
+
.fill("")
|
|
103
|
+
.map(() => LETTERS[randint(0, LETTERS.length - 1)])
|
|
104
|
+
.join("");
|
|
105
|
+
/**
|
|
106
|
+
* Generates a random name-like string with realistic length variation.
|
|
107
|
+
*
|
|
108
|
+
* Creates a name by generating a paragraph with 2-3 words (randomly chosen).
|
|
109
|
+
* The resulting string resembles typical human names in structure and length.
|
|
110
|
+
* Each word is between 3-7 characters by default, creating realistic-looking
|
|
111
|
+
* names.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* RandomGenerator.name(); // e.g. "lorem ipsum" (2-3 words)
|
|
116
|
+
* RandomGenerator.name(1); // e.g. "dolor" (single word)
|
|
117
|
+
* RandomGenerator.name(3); // e.g. "sit amet consectetur" (3 words)
|
|
118
|
+
*
|
|
119
|
+
* // Generate test user names
|
|
120
|
+
* const users = Array.from({ length: 10 }, () => ({
|
|
121
|
+
* id: RandomGenerator.alphaNumeric(8),
|
|
122
|
+
* name: RandomGenerator.name(),
|
|
123
|
+
* email: `${RandomGenerator.name(1)}@test.com`
|
|
124
|
+
* }));
|
|
125
|
+
*
|
|
126
|
+
* // Create random author names for blog posts
|
|
127
|
+
* const authorName = RandomGenerator.name();
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @param length - Number of words in the name (default: random between 2-3)
|
|
131
|
+
* @returns A space-separated string of random words (each 3-7 chars by
|
|
132
|
+
* default)
|
|
133
|
+
*/
|
|
134
|
+
RandomGenerator.name = (length = randint(2, 3)) => RandomGenerator.paragraph({
|
|
135
|
+
sentences: length,
|
|
136
|
+
});
|
|
137
|
+
/**
|
|
138
|
+
* Generates a random paragraph with configurable sentence structure.
|
|
139
|
+
*
|
|
140
|
+
* Creates a paragraph consisting of a specified number of "sentences"
|
|
141
|
+
* (words). Each sentence is a random alphabetic string, and sentences are
|
|
142
|
+
* joined with spaces. Accepts an optional configuration object for fine-tuned
|
|
143
|
+
* control over the paragraph structure.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* // Generate with defaults (random 2-5 words, 3-7 characters each)
|
|
148
|
+
* RandomGenerator.paragraph(); // e.g. "lorem ipsum dolor"
|
|
149
|
+
*
|
|
150
|
+
* // Specific number of sentences
|
|
151
|
+
* RandomGenerator.paragraph({ sentences: 5 }); // "lorem ipsum dolor sit amet"
|
|
152
|
+
*
|
|
153
|
+
* // Custom word length ranges
|
|
154
|
+
* RandomGenerator.paragraph({ sentences: 4, wordMin: 2, wordMax: 5 });
|
|
155
|
+
* // "ab cd ef gh"
|
|
156
|
+
*
|
|
157
|
+
* // Generate product descriptions
|
|
158
|
+
* const description = RandomGenerator.paragraph({
|
|
159
|
+
* sentences: 8,
|
|
160
|
+
* wordMin: 4,
|
|
161
|
+
* wordMax: 8
|
|
162
|
+
* });
|
|
163
|
+
*
|
|
164
|
+
* // Create test content for forms
|
|
165
|
+
* const placeholder = RandomGenerator.paragraph({
|
|
166
|
+
* sentences: 3,
|
|
167
|
+
* wordMin: 5,
|
|
168
|
+
* wordMax: 10
|
|
169
|
+
* });
|
|
170
|
+
* ```;
|
|
171
|
+
*
|
|
172
|
+
* @param props - Optional configuration object with sentences count and word
|
|
173
|
+
* length ranges
|
|
174
|
+
* @returns A string containing the generated paragraph
|
|
175
|
+
*/
|
|
176
|
+
RandomGenerator.paragraph = (props) => {
|
|
177
|
+
var _a;
|
|
178
|
+
return new Array((_a = props === null || props === void 0 ? void 0 : props.sentences) !== null && _a !== void 0 ? _a : randint(2, 5))
|
|
179
|
+
.fill("")
|
|
180
|
+
.map(() => { var _a, _b; return RandomGenerator.alphabets(randint((_a = props === null || props === void 0 ? void 0 : props.wordMin) !== null && _a !== void 0 ? _a : 3, (_b = props === null || props === void 0 ? void 0 : props.wordMax) !== null && _b !== void 0 ? _b : 7)); })
|
|
181
|
+
.join(" ");
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Generates random multi-paragraph content with customizable structure.
|
|
185
|
+
*
|
|
186
|
+
* Creates content consisting of multiple paragraphs separated by double
|
|
187
|
+
* newlines. Accepts an optional configuration object to control content
|
|
188
|
+
* structure including paragraph count, sentences per paragraph, and word
|
|
189
|
+
* character lengths. Ideal for generating realistic-looking text content for
|
|
190
|
+
* testing.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* // Generate with all defaults
|
|
195
|
+
* const article = RandomGenerator.content();
|
|
196
|
+
*
|
|
197
|
+
* // Specific structure: 5 paragraphs, 15-25 sentences each, 4-8 char words
|
|
198
|
+
* const longContent = RandomGenerator.content({
|
|
199
|
+
* paragraphs: 5,
|
|
200
|
+
* sentenceMin: 15,
|
|
201
|
+
* sentenceMax: 25,
|
|
202
|
+
* wordMin: 4,
|
|
203
|
+
* wordMax: 8
|
|
204
|
+
* });
|
|
205
|
+
*
|
|
206
|
+
* // Short content with brief sentences
|
|
207
|
+
* const shortContent = RandomGenerator.content({
|
|
208
|
+
* paragraphs: 2,
|
|
209
|
+
* sentenceMin: 5,
|
|
210
|
+
* sentenceMax: 8,
|
|
211
|
+
* wordMin: 2,
|
|
212
|
+
* wordMax: 4
|
|
213
|
+
* });
|
|
214
|
+
*
|
|
215
|
+
* // Generate blog post content
|
|
216
|
+
* const blogPost = {
|
|
217
|
+
* title: RandomGenerator.name(3),
|
|
218
|
+
* content: RandomGenerator.content({
|
|
219
|
+
* paragraphs: 4,
|
|
220
|
+
* sentenceMin: 10,
|
|
221
|
+
* sentenceMax: 20,
|
|
222
|
+
* wordMin: 3,
|
|
223
|
+
* wordMax: 7
|
|
224
|
+
* }),
|
|
225
|
+
* summary: RandomGenerator.paragraph({ sentences: 2 })
|
|
226
|
+
* };
|
|
227
|
+
*
|
|
228
|
+
* // Create test data for CMS
|
|
229
|
+
* const pages = Array.from({ length: 10 }, () => ({
|
|
230
|
+
* id: RandomGenerator.alphaNumeric(8),
|
|
231
|
+
* content: RandomGenerator.content({
|
|
232
|
+
* paragraphs: randint(2, 6),
|
|
233
|
+
* sentenceMin: 8,
|
|
234
|
+
* sentenceMax: 15
|
|
235
|
+
* })
|
|
236
|
+
* }));
|
|
237
|
+
* ```;
|
|
238
|
+
*
|
|
239
|
+
* @param props - Optional configuration object with paragraph, sentence, and
|
|
240
|
+
* word parameters
|
|
241
|
+
* @returns A string containing the generated multi-paragraph content
|
|
242
|
+
*/
|
|
243
|
+
RandomGenerator.content = (props) => {
|
|
244
|
+
var _a;
|
|
245
|
+
return new Array((_a = props === null || props === void 0 ? void 0 : props.paragraphs) !== null && _a !== void 0 ? _a : randint(3, 8))
|
|
246
|
+
.fill("")
|
|
247
|
+
.map(() => {
|
|
248
|
+
var _a, _b, _c, _d;
|
|
249
|
+
return RandomGenerator.paragraph({
|
|
250
|
+
sentences: randint((_a = props === null || props === void 0 ? void 0 : props.sentenceMin) !== null && _a !== void 0 ? _a : 10, (_b = props === null || props === void 0 ? void 0 : props.sentenceMax) !== null && _b !== void 0 ? _b : 40),
|
|
251
|
+
wordMin: (_c = props === null || props === void 0 ? void 0 : props.wordMin) !== null && _c !== void 0 ? _c : 1,
|
|
252
|
+
wordMax: (_d = props === null || props === void 0 ? void 0 : props.wordMax) !== null && _d !== void 0 ? _d : 7,
|
|
253
|
+
});
|
|
254
|
+
})
|
|
255
|
+
.join("\n\n");
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* Extracts a random substring from the provided content string.
|
|
259
|
+
*
|
|
260
|
+
* Selects two random positions within the content and returns the substring
|
|
261
|
+
* between them. The starting position is always before the ending position.
|
|
262
|
+
* Automatically trims whitespace from the beginning and end of the result.
|
|
263
|
+
* Useful for creating excerpts, search terms, or partial content samples.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* const text = "The quick brown fox jumps over the lazy dog";
|
|
268
|
+
*
|
|
269
|
+
* RandomGenerator.substring(text); // e.g. "quick brown fox"
|
|
270
|
+
* RandomGenerator.substring(text); // e.g. "jumps over"
|
|
271
|
+
* RandomGenerator.substring(text); // e.g. "fox jumps over the lazy"
|
|
272
|
+
*
|
|
273
|
+
* // Generate search terms from content
|
|
274
|
+
* const searchQuery = RandomGenerator.substring(articleContent);
|
|
275
|
+
*
|
|
276
|
+
* // Create excerpts for previews
|
|
277
|
+
* const excerpt = RandomGenerator.substring(fullBlogPost);
|
|
278
|
+
*
|
|
279
|
+
* // Generate partial matches for testing search functionality
|
|
280
|
+
* const partialMatch = RandomGenerator.substring(productDescription);
|
|
281
|
+
*
|
|
282
|
+
* // Create random selections for highlight testing
|
|
283
|
+
* const selectedText = RandomGenerator.substring(documentContent);
|
|
284
|
+
* ```;
|
|
285
|
+
*
|
|
286
|
+
* @param content - The source string to extract a substring from
|
|
287
|
+
* @returns A trimmed substring of the original content
|
|
288
|
+
*/
|
|
289
|
+
RandomGenerator.substring = (content) => {
|
|
290
|
+
const first = randint(0, content.length - 1);
|
|
291
|
+
const last = randint(first + 1, content.length);
|
|
292
|
+
return content.substring(first, last).trim();
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* Generates a random mobile phone number with customizable prefix.
|
|
296
|
+
*
|
|
297
|
+
* Creates a mobile phone number in the format: [prefix][3-4 digits][4
|
|
298
|
+
* digits]. The middle section is 3 digits if the random number is less than
|
|
299
|
+
* 1000, otherwise 4 digits. The last section is always 4 digits, zero-padded
|
|
300
|
+
* if necessary. Commonly used for generating Korean mobile phone numbers or
|
|
301
|
+
* similar formats.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```typescript
|
|
305
|
+
* RandomGenerator.mobile(); // e.g. "0103341234" or "01012345678"
|
|
306
|
+
* RandomGenerator.mobile("011"); // e.g. "0119876543" or "01112345678"
|
|
307
|
+
* RandomGenerator.mobile("+82"); // e.g. "+823341234" or "+8212345678"
|
|
308
|
+
*
|
|
309
|
+
* // Generate test user phone numbers
|
|
310
|
+
* const testUsers = Array.from({ length: 100 }, () => ({
|
|
311
|
+
* name: RandomGenerator.name(),
|
|
312
|
+
* phone: RandomGenerator.mobile(),
|
|
313
|
+
* altPhone: RandomGenerator.mobile("011")
|
|
314
|
+
* }));
|
|
315
|
+
*
|
|
316
|
+
* // Create international phone numbers
|
|
317
|
+
* const internationalPhone = RandomGenerator.mobile("+821");
|
|
318
|
+
*
|
|
319
|
+
* // Generate contact list for testing
|
|
320
|
+
* const contacts = ["010", "011", "016", "017", "018", "019"].map(prefix => ({
|
|
321
|
+
* carrier: prefix,
|
|
322
|
+
* number: RandomGenerator.mobile(prefix)
|
|
323
|
+
* }));
|
|
324
|
+
* ```;
|
|
325
|
+
*
|
|
326
|
+
* @param prefix - The prefix string for the phone number (default: "010")
|
|
327
|
+
* @returns A formatted mobile phone number string
|
|
328
|
+
*/
|
|
329
|
+
RandomGenerator.mobile = (prefix = "010") => [
|
|
330
|
+
prefix,
|
|
331
|
+
(() => {
|
|
332
|
+
const value = randint(0, 9999);
|
|
333
|
+
return value.toString().padStart(value < 1000 ? 3 : 4, "0");
|
|
334
|
+
})(),
|
|
335
|
+
randint(0, 9999).toString().padStart(4, "0"),
|
|
336
|
+
].join("");
|
|
337
|
+
/**
|
|
338
|
+
* Generates a random date within a specified range from a starting point.
|
|
339
|
+
*
|
|
340
|
+
* Returns a random date between the start date and start date + range. The
|
|
341
|
+
* range represents the maximum number of milliseconds to add to the starting
|
|
342
|
+
* date. Useful for generating timestamps, creation dates, or scheduling test
|
|
343
|
+
* data.
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* const now = new Date();
|
|
348
|
+
* const oneDay = 24 * 60 * 60 * 1000;
|
|
349
|
+
* const oneMonth = 30 * oneDay;
|
|
350
|
+
*
|
|
351
|
+
* // Random date within the next 30 days
|
|
352
|
+
* const futureDate = RandomGenerator.date(now, oneMonth);
|
|
353
|
+
*
|
|
354
|
+
* // Random date within the past week
|
|
355
|
+
* const pastWeek = new Date(now.getTime() - 7 * oneDay);
|
|
356
|
+
* const recentDate = RandomGenerator.date(pastWeek, 7 * oneDay);
|
|
357
|
+
*
|
|
358
|
+
* // Generate random creation dates for test data
|
|
359
|
+
* const startOfYear = new Date(2024, 0, 1);
|
|
360
|
+
* const endOfYear = new Date(2024, 11, 31).getTime() - startOfYear.getTime();
|
|
361
|
+
* const randomCreationDate = RandomGenerator.date(startOfYear, endOfYear);
|
|
362
|
+
*
|
|
363
|
+
* // Create test events with random timestamps
|
|
364
|
+
* const events = Array.from({ length: 50 }, () => ({
|
|
365
|
+
* id: RandomGenerator.alphaNumeric(8),
|
|
366
|
+
* title: RandomGenerator.name(2),
|
|
367
|
+
* createdAt: RandomGenerator.date(new Date(), oneMonth),
|
|
368
|
+
* scheduledFor: RandomGenerator.date(new Date(), oneMonth * 3)
|
|
369
|
+
* }));
|
|
370
|
+
* ```;
|
|
371
|
+
*
|
|
372
|
+
* @param from - The starting date for the random range
|
|
373
|
+
* @param range - The range in milliseconds from the starting date
|
|
374
|
+
* @returns A random date within the specified range
|
|
375
|
+
*/
|
|
376
|
+
RandomGenerator.date = (from, range) => new Date(from.getTime() + randint(0, range));
|
|
377
|
+
/**
|
|
378
|
+
* Randomly samples a specified number of unique elements from an array.
|
|
379
|
+
*
|
|
380
|
+
* Selects random elements from the input array without replacement, ensuring
|
|
381
|
+
* all returned elements are unique. The sample size is automatically capped
|
|
382
|
+
* at the array length to prevent errors. Uses a Set-based approach to
|
|
383
|
+
* guarantee uniqueness of selected indices. Ideal for creating test datasets
|
|
384
|
+
* or selecting random subsets for validation.
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```typescript
|
|
388
|
+
* const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
389
|
+
*
|
|
390
|
+
* RandomGenerator.sample(numbers, 3); // e.g. [2, 7, 9]
|
|
391
|
+
* RandomGenerator.sample(numbers, 5); // e.g. [1, 4, 6, 8, 10]
|
|
392
|
+
* RandomGenerator.sample(numbers, 15); // returns all 10 elements (capped at array length)
|
|
393
|
+
*
|
|
394
|
+
* // Sample users for testing
|
|
395
|
+
* const allUsers = await getUsersFromDatabase();
|
|
396
|
+
* const testUsers = RandomGenerator.sample(allUsers, 10);
|
|
397
|
+
*
|
|
398
|
+
* // Create random product selections
|
|
399
|
+
* const featuredProducts = RandomGenerator.sample(allProducts, 5);
|
|
400
|
+
*
|
|
401
|
+
* // Generate test data subsets
|
|
402
|
+
* const validationSet = RandomGenerator.sample(trainingData, 100);
|
|
403
|
+
*
|
|
404
|
+
* // Random A/B testing groups
|
|
405
|
+
* const groupA = RandomGenerator.sample(allParticipants, 50);
|
|
406
|
+
* const remaining = allParticipants.filter(p => !groupA.includes(p));
|
|
407
|
+
* const groupB = RandomGenerator.sample(remaining, 50);
|
|
408
|
+
* ```;
|
|
409
|
+
*
|
|
410
|
+
* @param array - The source array to sample from
|
|
411
|
+
* @param count - The number of elements to sample
|
|
412
|
+
* @returns An array containing the randomly selected elements
|
|
413
|
+
*/
|
|
414
|
+
RandomGenerator.sample = (array, count) => {
|
|
415
|
+
count = Math.min(count, array.length);
|
|
416
|
+
const indexes = new Set();
|
|
417
|
+
while (indexes.size < count)
|
|
418
|
+
indexes.add(randint(0, array.length - 1));
|
|
419
|
+
return Array.from(indexes).map((i) => array[i]);
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* Randomly selects a single element from an array.
|
|
423
|
+
*
|
|
424
|
+
* Chooses one element at random from the provided array using uniform
|
|
425
|
+
* distribution. Each element has an equal probability of being selected. This
|
|
426
|
+
* is a convenience function equivalent to sampling with a count of 1, but
|
|
427
|
+
* returns the element directly rather than an array containing one element.
|
|
428
|
+
*
|
|
429
|
+
* @example
|
|
430
|
+
* ```typescript
|
|
431
|
+
* const colors = ['red', 'blue', 'green', 'yellow', 'purple'];
|
|
432
|
+
* const fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
|
|
433
|
+
*
|
|
434
|
+
* RandomGenerator.pick(colors); // e.g. "blue"
|
|
435
|
+
* RandomGenerator.pick(fruits); // e.g. "apple"
|
|
436
|
+
*
|
|
437
|
+
* // Select random configuration options
|
|
438
|
+
* const randomTheme = RandomGenerator.pick(['light', 'dark', 'auto']);
|
|
439
|
+
* const randomLocale = RandomGenerator.pick(['en', 'ko', 'ja', 'zh']);
|
|
440
|
+
*
|
|
441
|
+
* // Choose random test scenarios
|
|
442
|
+
* const testScenario = RandomGenerator.pick([
|
|
443
|
+
* 'happy_path',
|
|
444
|
+
* 'edge_case',
|
|
445
|
+
* 'error_condition',
|
|
446
|
+
* 'boundary_test'
|
|
447
|
+
* ]);
|
|
448
|
+
*
|
|
449
|
+
* // Random user role assignment
|
|
450
|
+
* const userRole = RandomGenerator.pick(['admin', 'user', 'moderator']);
|
|
451
|
+
*
|
|
452
|
+
* // Select random API endpoints for testing
|
|
453
|
+
* const endpoints = ['/users', '/posts', '/comments', '/categories'];
|
|
454
|
+
* const randomEndpoint = RandomGenerator.pick(endpoints);
|
|
455
|
+
* ```;
|
|
456
|
+
*
|
|
457
|
+
* @param array - The source array to pick an element from
|
|
458
|
+
* @returns A randomly selected element from the array
|
|
459
|
+
*/
|
|
460
|
+
RandomGenerator.pick = (array) => array[randint(0, array.length - 1)];
|
|
461
|
+
})(RandomGenerator || (exports.RandomGenerator = RandomGenerator = {}));
|
|
462
|
+
/** @internal */
|
|
463
|
+
const randint = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
|
|
464
|
+
//# sourceMappingURL=RandomGenerator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RandomGenerator.js","sourceRoot":"","sources":["../src/RandomGenerator.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,IAAiB,eAAe,CAsc/B;AAtcD,WAAiB,eA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