-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateUsername.js
38 lines (28 loc) · 1.06 KB
/
generateUsername.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
30
31
32
33
34
35
36
37
38
"use strict";
const { nouns, adjectives } = require("./usernameDict");
const getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
const randomNumber = (maxNumber) => {
if (maxNumber < 1 || !Number.isFinite(maxNumber)) {
return "";
}
let min = Math.pow(10, maxNumber - 1);
let max = Math.pow(10, maxNumber) - 1;
return getRandomInt(min, max).toString();
};
function generateUsername(separator = "", randomDigits = 0, length = undefined, prefix = "") {
const noun = nouns[Math.floor(Math.random() * nouns.length)];
const adjective = prefix ? prefix.trim().replace(/\s+/g, separator).toLowerCase() : adjectives[Math.floor(Math.random() * adjectives.length)];
let username;
if (separator) {
username = adjective + separator + noun + randomNumber(randomDigits);
} else {
username = adjective + noun + randomNumber(randomDigits);
}
if (length) {
username = username.substring(0, length);
}
return username;
}
module.exports = { generateUsername };