Skip to content

Instantly share code, notes, and snippets.

@sma
Created January 12, 2015 22:42
Show Gist options
  • Save sma/a6aef167d38cb7f42112 to your computer and use it in GitHub Desktop.
Save sma/a6aef167d38cb7f42112 to your computer and use it in GitHub Desktop.
expands a string template with random values from arrays
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