Camelize, CamelCase
Monday, October 20, 2008As 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!
- ( ) { camelize: function () (
- this . split ( '-' ) , len = parts. length ; var parts = this. split ( '-'), len = parts. length;
- len == 1 ) return parts [ 0 ] ; if (len == 1) return parts [0];
- this . charAt ( 0 ) == '-' camelized var = this. charAt (0) == '-'
- 0 ] . charAt ( 0 ) . toUpperCase ( ) + parts [ 0 ] . substring ( 1 ) ? Parts [0]. CharAt (0). ToUpperCase () + parts [0]. Substring (1)
- 0 ] ; : Parts [0];
- var i = 1 ; i < len ; i ++ ) for (var i = 1; i <len; i + +)
- i ] . charAt ( 0 ) . toUpperCase ( ) + parts [ i ] . substring ( 1 ) ; camelized + = parts [i]. charAt (0). toUpperCase () + parts [i]. substring (1);
- camelized return;
- )
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.
- ( ) { camelCase: function () (
- ( /-\D/g , function ( match ) { return this. replace (/ - \ D / g, function (match) (
- ( 1 ) . toUpperCase ( ) ; return match. charAt (1). toUpperCase ();
- ));
- )
jQuery
jQuery, version 1.2.6, does not propose an explicit method. However, looking in the code, I found this:
- 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()
- / **
- * Return to camelized String, removing all underscores and dashes
- * And replacing it with the next character's uppercase representation.
- *
- * @ Example "font-weight". Camelize ()
- * @ Result "fontWeight"
- *
- * @ Example "border_width_bottom. Camelize ()
- * @ Result "borderWidthBottom"
- *
- * @ Example "border_width-bottom". Camelize ()
- * @ Result "borderWidthBottom"
- *
- * @ Name camelize
- * @ Type String
- * @ Cat JavaScript / String
- * /
- , function ( ) { add ( "camelize", function () (
- ( /[-_]([az])/ig , function ( z , b ) { return b. toUpperCase ( ) ; } ) ; return this. replace (/[-_]([ az]) / ig, function (z, b) (return b toUpperCase ();));
- ));
This, in particular, takes into consideration the character underscore (_) thus being more complete.













[...] 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 [...]