Created
October 11, 2022 03:43
-
-
Save olegchursin/f7a8314e2351b792ba4aacb2427b763b to your computer and use it in GitHub Desktop.
Convert a random string to camelized stringId
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
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