As seen in the post Variety of coding and coding approaches that a developer may have to solve a problem are multiple and diverse for 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, explicitly proposes a 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 it was not made any use of Regular Expression!
1 2 3 4 5 6 7 8 9 10 11 12 13 | ( ) { 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 at this interesting discussion .
mootools
The 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.
1 2 3 4 5 | ( ) { 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, peering into the code, I found this:
1 | name . replace ( /\-(\w)/g , function ( all , letter ) { return letter. toUpperCase ( ) ; } ) ; var name = camelCase. replace (/ \ - (\ w) / g, function (all, letter) {return letter. toUpperCase ();}); |
In addition, as a plugin, jQuery offers a String prototype extensions which adds, in fact, a function camelize() :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | / ** * Return to camelized String, removing all underscores and dashes * Replacing the next and with it's uppercase character 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 account the character underscore (_) , which makes them more complete.










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