forked from sebpearce/bullshit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_ES6.js
157 lines (131 loc) · 4.5 KB
/
main_ES6.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
"use strict";
/*
* main.js
*
* New Age Bullshit Generator
* © 2014-15 Seb Pearce (sebpearce.com)
* Licensed under the MIT License.
*
*/
// Toolkit of useful functions
var kit = {
copyArrayOfArrays(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
result[i] = arr[i].slice();
}
return result;
},
capitalizeFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
randomInt(max) {
return Math.floor(Math.random() * (max + 1));
}
};
// The generator in all its quantum glory
var bs = {
sentencePool: [],
initializeSentencePool() {
this.sentencePool = [];
this.sentencePool = kit.copyArrayOfArrays(this.sentencePatterns);
},
removeSentenceFromPool(topic, el) {
if (el > -1) {
this.sentencePool[topic].splice(el, 1);
}
},
retrieveRandomWordOfType(type) {
var rand = kit.randomInt(this.bullshitWords[type].length - 1);
return this.bullshitWords[type][rand];
},
generateSentence(topic) {
var patternNumber = kit.randomInt(this.sentencePool[topic].length - 1);
var pattern = this.sentencePool[topic][patternNumber];
if (typeof pattern == "undefined") {
console.log("ran out of pattern " + patternNumber);
}
// insert a space before . , ; ? so we can split the string into an array
var pattern = pattern.replace(/([\.,;\?])/g, " $1");
var pattern = pattern.split(" ");
// remove the pattern from the sentence pool so it can't be re-used
this.removeSentenceFromPool(topic, patternNumber);
// remove the topic from the sentence pool if there are no sentences left
// for that particular topic
if (this.sentencePool[topic].length === 0) {
this.sentencePool.splice(topic, 1);
}
var result = "";
for (var x in pattern) {
// if word matches one of the placeholder words (e.g. nPerson),
// replace it with a random instance of its type (e.g. warrior)
if (this.bullshitWords.hasOwnProperty(pattern[x])) {
result += this.retrieveRandomWordOfType(pattern[x]);
} else {
result += pattern[x];
}
result += " ";
}
// replace 'a [vowel]' with 'an [vowel]'
// I added a \W before the [Aa] because one time I got
// 'Dogman is the antithesis of knowledge' :)
result = result.replace(/(^|\W)([Aa]) ([aeiou])/g, "$1$2n $3");
result = result.trim();
result = kit.capitalizeFirstLetter(result);
// remove spaces before commas/periods/semicolons
result = result.replace(/ ([,\.;\?])/g, "$1");
// take care of prefixes (delete the space after the hyphen)
result = result.replace(/- /g, "-");
// add space after question marks if they're mid-sentence
result = result.replace(/\?(\w)/g, "? $1");
return result;
},
generateText(numberOfSentences, sentenceTopic) {
var fullText = "";
for (var i = 0; i < numberOfSentences; i++) {
fullText += this.generateSentence(sentenceTopic);
// if the topic has been deleted, pick another topic
if (typeof this.sentencePool[sentenceTopic] == "undefined") {
sentenceTopic = kit.randomInt(this.sentencePool.length - 1);
}
}
// insert a space between sentences (after periods and question marks)
fullText = fullText.replace(/([\.\?])(\w)/g, "$1 $2");
return fullText;
}
};
$(" .topbar button ").hover(function() {
$(this).removeClass("glowjump");
});
// Page interaction
$(" .topbar button ").click(function() {
$(" .page-flash ")
.show(1, function() {
// generate random topic
var sentenceTopic = 0;
$(" #main-heading ").text(bs.generateText(1, sentenceTopic));
$(" #sub-heading ").text(bs.generateText(2, sentenceTopic));
sentenceTopic = kit.randomInt(bs.sentencePool.length - 2);
$(" #third-heading ").text(bs.generateText(1, sentenceTopic));
$(" p ").each(function(i) {
sentenceTopic = kit.randomInt(bs.sentencePool.length - 1);
$(this).text(bs.generateText(3, sentenceTopic));
});
sentenceTopic = kit.randomInt(bs.sentencePool.length - 1);
$(" #quote ").text(bs.generateText(1, sentenceTopic));
// change image
$(" #nature-image ").fadeTo(1, 0, function() {
$(this)
.attr(
"src",
"http://placeimg.com/640/480/nature?" +
Math.floor(Math.random() * 100)
)
.bind("onreadystatechange load", function() {
if (this.complete) $(this).fadeTo(1000, 1);
});
});
})
.fadeOut(1000);
bs.initializeSentencePool();
});