Last active
September 17, 2015 00:54
-
-
Save ericoporto/ca36a2210412a81cf472 to your computer and use it in GitHub Desktop.
simple way to generate a random phrase from easy to type sentence
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
// 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