Last active
September 3, 2015 10:34
-
-
Save Vheissu/630f6b3c7bab3624517f to your computer and use it in GitHub Desktop.
Converting a Javascript constructor name in PascalCase to file-case. Can be used with ES2015 (formerly ES6) classes to get the actual file name.
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
// ES2015 classes | |
class MyClass { | |
// Would return: my-class | |
toFileCase() { | |
var className = this.constructor.name.toString(); | |
var normalised = className.replace(/((?!^)[A-Z])/g, '-$1').toLowerCase(); | |
return normalised; | |
} | |
} | |
// ES5 prototypal inheritance | |
function MyClass() { | |
} | |
// Would return: my-class | |
MyClass.prototype.toFileCase = function() { | |
var className = this.constructor.name.toString(); | |
var normalised = className.replace(/((?!^)[A-Z])/g, '-$1').toLowerCase(); | |
return normalised; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment