Skip to content

Instantly share code, notes, and snippets.

@sametcelikbicak
Last active September 16, 2021 13:21
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 sametcelikbicak/d9ee7df235c7ceb0cdf13a3a3c881667 to your computer and use it in GitHub Desktop.
Save sametcelikbicak/d9ee7df235c7ceb0cdf13a3a3c881667 to your computer and use it in GitHub Desktop.
String extension for toCamelCase and toPascalCase
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