This repository has been archived by the owner on Mar 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwuifier.js
188 lines (177 loc) · 4.95 KB
/
uwuifier.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
let settings = {
periodToExclamationChance: 0.2,
stutterChance: 0.1,
presuffixChance: 0.1,
suffixChance: 0.3,
duplicateCharactersChance: 0.4,
duplicateCharactersAmount: 3
};
function getChance(chance) { return Math.random() < chance; }
function escapeString(string) { return string.replaceAll(/(?=[~_<>])/g, '\\'); }
function isCaps(string) { return string == string.toUpperCase(); }
let simpleReplacements = [
[ 'l', 'w' ],
[ 'r', 'w' ],
[ 'na', 'nya' ],
[ 'ne', 'nye' ],
[ 'ni', 'nyi' ],
[ 'no', 'nyo' ],
[ 'nu', 'nyu' ],
[ 'pow', 'paw' ],
[ /(?<!w)ui/g, 'wi' ],
[ /(?<!w)ue/g, 'we' ],
[ 'attempt', 'attwempt' ],
[ 'config', 'cwonfig' ]
];
let wordReplacements = {
'you': 'uwu',
'no': 'nu',
'oh': 'ow',
'too': 'two',
'attempt': 'attwempt',
'config': 'cwonfig'
};
let presuffixes = [
'~',
'~~',
','
];
let suffixes = [
':D',
[
'xD',
'XD',
],
':P',
';3',
'<{^v^}>',
'^-^',
'x3',
[
'rawr',
'rawr~',
'rawr~~',
'rawr x3',
'rawr~ x3',
'rawr~~ x3'
],
[
'owo',
'owo~',
'owo~~'
],
[
'uwu',
'uwu~',
'uwu~~'
],
'-.-',
'>w<',
':3',
[
'nya',
'nya~',
'nya~~',
'nyaa',
'nyaa~',
'nyaa~~'
],
[
'>_<',
'>-<'
],
':flushed:',
'👉👈',
[
'^^',
'^^;;'
],
[
'w',
'ww'
],
','
];
function chooseSuffixVariation(list) {
let option = list[Math.floor(Math.random() * list.length)];
if(Array.isArray(option)) return chooseSuffixVariation(option);
return option;
}
let replacements = [
// . to !
[
// match a . with a space or string end after it
/\.(?= |$)/g, (_escape, isIgnoredAt) => function(match, offset, string) {
if(isIgnoredAt(offset, string)) return match;
if(!getChance(settings.periodToExclamationChance))
return match;
return '!';
}
],
// duplicate characters
[
/[,!]/g, (_escape, isIgnoredAt) => function(match, offset, string) {
if(isIgnoredAt(offset, string)) return match;
if(getChance(settings.duplicateCharactersChance)) {
let amount = Math.floor((Math.random() + 1) * (settings.duplicateCharactersAmount - 1));
let newMatch = match;
for(let i = 0; i < amount; i++)
newMatch += match;
match = newMatch;
}
return match;
}
],
// simple and word replacements
[
// match a word
/(?<=\b)[a-zA-Z\']+(?=\b)/g, (_escape, isIgnoredAt) => function(match, offset, string) {
if(isIgnoredAt(offset, string)) return match;
let caps = isCaps(match);
match = match.toLowerCase();
if(match in wordReplacements) // only replace whole words
match = wordReplacements[match];
for(const [ pattern, replacement ] of simpleReplacements) {
// don't replace whole words
if((pattern instanceof RegExp) && match.match(pattern)?.includes(match) || pattern == match)
continue;
match = match.replaceAll(pattern, replacement);
}
return caps ? match.toUpperCase() : match;
}
],
// stutter
[
// match beginning of a word
/(?<= |^)[a-zA-Z]/g, (_escape, isIgnoredAt) => function(match, offset, string) {
if(isIgnoredAt(offset, string)) return match;
if(!getChance(settings.stutterChance))
return match;
return `${match}-${match}`;
}
],
// suffixes
[
/(?<=[\.\!\?\,\;\-])(?= )|(?=$)/g, (escape, isIgnoredAt) => function(match, offset, string) {
if(isIgnoredAt(offset, string)) return match;
let presuffix = '';
let suffix = '';
if(getChance(settings.presuffixChance))
presuffix = chooseSuffixVariation(presuffixes);
if(getChance(settings.suffixChance))
suffix = ` ${chooseSuffixVariation(suffixes)}`;
let finalSuffix = `${presuffix}${suffix}`;
if(escape) finalSuffix = escapeString(finalSuffix);
return finalSuffix;
}
]
];
module.exports = {
settings: settings,
uwuify: function(string, escape = false, isIgnoredAt = () => false) {
if(string == null || !/\S/.test(string)) return string;
for(const [ pattern, replacement ] of replacements)
string = string.replaceAll(pattern, replacement(escape, isIgnoredAt));
return string;
}
}