xuchang-screen/websocket/utils.ts

49 lines
1003 B
TypeScript
Raw Normal View History

2023-09-10 20:35:29 +08:00
export default {
// 生成随机数
2023-09-11 10:39:38 +08:00
randomNum({ min, max }: { min: number; max: number }, isFloat = false) {
if (isFloat) return (Math.random() * (max - min) + min).toFixed(2);
2023-09-10 20:35:29 +08:00
return Math.floor(Math.random() * (max - min + 1) + min);
},
2023-09-11 10:39:38 +08:00
getMinmax(type: '*' | '.' | '+' | '$') {
2023-09-10 20:35:29 +08:00
let min: number, max: number;
switch (type) {
case '*':
min = 30;
max = 150;
break;
case '.':
min = 60;
max = 200;
break;
case '+':
min = 20;
max = 70;
break;
2023-09-11 10:39:38 +08:00
case '$':
min = 1;
max = 100;
break;
2023-09-10 20:35:29 +08:00
}
return { min, max };
},
getRandom(value: string) {
value = value.replace(
'***',
'' + this.randomNum({ ...this.getMinmax('*') }),
);
value = value.replace(
'...',
'' + this.randomNum({ ...this.getMinmax('.') }),
);
value = value.replace(
'+++',
'' + this.randomNum({ ...this.getMinmax('+') }),
);
2023-09-11 10:39:38 +08:00
value = value.replace(
'$$$',
'' + this.randomNum({ ...this.getMinmax('$') }, true),
);
2023-09-10 20:35:29 +08:00
return value;
},
};