-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrandom.ts
63 lines (56 loc) · 1.41 KB
/
random.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { shuffle } from './array';
/**
* 获取随机小写英文单词
*/
export function getRandomLower() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
/**
* 获取随机大写英文单词
*/
export function getRandomUpper() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
/**
* 获取随机数字
*/
export function getRandomNumber() {
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}
/**
* 获取随机符号
*/
export function getRandomSymbol() {
const symbols = '~!@#$%^&*()_+{}":?><;.,';
return symbols[Math.floor(Math.random() * symbols.length)];
}
const randomFunc = {
lower: getRandomLower,
upper: getRandomUpper,
number: getRandomNumber,
symbol: getRandomSymbol,
};
/**
* 生成一定长度随机数
* @param len 长度
* @param types 随机字符串需要包含的字符
*/
export function getRandomString(
len: number,
types: Array<keyof typeof randomFunc> = ['lower', 'upper', 'number', 'symbol'],
) {
return shuffle(
Array(len)
.fill(undefined)
.map((_, index) => index % types.length),
).reduce((acc, i) => (acc += randomFunc[types[i]]()), '');
}
/**
* 生成不重复字符串
* @returns {string}
*/
export function createUniqueString() {
const timestamp = +new Date() + '';
const randomNum = parseInt(String((1 + Math.random()) * 65536)) + '';
return (+(randomNum + timestamp)).toString(32);
}