Skip to content

Instantly share code, notes, and snippets.

@tsaniel
Forked from 140bytes/LICENSE.txt
Created July 20, 2011 09:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaniel/1094627 to your computer and use it in GitHub Desktop.
Save tsaniel/1094627 to your computer and use it in GitHub Desktop.
toCamelCase

toCamelCase

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

function f(
a, // the text, omitted if used as method
b, // placeholder for replace for the first letter of a word
c // placeholder for replace for the position
){
return
++c ?
b.toUpperCase()
:
(
a
||
this
).toLowerCase()
.replace(/[^a-z]+(.?)/g,f)
}
function f(a,b,c){return++c?b.toUpperCase():(a||this).toLowerCase().replace(/[^a-z]+(.?)/g,f)}
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 f(a,b,c){return++c?b.toUpperCase():(a||this).toLowerCase().replace(/[^a-z]+(.?)/g,f)}
document.getElementById( "ret" ).innerHTML = myFunction('this-is0just\nan!@#$%example')
</script>
@maettig
Copy link

maettig commented Nov 11, 2011

@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
Copy link

atk commented Nov 11, 2011

@maettig: wouldn't that fail on spaces?

@maettig
Copy link

maettig commented Nov 11, 2011

@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
Copy link
Author

tsaniel commented Nov 11, 2011

@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
Copy link

maettig commented Nov 11, 2011

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

@tsaniel
Copy link
Author

tsaniel commented Nov 12, 2011

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