Last active
September 20, 2023 09:51
-
-
Save jgphilpott/19e7a94cdf6d6d4cd868cc18e628026c to your computer and use it in GitHub Desktop.
A collection of functions for formatting strings into different cases.
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
lower = (string) -> | |
return string.toLowerCase().trim() | |
upper = (string) -> | |
return string.toUpperCase().trim() | |
# Credit: https://stackoverflow.com/a/1026087/1544937 | |
capitalize = (string) -> | |
return upper(string.trim().charAt(0)) + lower(string).slice(1) | |
# Credit: https://stackoverflow.com/a/52551910/1544937 | |
camelize = (string) -> | |
return lower(string).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, (match, char) -> upper(char)) | |
# Credit: https://stackoverflow.com/a/52551910/1544937 | |
pascalize = (string) -> | |
return (" " + lower(string)).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, (match, char) -> upper(char)) | |
# Credit: https://stackoverflow.com/a/52964182/1544937 | |
snakify = (string) -> | |
return string.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map((word) -> lower(word)).join("_") | |
# Credit: https://stackoverflow.com/a/52964182/1544937 | |
kebabify = (string) -> | |
return string.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map((word) -> lower(word)).join("-") | |
# Credit: https://stackoverflow.com/a/52551910/1544937 | |
titlize = (string) -> | |
return upper(string.trim().charAt(0)) + lower(string).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, (match, char) -> " " + upper(char)).slice(1) | |
# Credit: https://www.pbs.org/wnet/nature/blog/camel-fact-sheet/#:~:text=One%20of%20the%20camel's%20most,used%20as%20an%20energy%20source. | |
bactrianize = pascalize | |
dromedize = camelize |
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
var bactrianize, camelize, capitalize, dromedize, kebabify, lower, pascalize, snakify, titlize, upper; | |
lower = function(string) { | |
return string.toLowerCase().trim(); | |
}; | |
upper = function(string) { | |
return string.toUpperCase().trim(); | |
}; | |
// Credit: https://stackoverflow.com/a/1026087/1544937 | |
capitalize = function(string) { | |
return upper(string.trim().charAt(0)) + lower(string).slice(1); | |
}; | |
// Credit: https://stackoverflow.com/a/52551910/1544937 | |
camelize = function(string) { | |
return lower(string).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, function(match, char) { | |
return upper(char); | |
}); | |
}; | |
// Credit: https://stackoverflow.com/a/52551910/1544937 | |
pascalize = function(string) { | |
return (" " + lower(string)).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, function(match, char) { | |
return upper(char); | |
}); | |
}; | |
// Credit: https://stackoverflow.com/a/52964182/1544937 | |
snakify = function(string) { | |
return string.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map(function(word) { | |
return lower(word); | |
}).join("_"); | |
}; | |
// Credit: https://stackoverflow.com/a/52964182/1544937 | |
kebabify = function(string) { | |
return string.trim().replace(/\W+/g, " ").split(/ |\B(?=[A-Z])/).map(function(word) { | |
return lower(word); | |
}).join("-"); | |
}; | |
// Credit: https://stackoverflow.com/a/52551910/1544937 | |
titlize = function(string) { | |
return upper(string.trim().charAt(0)) + lower(string).replace(/[^a-zA-ZÀ-ÖØ-öø-ÿ0-9]+(.)/g, function(match, char) { | |
return " " + upper(char); | |
}).slice(1); | |
}; | |
// Credit: https://www.pbs.org/wnet/nature/blog/camel-fact-sheet/#:~:text=One%20of%20the%20camel's%20most,used%20as%20an%20energy%20source. | |
bactrianize = pascalize; | |
dromedize = camelize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These functions can also be found in my
String
prototype gist.