Last active
March 3, 2017 16:18
-
-
Save PatrickCLipscomb/674c6d1b3cc7e4188d48dadf185cf29e to your computer and use it in GitHub Desktop.
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
// this code is from a personal lorem ipsum text generation project I built for fun on my own time. The app focuses heavily on the UI and creating a fun experience for the user. The getIpsum() function below creates paragraphs of themed ipsum for the application. | |
// github: https://github.com/PatrickCLipscomb/react-rails-wordplay | |
// heroku: https://railswordplay.herokuapp.com/ | |
getIpsum(paraNum) { | |
// sets the number of paragraphs of ipsum to return | |
var paragraphNumber = parseInt(paraNum); | |
var ipsumParagraphArray = []; | |
// pulls in the phrases from the selected ipsum. the phrases exist as an array of strings somethimes with several words per phrase | |
var phrases = this.state.activeIpsum.phrases | |
var _this = this; | |
// will loop to create ipsum paragraphs for a given number of paragraphs | |
for (var paraNum = 0; paraNum < paragraphNumber; paraNum++) { | |
var ipsumStringArray = [] | |
// this is currently set to create ipsum paragraphs proportional to the number of phrases in an ipsum theme and can be easily changed to return paragraphs of any given length. The length will be augmented by the randomly ocurring fillerWord()and punctuation()functions below | |
for (var i = 0; i < phrases.length; i++) { | |
ipsumStringArray.push(phrases[Math.floor(Math.random() * phrases.length)] + ' '); | |
// gives a 35% chance that a fillerWord will be added to the ipsum and then a random phrase from the selected ipsum theme | |
if (Math.floor(Math.random() * 100) < 35) { | |
ipsumStringArray.push(_this.fillerWord()); | |
ipsumStringArray.push(phrases[Math.floor(Math.random() * phrases.length)] + ' '); | |
} | |
// gives a 22% chance that punctuation and then a capitalized ipsum phrase will be added following a standard ipsum phrase | |
if (ipsumStringArray.length > 3 && Math.floor(Math.random() * 100) < 22) { | |
ipsumStringArray.push(phrases[Math.floor(Math.random() * phrases.length)]); | |
ipsumStringArray.push(_this.punctuation()) | |
ipsumStringArray.push(_this.capitalize(phrases[Math.floor(Math.random() * phrases.length)]) + ' '); | |
} | |
} | |
ipsumStringArray.push(phrases[Math.floor(Math.random() * phrases.length)]); | |
// this finishes off the paragraph with punctuation and removes any white space | |
ipsumStringArray.push(_this.punctuation().trim()) | |
// this takes the first ipsum phrase in the paragraph and capitalizes it | |
var firstWord = _this.capitalize(ipsumStringArray.shift()); | |
ipsumStringArray.unshift(firstWord); | |
// joins the phrases and punctuation from an array into a string | |
let ipsumString = ipsumStringArray.join(""); | |
ipsumParagraphArray.push(ipsumString) | |
} | |
this.setState({currentIpsum: ipsumParagraphArray}); | |
} | |
// capitalization method | |
capitalize(word) { | |
return word.charAt(0).toUpperCase() + word.slice(1); | |
} | |
// this method provides gatIpsum()with random punctuation | |
punctuation() { | |
var puncArray = ['. ', '! ', '? ', '. ']; | |
return puncArray[Math.floor(Math.random() * 4)]; | |
} | |
// this method provides gatIpsum() with a random filler word | |
fillerWord() { | |
var theFiller; | |
var fillerWords = ['and', 'or', 'while', 'then', 'and', 'where'] | |
if (Math.floor(Math.random() * 100) < 40) { | |
theFiller = fillerWords[Math.floor(Math.random() * 6)] | |
theFiller = theFiller.concat(', '); | |
} else { | |
theFiller = fillerWords[Math.floor(Math.random() * 6)] + ' '; | |
} | |
return theFiller; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment