Created
December 23, 2015 17:09
-
-
Save nickman/ad1ffa4dc1f0a884d107 to your computer and use it in GitHub Desktop.
Groovy to proper case underscore containing strings (i.e. DB column names) to convert to reasonable java/groovy field names
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
int US = '_'; | |
uinds = { name -> | |
int index = 1; | |
int offset = 0; | |
int loops = 0; | |
uids = []; | |
while(index!=-1) { | |
index = name.indexOf(US, offset); | |
if(index!=-1) { | |
uids.add(index-loops); | |
offset += index+1; | |
} | |
loops++; | |
} | |
return uids as int[]; | |
} | |
pc = { name -> | |
int[] u = uinds(name); | |
s = name.toLowerCase().replace("_", ""); | |
if(u.length > 0) { | |
StringBuilder b = new StringBuilder(s); | |
u.each() { index -> | |
int nextChar = index+1; | |
if(nextChar <= b.length()) { | |
upchar = b.substring(index, nextChar).toUpperCase(); | |
b.replace(index, index+1, upchar); | |
} | |
} | |
s = b.toString(); | |
} | |
return s; | |
} | |
println pc("APEX_STANDARD_CONDITIONS"); | |
println pc("APEX_STANDARD_CONDITIONS_PLUS"); | |
println pc("APEX"); | |
println pc("APEX_"); | |
return null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment