-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (123 loc) · 3.54 KB
/
main.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
var hiphens = ['-powered', '-based', '-enabled'];
var words = ['AI', 'ML', 'Blockchain', 'IOT', 'Cloud', 'VR', 'AR', 'NLP'];
/**
* Returns a random element from a list.
* @param {Array} list
* @return {Any}
*/
function getRandomValue(list) {
let random = parseInt(Math.random() * 100);
return list[random % list.length];
}
/**
* Generates a buzzword.
* @return {String}
*/
function generateBuzzword() {
return `${getRandomValue(words)}${getRandomValue(hiphens)}`;
}
/**
* Returns whether or not a string starts with a vowel.
* @param {String} str
* @return {Boolean}
*/
function startsWithVowel(str) {
var vowels = ['a', 'e', 'i', 'o', 'u'];
return vowels.indexOf(str.toLowerCase()[0]) >= 0;
}
/**
* Return "A" or "An" to be used based on the word.
* @param {String} word Word for which to get A or An.
* @param {String} existing Existing word occuring before the word (Used to determine case)
* @return {String}
*/
function getAOrAn(word, existing) {
if (!existing) {
existing = 'A';
}
// Does `existing` start with upper case?
var startsWithUpperCase = existing[0].toUpperCase() === existing[0];
// First part of `word`
var first = word.split('-')[0];
// Is `first` an abbreviation?
var isAbbr = first === first.toUpperCase();
if (isAbbr) {
return startsWithUpperCase ? 'An' : 'an';
}
console.log('word', word);
if (startsWithVowel(word)) {
return startsWithUpperCase ? 'An' : 'an';
} else {
return startsWithUpperCase ? 'A' : 'a';
}
}
/**
* Buzzwordifies a pitch.
* @param {String} pitch
* @return {String}
*/
function buzzwordify(pitch) {
// Store for used buzzwords.
var usedWords = [];
var split = pitch.split(' ');
split = split.filter(function(s) {
return s.trim().length > 0;
});
lowerCaseSplit = split.map(function(s) {
return s.toLowerCase();
});
// Put buzzword as first word if pitch doesn't start with "A" or "An".
if (!(lowerCaseSplit[0] === 'a' || lowerCaseSplit[0] === 'an')) {
var currentWord = generateBuzzword();
usedWords.push(currentWord);
split.splice(0, 0, currentWord);
}
// Put buzzword as second word if pitch starts with "A" or "An".
else {
var currentWord = generateBuzzword();
usedWords.push(currentWord);
split.splice(1, 0, currentWord);
// Fix grammar.
split[0] = getAOrAn(currentWord, split[0]);
}
// Split has changed, map again.
lowerCaseSplit = split.map(function(s) {
return s.toLowerCase();
});
// Put some buzzword a word before any occuring "for".
var forIndex = lowerCaseSplit.indexOf('for');
if (forIndex > 0) {
var currentWord = generateBuzzword();
// Don't use already-used jargon.
while (usedWords.indexOf(currentWord) != -1) {
currentWord = generateHyphenatedJargon();
}
split.splice(forIndex - 1, 0, currentWord);
// Tries to satisfy Grammar Nazis a little bit.
if (forIndex - 2 >= 0) {
var s = split[forIndex - 2].toLowerCase();
if (s === 'a' || s === 'an') {
split[forIndex - 2] = getAOrAn(currentWord, split[forIndex - 2]);
}
}
}
// Generate pitch again.
pitch = split.join(' ');
return pitch;
}
/**
* Initializes page.
*/
function initPage () {
document.forms[0].addEventListener('submit', function (e) {
e.preventDefault();
var pitch = document.getElementById('pitch').value;
if (!pitch) return;
var newPitch = buzzwordify(pitch);
document.getElementById('buzzwordified').value = newPitch;
});
document.forms[1].addEventListener('submit', function (e) {
e.preventDefault();
});
}
initPage();