Skip to content

Instantly share code, notes, and snippets.

@tlux
Last active December 17, 2015 06:39
Show Gist options
  • Select an option

  • Save tlux/5567242 to your computer and use it in GitHub Desktop.

Select an option

Save tlux/5567242 to your computer and use it in GitHub Desktop.
Ruby-like string interpolations for JavaScript/CoffeeScript
# "Hello, %{name}!".interpolate(name: 'Tobias') # => "Hello, Tobias!"
String::interpolate = (interpolations = {}) ->
@replace /%\{(.*?)\}/g, (whole, key) ->
camelizedKey = _(key).camelize()
camelizedKey = camelizedKey.charAt(0).toLowerCase() + camelizedKey.slice(1)
interpolations[camelizedKey] || interpolations[key] || ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment