Skip to content

@tsaniel /LICENSE.txt forked from 140bytes/LICENSE.txt
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
toCamelCase

toCamelCase

A simple function(or can be used as method) that makes texts lowerCamelCased. Separated by any non-alphabetic characters.

function(
a, // the text, omitted if used as method
b // undefined
){
return (a === b ? this : a + '') // either used as function or method.
.toLowerCase(a = '') // make letters lowercase and reuse a.
.replace(/([a-z])([a-z]*)/g, function(b,c,d){ // process words, cache them and their initials,
a += a ? c.toUpperCase() + d : b // then capitalize the word only if it is not the first word.
}),
a
}
function(a,b){return(a===b?this:a+"").toLowerCase(a="").replace(/([a-z])([a-z]*)/g,function(b,c,d){a+=a?c.toUpperCase()+d:b}),a}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "toCamelCase",
"description": "Make texts lowerCamelCased",
"keywords": [
"CamelCase",
"toCamelCase"
]
}
<!DOCTYPE html>
<title>toCamelCase</title>
<div>Expected value: <b>thisIsJustAnExample</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var myFunction = function(a){(a||this).toLowerCase(a="").replace(/([a-z])([a-z]*)/g,function(b,c,d){a+=a?c.toUpperCase()+d:b});return a};
document.getElementById( "ret" ).innerHTML = myFunction('this-is0just\nan!@#$%example')
</script>
@subzey

toCamelCase("a b c") returns "aBundefinedCundefined"
It can be fixed (and shortened) by replacing /([a-z])([a-z]+)?/g with /([a-z])([a-z]*)/g.

Another byte may be shaved by rearranging the return statement:
function(a){return(a||this).toLowerCase(a="").replace(/([a-z])([a-z]*)/g,function(b,c,d){a+=a?c.toUpperCase()+d:b}),a}

@tsaniel
Owner

Thanks @subzey! Changed.

@tsaniel
Owner

Thanks again @subzey, the second tip is really awesome!

@subzey

@tsaniel, not at all!
Another note: function works unexpectedly if argument is an empty string

@tsaniel
Owner

I have tried to fix, thanks!

@atk

you can shave another byte by using "top" instead of "this"...

@tsaniel
Owner

The this here is actually this.
For example,

String.prototype.toCamelCase = function f(a,b){return b?b.toUpperCase():(a||this).toLowerCase().replace(/[^a-z]+([a-z])/g,f)}
'this-is0just\nan!@#$%example'.toCamelCase();
@atk

If you are going to go prototypical on me, I'll pass. ;-)

@maettig

I tried to create a version that works without this (introduced some problems for me) and without a function name f in the global scope. (Update: It's not in the global scope but I simply don't like this.) It also strips non-letters from the end of the string (your version does not). It's 97 bytes, a little bit bigger than your attempt. If you don't care about the "non-letters at the end of string" case you can remove the ?.

function(a){return a.toLowerCase().replace(/[^a-z]+(.?)/g,function(a,b){return b.toUpperCase()})}
@tsaniel
Owner

@maettig nice try!
But the f doesn't leak to the global scope, see Named function expressions.

@maettig

@tsaniel, you can save 4 bytes by replacing [^a-z]+([a-z]) with [^a-z]+(.). (Note: This changes the behavior for some strings, e.g. "x12" becomes "x2".) But you can not use [^a-z]+(.?) to strip non-letters from the end of the string as in my approach without introducing some kind of typeof, e.g. ''+b===b. And this will make your version 1 byte longer than mine. ;-)

@atk

@maettig: wouldn't that fail on spaces?

@maettig

@atk, no, why? ' aa bb ' becomes 'AaBb' in my version (but 'AaBb ' in @tsaniel's current one). ''+b===b is mentioned in the Byte-saving Techniques wiki. It returns true if b is a string, no matter what's in the string.

@tsaniel
Owner

@maettig I've reflected the change, thanks!
By the way, we can use a variable c and the trick ++c rather than ''+b===b.

@maettig

Another cool trick. By the way, I still don't understand why the this is in the code?

@tsaniel
Owner

It's for the prototyping use.
See https://gist.github.com/1094627#gistcomment-56234.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.