Created
February 8, 2014 07:36
-
-
Save nbashaw/8878036 to your computer and use it in GitHub Desktop.
Ipsum.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create our IpsumGenerator function | |
var IpsumGenerator = function(){}; | |
// Master method that gets called to generate the ipsum based on a set | |
// of words and a desired word count. | |
IpsumGenerator.prototype.generateIpsum = function(words, wordCount) { | |
this.wordList = []; | |
this.sentences = []; | |
this.paragraphs = []; | |
this.generateWordlist(words, wordCount); | |
this.generateSentencesFromWordlist(); | |
this.generateParagraphsFromSentences(); | |
// Return the Ipsum string | |
ipsum = ''; | |
this.paragraphs.forEach(function(paragraph) { | |
ipsum += paragraph.join(' '); | |
ipsum += "\n\n"; | |
}); | |
return ipsum; | |
}; | |
// Creates an array n elements long, where n = wordCount | |
// Each element is a random word from the set of words given by the user | |
IpsumGenerator.prototype.generateWordlist = function(words, wordCount) { | |
var wordArray = words.split(' '); | |
for (i=0; i < wordCount; i++) { | |
var randomWord = wordArray[Math.floor(Math.random() * wordArray.length)]; | |
this.wordList.push(randomWord); | |
} | |
}; | |
// Split the wordList into sentences that are between 5 and 15 words long | |
IpsumGenerator.prototype.generateSentencesFromWordlist = function() { | |
var cursor = 0; | |
while (cursor <= this.wordList.length) { | |
var sentence = []; | |
var sentenceLength = Math.floor((Math.random() * 10) + 5); | |
for (var i = 0; i < sentenceLength; i++) { | |
var word = this.wordList[i + cursor]; | |
if (word !== undefined) { | |
sentence.push(word); | |
} | |
} | |
this.sentences.push(sentence); | |
cursor += sentenceLength; | |
} | |
}; | |
// Split the sentences into paragraphs that are between 2 and 8 sentences long | |
// Also format the sentences to be capitalized and end with a period | |
IpsumGenerator.prototype.generateParagraphsFromSentences = function() { | |
var cursor = 0; | |
while (cursor <= this.sentences.length) { | |
var paragraph = []; | |
var paragraphLength = Math.floor((Math.random() * 6) + 2); | |
for (var i = 0; i < paragraphLength; i++) { | |
var sentence = this.sentences[i + cursor]; | |
if (sentence !== undefined) { | |
sentence = sentence.join(' ') + '.'; | |
sentence = sentence.charAt(0).toUpperCase() + sentence.slice(1); | |
sentence[0] = sentence[0].toUpperCase(); | |
paragraph.push(sentence); | |
} | |
} | |
this.paragraphs.push(paragraph); | |
cursor += paragraphLength; | |
} | |
}; | |
// Instantiate the ipsum generator, set up our Nietzsche-flavored string, and generate the nietzsche ipsum! | |
ipsumGen = new IpsumGenerator(); | |
nietzscheWords = 'society joy madness morality snare truth abstract depths faithful '; | |
nietzscheWords += 'victorious sexuality ultimate pinnacle spirit good evil overcome '; | |
nietzscheWords += 'christianity love hatred war ubermensch play suicide contradict battle'; | |
ipsum = ipsumGen.generateIpsum(nietzscheWords, 200); | |
console.log(ipsum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment