Skip to content

Instantly share code, notes, and snippets.

@olegchursin
Created October 11, 2022 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegchursin/f7a8314e2351b792ba4aacb2427b763b to your computer and use it in GitHub Desktop.
Save olegchursin/f7a8314e2351b792ba4aacb2427b763b to your computer and use it in GitHub Desktop.
Convert a random string to camelized stringId
function removeSpecialChars(str: string): string {
return str.replace(/[^0-9a-zA-Z]/g, '');
}
function camelize(str: string): string {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
})
.replace(/\s+/g, '');
}
function stringToId(str: string): string {
const alphanumericString = removeSpecialChars(str);
return camelize(alphanumericString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment