1
1
'use strict' ;
2
2
3
+ const UPPERCASE = / [ \p{ Lu} ] / u;
4
+ const LOWERCASE = / [ \p{ Ll} ] / u;
5
+ const LEADING_CAPITAL = / ^ [ \p{ Lu} ] (? ! [ \p{ Lu} ] ) / gu;
6
+ const IDENTIFIER = / ( [ \p{ Alpha} \p{ N} _ ] | $ ) / u;
7
+ const SEPARATORS = / [ _ . \- ] + / ;
8
+
9
+ const LEADING_SEPARATORS = new RegExp ( '^' + SEPARATORS . source ) ;
10
+ const SEPARATORS_AND_IDENTIFIER = new RegExp ( SEPARATORS . source + IDENTIFIER . source , 'gu' ) ;
11
+ const NUMBERS_AND_IDENTIFIER = new RegExp ( '\\d+' + IDENTIFIER . source , 'gu' ) ;
12
+
3
13
const preserveCamelCase = ( string , locale ) => {
4
14
let isLastCharLower = false ;
5
15
let isLastCharUpper = false ;
@@ -8,13 +18,13 @@ const preserveCamelCase = (string, locale) => {
8
18
for ( let i = 0 ; i < string . length ; i ++ ) {
9
19
const character = string [ i ] ;
10
20
11
- if ( isLastCharLower && / [ \p { Lu } ] / u . test ( character ) ) {
21
+ if ( isLastCharLower && UPPERCASE . test ( character ) ) {
12
22
string = string . slice ( 0 , i ) + '-' + string . slice ( i ) ;
13
23
isLastCharLower = false ;
14
24
isLastLastCharUpper = isLastCharUpper ;
15
25
isLastCharUpper = true ;
16
26
i ++ ;
17
- } else if ( isLastCharUpper && isLastLastCharUpper && / [ \p { Ll } ] / u . test ( character ) ) {
27
+ } else if ( isLastCharUpper && isLastLastCharUpper && LOWERCASE . test ( character ) ) {
18
28
string = string . slice ( 0 , i - 1 ) + '-' + string . slice ( i - 1 ) ;
19
29
isLastLastCharUpper = isLastCharUpper ;
20
30
isLastCharUpper = false ;
@@ -30,12 +40,17 @@ const preserveCamelCase = (string, locale) => {
30
40
} ;
31
41
32
42
const preserveConsecutiveUppercase = input => {
33
- return input . replace ( / ^ [ \p{ Lu} ] (? ! [ \p{ Lu} ] ) / gu, m1 => m1 . toLowerCase ( ) ) ;
43
+ LEADING_CAPITAL . lastIndex = 0 ;
44
+
45
+ return input . replace ( LEADING_CAPITAL , m1 => m1 . toLowerCase ( ) ) ;
34
46
} ;
35
47
36
48
const postProcess = ( input , options ) => {
37
- return input . replace ( / [ _ . \- ] + ( [ \p{ Alpha} \p{ N} _ ] | $ ) / gu, ( _ , p1 ) => p1 . toLocaleUpperCase ( options . locale ) )
38
- . replace ( / \d + ( [ \p{ Alpha} \p{ N} _ ] | $ ) / gu, m => m . toLocaleUpperCase ( options . locale ) ) ;
49
+ SEPARATORS_AND_IDENTIFIER . lastIndex = 0 ;
50
+ NUMBERS_AND_IDENTIFIER . lastIndex = 0 ;
51
+
52
+ return input . replace ( SEPARATORS_AND_IDENTIFIER , ( _ , identifier ) => identifier . toLocaleUpperCase ( options . locale ) )
53
+ . replace ( NUMBERS_AND_IDENTIFIER , m => m . toLocaleUpperCase ( options . locale ) ) ;
39
54
} ;
40
55
41
56
const camelCase = ( input , options ) => {
@@ -71,7 +86,7 @@ const camelCase = (input, options) => {
71
86
input = preserveCamelCase ( input , options . locale ) ;
72
87
}
73
88
74
- input = input . replace ( / ^ [ _ . \- ] + / , '' ) ;
89
+ input = input . replace ( LEADING_SEPARATORS , '' ) ;
75
90
76
91
if ( options . preserveConsecutiveUppercase ) {
77
92
input = preserveConsecutiveUppercase ( input ) ;
0 commit comments