Created
January 12, 2015 22:42
-
-
Save sma/a6aef167d38cb7f42112 to your computer and use it in GitHub Desktop.
expands a string template with random values from arrays
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
function generate(data) { | |
function oneOf(ary) { return ary[Math.floor(Math.random() * ary.length)] } | |
return data.template.replace(/@(\w+)/g, function (_, m) { return oneOf(data[m]) }) | |
} | |
console.log(generate({ | |
template: "@descriptor @type who @focus", | |
descriptor: ["appealing","brash","clever"], | |
type: ["vector","paradox","spinner"], | |
focus: ["looks for trouble","operates under cover","solves mysteries"] | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment