Rewrite `String#camelize` using `String#replace` with a replacement function [#297 state:resolved]

This commit is contained in:
Samuel Lebeau 2009-09-01 15:59:47 +02:00 committed by Juriy Zaytsev
parent cfc7e7a2e7
commit f4ea4c6ef7
1 changed files with 3 additions and 11 deletions

View File

@ -311,17 +311,9 @@ Object.extend(String.prototype, (function() {
* // -> 'MozBinding'
**/
function camelize() {
var parts = this.split('-'), len = parts.length;
if (len == 1) return parts[0];
var camelized = this.charAt(0) == '-'
? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
: parts[0];
for (var i = 1; i < len; i++)
camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
return camelized;
return this.replace(/-+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
}
/**