File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 1
1
const DEFAULT_ALLOWED_CHARACTERS =
2
2
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*' ;
3
3
4
+ /**
5
+ * Generates a random string based on the given length and set of allowed characters.
6
+ *
7
+ * @param {Object } [options={}] - The options for generating the random string.
8
+ * @param {number } [options.length=Math.ceil(Math.random() * 20)] - The length of the string to generate.
9
+ * @param {string } [options.allowedCharacters=DEFAULT_ALLOWED_CHARACTERS] - A string of allowed characters.
10
+ * @returns {string } The generated random string.
11
+ *
12
+ * @example
13
+ * // Example 1: Generate a random string with default options
14
+ * const randomStr = randomString();
15
+ * console.log(randomStr); // Output will be a random string of length between 1 and 20 characters.
16
+ *
17
+ * @example
18
+ * // Example 2: Generate a random string of length 10
19
+ * const randomStr10 = randomString({ length: 10 });
20
+ * console.log(randomStr10); // Output will be a random string of exactly 10 characters.
21
+ *
22
+ * @example
23
+ * // Example 3: Generate a random string of length 5 with specific allowed characters
24
+ * const allowedChars = 'abc123';
25
+ * const randomStrCustom = randomString({ length: 5, allowedCharacters: allowedChars });
26
+ * console.log(randomStrCustom); // Output will be a random string of exactly 5 characters, with characters only from 'abc123'.
27
+ */
4
28
function randomString ( {
5
29
length = Math . ceil ( Math . random ( ) * 20 ) ,
6
30
allowedCharacters = DEFAULT_ALLOWED_CHARACTERS
You can’t perform that action at this time.
0 commit comments