Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (119 loc) · 3.39 KB
/
Copy pathindex.js
File metadata and controls
136 lines (119 loc) · 3.39 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Position, Type, fragments } from "./fragments.js";
export function findFragments({
type = null,
position = null,
rude = null,
}) {
return fragments.filter(fragment => {
if (type !== null) {
if (fragment.type !== Type.ANY && fragment.type !== type) {
return false;
}
}
if (position !== null) {
if (fragment.position !== Position.ANY && fragment.position !== Position.ALONE && fragment.position !== position) {
return false;
}
}
if (rude !== null && rude !== fragment.rude) {
return false;
}
return true;
})
}
export function chooseFragment({
type = null,
position = null,
rude = null,
}) {
let choices = findFragments({type, position, rude});
let index = Math.floor(Math.random() * choices.length);
return choices[index];
}
const needsHyphen = [
"a", "e", "i", "o", "u", "s"
]
const duoNeedsHyphen = [
"ss",
"yd",
"ff",
"gr",
"gg",
"ng",
"ry",
]
const needsCaps = [
" ", ".", "-", "!", ",", "*"
]
export function titleCase(text) {
if (!text || text.length === 0) {
return "";
}
let array = text.split("");
array[0] = array[0].toUpperCase();
for (let i = array.length - 1; i >= 0; i--) {
if (needsCaps.includes(array[i])) {
if (i == array.length - 1) {
continue;
}
array[i + 1] = array[i + 1].toUpperCase();
}
}
return array.join("");
}
export class Word {
constructor(type = Type.ANY, rude = null) {
this.type = type
this.rude = rude
this.text = this.generate();
};
generate() {
let start = chooseFragment({type: this.type, position: Position.START, rude: this.rude});
if (start.position === Position.ALONE) {
return titleCase(start.fragment);
}
let end = chooseFragment({type: this.type, position: Position.END, rude: this.rude});
if (end.position === Position.ALONE) {
return titleCase(end.fragment);
}
let word = [start.fragment, end.fragment]
let endChar = "";
let startChar = "";
if (start.fragment.length > 0) {
endChar = start.fragment[start.fragment.length - 1].toLowerCase();
}
if (end.fragment.length > 0) {
startChar = end.fragment[0].toLowerCase();
}
if (
(
(start.hyphenate || end.hyphenate)
|| (startChar === endChar)
|| needsHyphen.includes(startChar)
|| needsHyphen.includes(endChar)
|| duoNeedsHyphen.includes(endChar + startChar)
)
&& (
endChar !== " "
&& startChar !== " "
)
) {
word = [titleCase(start.fragment), "-", titleCase(end.fragment)]
}
let text = word.join("");
return titleCase(text);
}
}
export class Cumberbatcher {
constructor(rude = null) {
this.rude = rude;
this.forename = null;
this.surname = null;
this.name = null;
}
generate() {
this.forename = new Word(Type.FORENAME, this.rude);
this.surname = new Word(Type.SURNAME, this.rude);
this.name = [this.forename.text, this.surname.text].join(" ");
}
}