Skip to content

Instantly share code, notes, and snippets.

@Vheissu
Last active September 3, 2015 10:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vheissu/630f6b3c7bab3624517f to your computer and use it in GitHub Desktop.
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.
// 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