Skip to content

Instantly share code, notes, and snippets.

@tkissing
Forked from 140bytes/LICENSE.txt
Created November 8, 2011 05:40
Show Gist options
  • Save tkissing/1347102 to your computer and use it in GitHub Desktop.
Save tkissing/1347102 to your computer and use it in GitHub Desktop.
getOffTheCamel - 140byt.es splitCamelCaseString in JavaScript

getOffTheCamel

Split up a camel-cased word at the humps, insert separator and lowercase the character after it.

function(a,b) { // a: string in camelCase format [b: separator; defaults to SPACE]
// coerce argument to string
return (a+'').replace(
// search for uppercase (ascii) characters following anything that is not an uppercase (ascii) character
/([^A-Z])([A-Z]+)?([A-Z])/g,
function(c,d,e,f) {
// leave the not-uppercase-character as is, append separator, append the hump (in lowercase unless its multiple uppercase chars in a row)
return [d,b||' ',e?e+f:f.toLowerCase()].join('');
}
);
};
function(a,b){return(a+'').replace(/([^A-Z])([A-Z]+)?([A-Z])/g,function(c,d,e,f){return[d,b||' ',e?e+f:f.toLowerCase()].join('')})}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Timo Kissing http://kissing.name
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": "getOffTheCamel",
"description": "JavaScript function to split a camelCase word at the humps",
"keywords": [
"140bytes",
"string",
"camelCase"
]
}
<!DOCTYPE html>
<head>
<title>getOffTheCamel -&gt; get off the camel</title>
</head>
<body>
<div>Expected value: <b>"Click fork to play!","byte-saving", "get USBController adapter"</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var getOffTheCamel = function(a,b){return(a+'').replace(/([^A-Z])([A-Z]+)?([A-Z])/g,function(c,d,e,f){return[d,b||' ',e?e+f:f.toLowerCase()].join('')})};
document.getElementById( "ret" ).innerHTML = [getOffTheCamel("ClickForkToPlay!"), getOffTheCamel("byteSaving", '-'), getOffTheCamel("getUSBControllerAdapter")];
</script>
</body>
</html>
@tsaniel
Copy link

tsaniel commented Nov 8, 2011

What about

function(a,b){return a.replace(/(?=(?!^)[A-Z])/g,b||' ')}

?

@tkissing
Copy link
Author

tkissing commented Nov 9, 2011

tsaniel: Very nice regex-magic there. I like the lowercasing, so will stick with my version. But using a lookahead might help handling multiple uppercase characters in a row. Currently passing in "getUSBController" gives "get uSBController" using my version and "get U S B Controller" using your version - would love to have it return "get USB Controller" or at least "get USBController" (iow, don't lowercase if the hump consists of multiple uppercase characters

@tkissing
Copy link
Author

tkissing commented Nov 9, 2011

Ok, no need for lookaheads. "getUSBController" now turns into "get USBController". If you'd rather have "get USB controller", try this:
function(a,b){b=b||' ';return(a+'').replace(/([^A-Z])([A-Z]+)?([A-Z])/g,function(c,d,e,f){return[d,b,e,e?b:e,f.toLowerCase()].join('')})}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment