Created
February 19, 2014 11:54
-
-
Save timyates/9090514 to your computer and use it in GitHub Desktop.
CamelCase, Snake_case and caterpillar-case with Guava
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
@Grab( 'com.google.guava:guava:16.0.1' ) | |
import static com.google.common.base.CaseFormat.* | |
String.metaClass.caseFormat = { from, to -> | |
from.to( to, delegate ) | |
} | |
// From camelCase (LOWER_CAMEL) to SNAKE_CASE (UPPER_UNDERSCORE) | |
assert 'varName'.caseFormat( LOWER_CAMEL, UPPER_UNDERSCORE ) == 'VAR_NAME' | |
// From caterpillar-case (LOWER_HYPHEN) to CamelCase (UPPER_CAMEL) | |
assert 'var-name'.caseFormat( LOWER_HYPHEN, UPPER_CAMEL ) == 'VarName' | |
// from snake_case (LOWER_UNDERSCORE) to camelCase (LOWER_CAMEL) | |
assert 'var_name'.caseFormat( LOWER_UNDERSCORE, LOWER_CAMEL ) == 'varName' | |
// from SNAKE_CASE (UPPER_UNSERSCORE) to snake_case (LOWER_UNDERSCORE) | |
assert 'VAR_NAME'.caseFormat( UPPER_UNDERSCORE, LOWER_UNDERSCORE ) == 'var_name' | |
// from CamelCase (UPPER_CAMEL) to caterpillar-case (LOWER_HYPHEN) | |
assert 'VarName'.caseFormat( UPPER_CAMEL, LOWER_HYPHEN ) == 'var-name' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment