Created
October 1, 2012 18:59
-
-
Save davidjbeveridge/3813724 to your computer and use it in GitHub Desktop.
Underscore and Camelize in CoffeeScript
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
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