Created
May 3, 2014 15:41
-
-
Save sma/8c170de6c652028a72b3 to your computer and use it in GitHub Desktop.
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 choice(s) { | |
function tokenize() { | |
var st = [[]]; | |
s.replace(/[{|}]|[^{|}]+/g, function (m) { | |
if (m === '{') st.unshift([], []); | |
else if (m === '}') st[0].push(st.shift()), st[1].push(st.shift()); | |
else if (m === '|') st[1].push(st.shift()), st.unshift([]); | |
else st[0].push(m); | |
}); | |
return st[0]; | |
} | |
function render(t) { | |
return t.map(function (a) { | |
return typeof a === "string" ? a : render(a[Math.floor(Math.random() * a.length)]); | |
}).join(""); | |
} | |
return render(tokenize(s)); | |
} | |
console.log(choice("In diesem Raum {schweben|hängen|liegen|stehen|schwimmen|bewegen sich {langsam|wild|wie in Zeitlupe|plötzlich}} {farbige|transparente|schillernde|ölige|weiße|schwarze|warme|heiße|kalte|metallische} {Cuben|Zylinder|Sphären|Kisten|Container|Pflanzen|Maschinen|Flüssigkeiten}. Es ist möglicherweise ein {Vorrats|Aufenthalts|Überwachungs|Schlaf|Reparatur|Wohn}raum.")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment