Skip to content

Instantly share code, notes, and snippets.

@davidjbeveridge
Created October 1, 2012 18:59
Show Gist options
  • Save davidjbeveridge/3813724 to your computer and use it in GitHub Desktop.
Save davidjbeveridge/3813724 to your computer and use it in GitHub Desktop.
Underscore and Camelize in CoffeeScript
each = (arr, func, index=0) ->
if index < arr.length then [ func(arr[index], index), each(arr, func, index + 1)... ] else []
camelize = (input) ->
pieces = input.split(/[\W_-]/)
each(pieces, capitalize).join("")
capitalize = (input) ->
input.charAt(0).toUpperCase() + input.slice(1)
lowercase = (input) ->
input.toLowerCase()
underscore = (input) ->
pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n
each(pieces, lowercase ).join("_")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment