Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created May 8, 2015 21:41
Show Gist options
  • Save indexzero/95e972764f534046d234 to your computer and use it in GitHub Desktop.
Save indexzero/95e972764f534046d234 to your computer and use it in GitHub Desktop.
PascalCase DEAcronym-er
function deacronym(anyKey) {
return anyKey.replace(/([A-Z]+)[A-Z]/g, function (match, cap, i, str) {
var sub;
if (i + match.length === str.length) {
sub = match.slice(1);
} else {
sub = !i ? cap.slice(1) : match.slice(1, -1);
}
return match.replace(sub, sub.toLowerCase());
});
}
[
'InPascal', // InPascal
'PREFix', // PreFix
'FixPOST', // FixPost
'AndINFix', // AndInFix
'PREAndINAndPostFIX' // PreAndInAndPostFix
].forEach(function (test) {
precamel = deacronym(test);
console.log(test, precamel, require('camel-case')(precamel));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment