Skip to content

Instantly share code, notes, and snippets.

@timyates
Created September 28, 2012 12:48
Show Gist options
  • Save timyates/3799608 to your computer and use it in GitHub Desktop.
Save timyates/3799608 to your computer and use it in GitHub Desktop.
Use Guava to convert string formats
@Grab( 'com.google.guava:guava:13.0.1' )
import static com.google.common.base.CaseFormat.*
String.metaClass.caseFormat = { from, to ->
from.to( to, delegate )
}
assert 'varName'.caseFormat( LOWER_CAMEL, UPPER_UNDERSCORE ) == 'VAR_NAME'
assert 'var-name'.caseFormat( LOWER_HYPHEN, UPPER_CAMEL ) == 'VarName'
assert 'var_name'.caseFormat( LOWER_UNDERSCORE, LOWER_CAMEL ) == 'varName'
assert 'VAR_NAME'.caseFormat( UPPER_UNDERSCORE, LOWER_UNDERSCORE ) == 'var_name'
assert 'VarName'.caseFormat( UPPER_CAMEL, LOWER_HYPHEN ) == 'var-name'
@danveloper
Copy link

this is awesome and great to know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment