Skip to content

Instantly share code, notes, and snippets.

@pseudosavant
Created March 3, 2015 19:44
Show Gist options
  • Save pseudosavant/731715198f444a3931d9 to your computer and use it in GitHub Desktop.
Save pseudosavant/731715198f444a3931d9 to your computer and use it in GitHub Desktop.
Camel case strings with dashes in them. E.g. 'camel-case' -> 'camelCase'
// Camel case strings with dashes in them. E.g. 'camel-case' -> 'camelCase'
function camelCase(s) {
function upperCaseFirst(s) {
return s[0].toUpperCase() + s.slice(1);
}
var separator = '-';
// Return early if no additional camel casing is needed
if (s.indexOf(separator) === -1) {
return s;
}
var parts = s.split(separator);
return parts.map(function(v, i) {
if (i > 0) {
return upperCaseFirst(v);
} else {
return v;
}
}).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment