-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
29 lines (22 loc) · 963 Bytes
/
generator.js
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
const words = require("./word.json")
/**
* @param {number} max max length of username, default: 8
* @param {boolean} space allows space or not, default: false
*/
function generateUsername(max = 8, space = false) {
if (max < 6) return TypeError("max value must be greater than 6")
const generatedUsrn = max >= 10 ? getRandomWord() + getRandomSpace(space) + getRandomWord() : getRandomWord()
return generatedUsrn.slice(0, max-2) + getRandomNumber().toString()
}
function getRandomWord() {
let dic = Object.keys(words)[Math.floor(Math.random()*Object.keys(words).length)]
return words[dic][Math.floor(Math.random()*words[dic].length)]
}
function getRandomSpace(space) {
const spaces = space == true ? ["-", "_", " ", ""] : ["-", "_", ""]
return spaces[Math.floor(Math.random()*spaces.length)]
}
function getRandomNumber() {
return Math.floor(Math.random()*(99 - 10)+10)
}
module.exports.generateUsername = generateUsername