utils: String.random with dict

This commit is contained in:
undefined committed 2021-06-21 13:40:38 +08:00
1 parent 387bd5eb38
commit e2c5e57e53
3 files changed
+5 -4

No files matched your search

+3 -3
View File
@@ -31,11 +31,11 @@ declare global {
}
}
const dict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
const defaultDict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
String.random = function random(digit = 32) {
String.random = function random(digit = 32, dict = defaultDict) {
let str = '';
for (let i = 1; i <= digit; i++) str += dict[Math.floor(Math.random() * 62)];
for (let i = 1; i <= digit; i++) str += dict[Math.floor(Math.random() * dict.length)];
return str;
};