Skip to content

Instantly share code, notes, and snippets.

@abstracthat
abstracthat / unspin.coffee
Last active March 22, 2018 17:01
Unspin Spintax with Coffeescript
unspin = (text) ->
# spintax regex: "{" + anything not "{" or "}" + "}"
findSpintax = -> text.match /{[^{}]+?}/g
# while we find spintax keep unspinning it
while findSpintax text
# find spintax chunks and work on the first one
spintax = (findSpintax text)[0]
# put the spintax options in an array
options = spintax.split '|'
# choose a random index based on the array length