Last active
September 16, 2021 13:21
-
-
Save sametcelikbicak/d9ee7df235c7ceb0cdf13a3a3c881667 to your computer and use it in GitHub Desktop.
String extension for toCamelCase and toPascalCase
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
declare global { | |
interface String { | |
toCamelCase(): string; | |
toPascalCase(): string; | |
} | |
} | |
String.prototype.toCamelCase = function (): string { | |
return this.replace(/(?:^\w|[A-Z]|-|\b\w)/g, (character, index) => | |
index === 0 | |
? character.toLocaleLowerCase() | |
: character.toLocaleUpperCase() | |
); | |
}; | |
String.prototype.toPascalCase = function (): string { | |
return this.replace(/(?:^\w|[A-Z]|-|\b\w)/g, (firstCharacter) => | |
firstCharacter.toLocaleUpperCase() | |
); | |
}; | |
export {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment