Skip to content

Instantly share code, notes, and snippets.

@Nutrox
Created January 26, 2011 16:49
Show Gist options
  • Save Nutrox/796986 to your computer and use it in GitHub Desktop.
Save Nutrox/796986 to your computer and use it in GitHub Desktop.
JavaScript - toConstantCase()
function toConstantCase() {
var a = arguments;
if( a.length == 1 ) {
return a[0].replace( toConstantCase.REGEX, toConstantCase ).toUpperCase();
}
return a[0][0] + "_" + a[0][1] + ( a[0][2] ? a[0][2] : "" );
}
toConstantCase.REGEX = /[a-z][A-Z]|[A-Z]{2}[a-z]/g;
//
// EXAMPLE
//
console.log( toConstantCase( "someValue" ) ); // SOME_VALUE
console.log( toConstantCase( "someSuperValue" ) ); // SOME_SUPER_VALUE
console.log( toConstantCase( "someSuperDOOPERValue" ) ); // SOME_SUPER_DOOPER_VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment