-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
111 lines (92 loc) · 3.54 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* This module provides the main function that outputs a username.
* Copyright 2021 © Joey Malvinni
* @author Joey Malvinni
*
* @license
* Copyright 2021 © Joey Malvinni. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
const fs = require('fs');
const capitalize = require('./src/capitalize')
const CategoryError = require('./src/CategoryError')
const { randomNumber, randomRange } = require('./src/random')
const shuffle = require('./src/shuffle')
let words = {};
let wordCategories = [];
let wordsFolder;
wordsFolder = fs.readdirSync(__dirname + '/data')
for(let i = 0; i < wordsFolder.length; i++){
words[wordsFolder[i].slice(0, -5).split('_').join(' ')] = JSON.parse(fs.readFileSync(__dirname + '/data/' + wordsFolder[i]).toString());
wordCategories.push(wordsFolder[i].slice(0, -5).split('_').join(' '));
};
function generateNumber(){
var number = randomNumber(randomRange(1, 6));
while(isNaN(number)){
number = randomNumber(randomRange(1, 6));
};
return number;
}
/**
* Generates a username.
* @function
* @param {object} settings - Advanced options can be set here.
*/
function generateUsername(settings){
settings = settings || {};
if (typeof(settings.wordFormatter) !== 'function') {
settings.wordFormatter = (word) => word;
};
if (typeof(settings.formatter) !== 'function') {
settings.formatter = (word) => word;
};
if (typeof(settings.separator) !== 'string') {
settings.separator = '';
};
if (!Array.isArray(settings.order)) {
settings.order = ["adjectives", "nouns", "numbers"];
};
if (!Array.isArray(settings.capitalize)) {
settings.capitalize = wordCategories;
};
var final = '';
for (let i = 0; i < settings.order.length; i++){
if (typeof words[settings.order[i]] !== 'undefined' && wordCategories.indexOf(settings.order[i]) !== -1) {
let index = randomNumber(randomRange(0, words[settings.order[i]].length));
while(typeof words[settings.order[i]][index] == 'undefined'){
index = randomNumber(randomRange(0, words[settings.order[i]].length));
};
let word = shuffle(words[settings.order[i]])[index].toString();
if (settings.capitalize.includes(settings.order[i])) {
word = capitalize(word);
} else {
word = word.toLowerCase();
};
final += settings.wordFormatter(word);
} else {
if (settings.order[i] === 'numbers') {
final += settings.wordFormatter(generateNumber().toString());
} else {
if (settings.customWords) {
final += settings.wordFormatter(settings.order[i].toString());
} else {
throw new CategoryError(settings.order[i]);
};
};
};
if (i !== settings.order.length - 1) final += settings.separator;
};
return settings.formatter(final.toString());
};
module.exports = generateUsername;