1+ // Pre-compiled regex patterns for better performance
2+ const DOT_ACRONYM = / ( [ A - Z ] + ) ( [ A - Z ] [ a - z ] ) / g;
3+ const DOT_LOWERCASE_UPPER = / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g;
4+ const DOT_LETTER_NUMBER = / ( [ a - z A - Z ] ) ( [ 0 - 9 ] ) / g;
5+ const DOT_NUMBER_LETTER = / ( [ 0 - 9 ] ) ( [ a - z A - Z ] ) / g;
6+ const DOT_NON_ALNUM = / [ ^ a - z 0 - 9 ] + / gi;
7+ const DOT_TRIM = / ^ \. + | \. + $ / g;
8+ const DOT_MULTIPLE = / \. + / g;
9+
110/**
211 * Converts a string to dot.case
312 * @param str - The input string to convert
@@ -16,19 +25,19 @@ export const dotCase = (str: string): string => {
1625 str
1726 . trim ( )
1827 // Handle consecutive uppercase letters (acronyms)
19- . replace ( / ( [ A - Z ] + ) ( [ A - Z ] [ a - z ] ) / g , "$1.$2" )
28+ . replace ( DOT_ACRONYM , "$1.$2" )
2029 // Add dot between lowercase/number and uppercase
21- . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g , "$1.$2" )
30+ . replace ( DOT_LOWERCASE_UPPER , "$1.$2" )
2231 // Add dot between letter and number
23- . replace ( / ( [ a - z A - Z ] ) ( [ 0 - 9 ] ) / g , "$1.$2" )
32+ . replace ( DOT_LETTER_NUMBER , "$1.$2" )
2433 // Add dot between number and letter
25- . replace ( / ( [ 0 - 9 ] ) ( [ a - z A - Z ] ) / g , "$1.$2" )
34+ . replace ( DOT_NUMBER_LETTER , "$1.$2" )
2635 // Replace non-alphanumeric with dot
27- . replace ( / [ ^ a - z 0 - 9 ] + / gi , "." )
36+ . replace ( DOT_NON_ALNUM , "." )
2837 // Remove leading/trailing dots
29- . replace ( / ^ \. + | \. + $ / g , "" )
38+ . replace ( DOT_TRIM , "" )
3039 // Replace multiple dots with single
31- . replace ( / \. + / g , "." )
40+ . replace ( DOT_MULTIPLE , "." )
3241 . toLowerCase ( )
3342 ) ;
3443} ;
0 commit comments