Skip to content

Instantly share code, notes, and snippets.

@timyates
Created February 19, 2014 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timyates/9090514 to your computer and use it in GitHub Desktop.
Save timyates/9090514 to your computer and use it in GitHub Desktop.
CamelCase, Snake_case and caterpillar-case with Guava
@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