Posts yoGetRandomString
Post
Cancel

yoGetRandomString

npm install @benline/yoGetRandomString

Get random string module for react thingy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const getRandomString = (length:any, chars:any) => {
    let mask = '';
    if (chars.indexOf('a') > -1) mask += 'abcdefghijklmnopqrstuvwxyz';
    if (chars.indexOf('A') > -1) mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    if (chars.indexOf('#') > -1) mask += '0123456789';
    if (chars.indexOf('!') > -1) mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';

    let result = '';
    for (let i = length; i > 0; --i) result += mask[Math.round(Math.random() * (mask.length - 1))];
    return result;
}

getRandomString(10, "myexamplestring")

View Repo

Trending Tags

Trending Tags