Camelize, CamelCase

Monday, October 20, 2008

As seen in the post Variety of coding and coding approaches that a developer may have to solve a problem are varied and many of the same programming language used. Here's how some of the most popular JavaScript frameworks have solved a simple function of CamelCase:

Prototype.js

Prototype.js, version 1.6.0.3, propose an explicit method camelize() to make the camelcase on a string. The author's approach is quite simple and the code is self-explanatory. In this case was not made any use of Regular Expression!

JavaScript:
  1. ( ) { camelize: function () (
  2. this . split ( '-' ) , len = parts. length ; var parts = this. split ( '-'), len = parts. length;
  3. len == 1 ) return parts [ 0 ] ; if (len == 1) return parts [0];
  4. this . charAt ( 0 ) == '-' camelized var = this. charAt (0) == '-'
  5. 0 ] . charAt ( 0 ) . toUpperCase ( ) + parts [ 0 ] . substring ( 1 ) ? Parts [0]. CharAt (0). ToUpperCase () + parts [0]. Substring (1)
  6. 0 ] ; : Parts [0];
  7. var i = 1 ; i < len ; i ++ ) for (var i = 1; i <len; i + +)
  8. i ] . charAt ( 0 ) . toUpperCase ( ) + parts [ i ] . substring ( 1 ) ; camelized + = parts [i]. charAt (0). toUpperCase () + parts [i]. substring (1);
  9. camelized return;
  10. )

Take a look also at this interesting debate.

mootools

Library mootools, version 1.2, also proposes a method camelCase() In this case the function is considerably more compact than that proposed by Prototype.js, thanks to the use of Regular Expression.

JavaScript:
  1. ( ) { camelCase: function () (
  2. ( /-\D/g , function ( match ) { return this. replace (/ - \ D / g, function (match) (
  3. ( 1 ) . toUpperCase ( ) ; return match. charAt (1). toUpperCase ();
  4. ));
  5. )

jQuery

jQuery, version 1.2.6, does not propose an explicit method. However, looking in the code, I found this:

JavaScript:
  1. name . replace ( /\-(\w)/g , function ( all , letter ) { return letter. toUpperCase ( ) ; } ) ; var = name camelCase. replace (/ \ - (\ w) / g, function (all, letter) (return letter. toUpperCase ();));

Moreover, as a plugin, jQuery offers a String prototype extensions where added, precisely, a function camelize()

JavaScript:
  1. / **
  2. * Return to camelized String, removing all underscores and dashes
  3. * And replacing it with the next character's uppercase representation.
  4. *
  5. * @ Example "font-weight". Camelize ()
  6. * @ Result "fontWeight"
  7. *
  8. * @ Example "border_width_bottom. Camelize ()
  9. * @ Result "borderWidthBottom"
  10. *
  11. * @ Example "border_width-bottom". Camelize ()
  12. * @ Result "borderWidthBottom"
  13. *
  14. * @ Name camelize
  15. * @ Type String
  16. * @ Cat JavaScript / String
  17. * /
  18. , function ( ) { add ( "camelize", function () (
  19. ( /[-_]([az])/ig , function ( z , b ) { return b. toUpperCase ( ) ; } ) ; return this. replace (/[-_]([ az]) / ig, function (z, b) (return b toUpperCase ();));
  20. ));

This, in particular, takes into consideration the character underscore (_) thus being more complete.

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

One comment to "Camelize, CamelCase"

  1. getAvatar 1.0
    May 28, 2009 Undolog.com "Very short snippet: PHP slug:

    [...] In "this-and-a-post". The transformation is similar to that of the slug camelize or camelcase, seen here. Here's a possible implementation in PHP: PLAIN TEXT [...]

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL