-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
41 lines (35 loc) · 869 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { RNG, WORD_CHARS } from '../../constants';
import { WithRNGOption } from '../../types/WithRNGOption';
import { char } from '../char';
import { int } from '../int';
export type StrOptions = {
/**
* Alphabet of characters where to pick a value.
* @default `[A-z0-9]`
*/
alphabet?: string;
/**
* Minimum posible length of returned string, inclusive.
*/
minLength?: number;
/**
* Maximum possible length of returned string, inclusive.
*/
maxLength?: number;
} & WithRNGOption;
/**
* Returns random string value.
*/
export const str = ({
alphabet = WORD_CHARS,
minLength = 0,
maxLength = 1,
rng = RNG,
}: StrOptions = {}): string => {
const length = int({ min: minLength, max: maxLength, rng });
let result = '';
for (let i = 0; i < length; i++) {
result += char({ alphabet, rng });
}
return result;
};