forked from michelle/console.loge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loge.js
230 lines (191 loc) · 5.58 KB
/
loge.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(function(exports) {
var ILLITERACY_RATE = 0.4; // illiterite
var CONFUSION_RATE = 0.2; // *what*
var SUPPLIES_RATE = 0.1; // wow, who am i?
var DOGE_RATE = 0.8; // very hi
var PREFIXES = ['so', 'such', 'very', 'plz', 'how', 'am', 'much'];
var SURPRISES = ['who am i', 'wow', 'wow', 'so amaze'];
var AVERAGE_WHITESPACE = 50;
var VOWELS = {
'e': 'a', // TODO: what to do about double letters?
'a': 'e',
'i': 'e',
'u': 'o',
'o': 'u'
};
var REPLACEMENTS = {
'error': 'bad bad',
'here': 'so amaze',
'todo': 'must do',
'fixme': 'such broken'
};
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
function loge() {
var args = arguments;
var results = [];
var rand = randomSurprise();
if (rand) {
results.push(rand);
}
for (var i = 0, ii = args.length; i < ii; i += 1) {
var phrase = randomWhitespace();
var input = args[i];
if (Math.random() < DOGE_RATE) {
phrase += randomPrefix() + ' ';
}
if (typeof input === 'string') {
phrase += randomPhraseMutation(args[i]);
} else {
phrase += (typeof input) + ': ';
results.push(phrase);
// Log objects on their own.
phrase = input;
}
if (Math.random() < SUPPLIES_RATE) {
results.push(randomWhitespace() + randomSurprise());
}
}
if (isChrome) {
/* Gets the control string */
control_string = "";
for (var i = 0; i < results.length; i++){
control_string += '%c%s';
}
/* arr is the array to return */
arr = [];
arr.push (control_string);
/* Push the formatting and the strings */
for (var i = 0; i < results.length; i++){
arr.push('font-family: Comic Sans MS; line-height: 2em; font-size: 1.5em; color: ' + randomColor());
arr.push(results[i]);
}
}
console.log.apply(console, arr);
}
/**
* so many random
*
* such functions
*/
// Generate a random number between two numbers! Wow!
function randomNumber(min, max) {
return Math.floor(Math.random() * max) + min;
}
function randomColor() {
return ['red', 'blue', 'green', 'aqua', 'magenta', 'yellow'][randomNumber(0, 5)];
}
// Select a random prefix! Wow!
function randomPrefix() {
return PREFIXES[randomNumber(0, PREFIXES.length)];
}
// Select a random substitution! Wow!
function randomSurprise() {
return SURPRISES[randomNumber(0, SURPRISES.length)];
}
// Select a random amount of whitespace! Wow!
function randomWhitespace() {
var randomMax = AVERAGE_WHITESPACE * (1 + Math.random() - 0.5);
str = '';
for (var i = 0; i < randomMax; i += 1) {
str += ' ';
}
return str;
}
// TODO
function randomPhraseMutation(phrase) {
phrase = maybeReplaceVowels(phrase);
phrase = phrase.split(' ');
for (var i = 0, ii = phrase.length; i < ii; i += 1) {
phrase[i] = maybeSwitchLastLetters(phrase[i]);
// TODO: add more
}
phrase = phrase.join(' ');
phrase = maybeAddStars(phrase);
phrase = globalReplacements(phrase);
return phrase;
}
/**
* woww the utils
*
* such phrase utils
*/
function maybeReplaceVowels(phrase) {
if (Math.random() > ILLITERACY_RATE) {
return phrase;
}
var words = phrase.split(' ');
// Choose longest word to replace.
var bestWord;
var index = 0;
for (var i = 0, ii = words.length; i < ii; i += 1) {
if (!bestWord || words[i].length > bestWord.length) {
bestWord = words[i];
index = i;
}
}
var vowel = findBestVowel(bestWord);
if (vowel) {
words[index] = bestWord.replace(vowel, VOWELS[vowel]);
}
return words.join(' ');
}
// Replace global things.
function globalReplacements(phrase) {
phrase = phrase.toLowerCase();
var targetWords = Object.keys(REPLACEMENTS);
for (var i = 0, ii = targetWords.length; i < ii; i += 1) {
phrase = phrase.replace(targetWords[i], REPLACEMENTS[targetWords[i]]);
}
return phrase;
}
// Adds stars.
function maybeAddStars(phrase) {
if (Math.random() < CONFUSION_RATE) {
return '*' + phrase + '*';
}
else {
return phrase;
}
}
/**
* oh wow
*
* utils for words
*/
// Returns the most common vowel in word.
function findBestVowel(word) {
var letters = word.split('');
letters.pop(); // trailing letters don't count--we're not THAT illiterate.
var bestLetters = {};
var uniqueLetters = {};
for (var i = 0, ii = letters.length; i < ii; i += 1) {
var letter = letters[i];
uniqueLetters[letter] |= 0;
uniqueLetters[letter] += 1;
}
var vowels = Object.keys(VOWELS);
var bestVowel;
var bestVowelCount = 0;
for (var i = 0, ii = vowels.length; i < ii; i += 1) {
var vowel = vowels[i];
if (uniqueLetters[vowel] !== undefined && uniqueLetters[vowel] >= bestVowelCount) {
bestVowel = vowel;
bestVowelCount = uniqueLetters[vowel];
}
}
return bestVowel;
}
// Switch last two letters of a word ending with 'e'.
function maybeSwitchLastLetters(word) {
if (Math.random() < ILLITERACY_RATE && word[word.length - 1] === 'e' && word.length > 2) {
word = word.split('');
var last = word[word.length - 1];
word[word.length - 1] = word[word.length - 2];
word[word.length - 2] = last;
word = word.join('');
}
return word;
}
exports.console = exports.console || {};
exports.console.loge = loge;
})(this);