A simple function(or can be used as method) that makes texts lowerCamelCased. Separated by any non-alphabetic characters.
-
-
Save tsaniel/1094627 to your computer and use it in GitHub Desktop.
toCamelCase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function f(a,b,c){return++c?b.toUpperCase():(a||this).toLowerCase().replace(/[^a-z]+(.?)/g,f)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "toCamelCase", | |
"description": "Make texts lowerCamelCased", | |
"keywords": [ | |
"CamelCase", | |
"toCamelCase" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Another cool trick. By the way, I still don't understand why the this
is in the code?
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
@maettig I've reflected the change, thanks!
By the way, we can use a variable
c
and the trick++c
rather than''+b===b
.