-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
35 lines (29 loc) · 1.02 KB
/
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
import containsProfaneWord from "./functions/containsProfaneWord";
/**
* Generate URL friendly short IDs just like YouTube
* @returns {string} Randomly generated string of characters just like YouTube
*/
export function accountId(){
const characters = 'ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789-_';
const length = 11;
let id;
do {
id = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
const selectedCharacter = characters[randomIndex];
// Preventing identical three or more consecutive characters
if (i >= 2 && id[i - 1] === selectedCharacter && id[i - 2] === selectedCharacter) {
i--;
continue;
}
// Preventing two consecutive underscores and hyphens
if ((selectedCharacter === '_' && id[i - 1] === '_') || (selectedCharacter === '-' && id[i - 1] === '-')) {
i--;
continue;
}
id += selectedCharacter;
}
} while (containsProfaneWord(id));
return id;
}