Skip to content

Instantly share code, notes, and snippets.

@gnab
Last active January 3, 2016 20:38
Show Gist options
  • Save gnab/8515838 to your computer and use it in GitHub Desktop.
Save gnab/8515838 to your computer and use it in GitHub Desktop.
Sweet.js random expression macro
macro random {
case { _ ($expr:expr (,) ...) } => {
var matches = match.patternEnv.$expr.match;
var randomIndex = Math.floor(Math.random() * matches.length);
var randomMatch = matches[randomIndex].match;
return randomMatch
}
}
random(console.log(1), console.log(2), console.log(3))
// => console.log(1) OR console.log(2) OR console.log(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment