Skip to content

Instantly share code, notes, and snippets.

@ericoporto
Last active September 17, 2015 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericoporto/ca36a2210412a81cf472 to your computer and use it in GitHub Desktop.
Save ericoporto/ca36a2210412a81cf472 to your computer and use it in GitHub Desktop.
simple way to generate a random phrase from easy to type sentence
// An easy way to generate random text from easy to type text
//
// Use "word word [possibility1|possibilities stuff|another stuff thing] stuff"
//to make a string with possibilities.
// Example:
//
//> var t = "A [greekish|futuristic|antique] world with a [dinossaur|ninja] flying"
//> console.log(randtext(t))
//A antique world with a dinossaur flying
//
function randtext(text) {
var t = text.split(/[\[\]]+/).filter(function(e) { return e; });
var sentence = ""
for (i = 0; i < t.length; i++) {
if(t[i].indexOf('|') === -1 ){
sentence += t[i]
} else {
var possibles = t[i].split('|')
sentence += possibles[Math.floor(Math.random()*possibles.length)]
}
}
return sentence.replace(/ +(?= )/g,'');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment