Quantcast

Utility methods for camelcase conversion?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Utility methods for camelcase conversion?

John Moore
Somewhere or other I'm sure I've seen a handy little pair of methods for
converting between camelCase/titleCase as used in property names and
lowercase with underscore separators, as used in column names. I.e.,
something which will give me this:

myPropertyName > my_property_name
my_property_name > myPropertyName

Can't lay my hands on them at the moment. Could someone please jog my
memory? Thanks.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

Jeff Scott Brown
On Fri, Jan 6, 2012 at 11:47 AM, John Moore <[hidden email]> wrote:

> Somewhere or other I'm sure I've seen a handy little pair of methods for
> converting between camelCase/titleCase as used in property names and
> lowercase with underscore separators, as used in column names. I.e.,
> something which will give me this:
>
> myPropertyName > my_property_name
> my_property_name > myPropertyName
>
> Can't lay my hands on them at the moment. Could someone please jog my
> memory? Thanks.
>

Does https://github.com/grails/grails-core/blob/master/grails-bootstrap/src/main/groovy/grails/util/GrailsNameUtils.java
give you what you are looking for?



jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

John Moore
On 06/01/12 18:10, Jeff Brown wrote:
>
> Does https://github.com/grails/grails-core/blob/master/grails-bootstrap/src/main/groovy/grails/util/GrailsNameUtils.java
> give you what you are looking for?
>
>
Hm, not quite (it's hyphen separated not underscore separated, and only
one way). But it's given me some ideas, and it's probably what I
remember seeing before, thanks.

John

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

John Moore
In reply to this post by John Moore
On 06/01/12 17:47, John Moore wrote:
> Somewhere or other I'm sure I've seen a handy little pair of methods
> for converting between camelCase/titleCase as used in property names
> and lowercase with underscore separators, as used in column names.
> I.e., something which will give me this:
>
> myPropertyName > my_property_name
> my_property_name > myPropertyName
>

Half of what I want could be done with this:

String underscoreSeparatedCase (String s) {
     return s.replaceAll(/\B[A-Z]/) { '_' + it }.toLowerCase()
}

Now for the other half...

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

Jeff Scott Brown
In reply to this post by John Moore
On Fri, Jan 6, 2012 at 12:18 PM, John Moore <[hidden email]> wrote:

> On 06/01/12 18:10, Jeff Brown wrote:
>>
>>
>> Does
>> https://github.com/grails/grails-core/blob/master/grails-bootstrap/src/main/groovy/grails/util/GrailsNameUtils.java
>> give you what you are looking for?
>>
>>
> Hm, not quite (it's hyphen separated not underscore separated, and only one
> way). But it's given me some ideas, and it's probably what I remember seeing
> before, thanks.
>
> John
>
>

Right.  I didn't read your original note carefully enough.  I
(wrongly) thought you were trying to go back and forth between
something like MyDemoController to myDemoController.

Another thing that is related but still not really what you are
looking for is https://github.com/grails/grails-core/blob/master/grails-core/src/main/groovy/grails/web/HyphenatedUrlConverter.java.
 That converts something like SomeClassName to some-class-name.  That
of course could be easily adapted to use underscores instead of
hyphens.  It only goes in one direction though.  It doesn't help you
go from some-class-name to anything else.

The spec that describes the expected behavior is at
https://github.com/grails/grails-core/blob/master/grails-core/src/test/groovy/grails/web/HyphenatedUrlConverterSpec.groovy.

Again, not really what you need but maybe some food for thought.



jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

John Moore
I actually ended up with these methods, one adapted from a suggestion on
the Groovy mailing list, the other from the utility class Jeff
mentioned. They do the job, although could probably be optimized.

--------------------------------------------------------------

public static String underscoreSeparatedCase(String s) {
     return s.replaceAll(/\B[A-Z]/) { '_' + it }.toLowerCase()
}

public static String propertyNameCase(String s) {
     // Handle null and empty strings.
     if(s == null || s.trim().length() == 0 ) return s;
     String str;
     if (s.indexOf('_') != -1) {

         StringBuilder buf = new StringBuilder();
         String[] tokens = s.split("_");
         for (String token: tokens) {
             if (token == null || token.length() == 0) continue;
             buf.append(token.substring(0, 1).toUpperCase()).append(token.substring(1));
         }
         str = buf.toString();
     } else {
         str = s;
     }
     if (str.length()>  0) {
         str = str.substring(0, 1).toLowerCase() + str.substring(1);
     }
     return str;
}



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

Giancarlo Angulo
These are useful. Thanks for the share.
================================
                   Giancarlo Angulo
================================



On Sat, Jan 7, 2012 at 4:23 AM, John Moore <[hidden email]> wrote:
I actually ended up with these methods, one adapted from a suggestion on the Groovy mailing list, the other from the utility class Jeff mentioned. They do the job, although could probably be optimized.

--------------------------------------------------------------

public static String underscoreSeparatedCase(String s) {

   return s.replaceAll(/\B[A-Z]/) { '_' + it }.toLowerCase()
}

public static String propertyNameCase(String s) {
   // Handle null and empty strings.
   if(s == null || s.trim().length() == 0 ) return s;
   String str;
   if (s.indexOf('_') != -1) {

       StringBuilder buf = new StringBuilder();
       String[] tokens = s.split("_");
       for (String token: tokens) {
           if (token == null || token.length() == 0) continue;
           buf.append(token.substring(0, 1).toUpperCase()).append(token.substring(1));
       }
       str = buf.toString();
   } else {
       str = s;
   }
   if (str.length()>  0) {
       str = str.substring(0, 1).toLowerCase() + str.substring(1);
   }
   return str;

}



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

iamclyde
This post was updated on .
In reply to this post by John Moore
Know this is an old thread, but this worked for me going from a delimited string to camel case.

def yep = new StringBuffer()

'Im_Delimited'.split('_').eachWithIndex { value, index -> yep << ((index > 0) ? value.toLowerCase().capitalize() : value.toLowerCase()) }

def camelCase = yep.toString()
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Utility methods for camelcase conversion?

joasgarcia
In reply to this post by John Moore
I did a little improvement in the John Moore code to handle upper cased strings. See here: https://gist.github.com/joasgarcia/dcb6080273202427227b 
Loading...