-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (32 loc) · 1.05 KB
/
index.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
39
40
41
42
import adjectives from './adjectives.js';
import animals from './animals.js';
function getRandomFromArray(arr) {
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
function getRandomDigits(digits) {
const randomNumber = Math.floor(
Math.random() * Math.pow(10, digits)
);
return randomNumber;
}
function capitalizeWord(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
export default function generateRandomUsername({ separator = '-', digits = 0, capitalize = false } = {}) {
if (typeof separator !== 'string') {
separator = '';
}
if (typeof digits !== 'number') {
digits = 0;
}
let first = getRandomFromArray(adjectives);
let second = getRandomFromArray(animals);
if (capitalize) {
first = capitalizeWord(first);
second = capitalizeWord(second);
}
const randomDigits = digits > 0 ? separator + getRandomDigits(digits) : '';
const username = `${first}${separator}${second}${randomDigits}`;
return username;
}