File tree Expand file tree Collapse file tree 1 file changed +16
-9
lines changed Expand file tree Collapse file tree 1 file changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -2,16 +2,23 @@ export function capitalise(text: string) {
2
2
return text [ 0 ] . toUpperCase ( ) + text . slice ( 1 )
3
3
}
4
4
5
+ const UC_RE = / [ A - Z ] /
5
6
export function splitStringByCase ( text : string ) {
6
- return capitalise (
7
- text
8
- . replace ( / [ A - Z ] + [ a - z ] / g, h =>
9
- h . length > 2
10
- ? h . slice ( 0 , - 2 ) + ' ' + capitalise ( h . slice ( - 2 ) . toLowerCase ( ) )
11
- : Array ( h ) . join ( ' ' )
12
- )
13
- . replace ( / [ a - z ] [ A - Z ] + / g, h => `${ h [ 0 ] } ${ h . slice ( 1 ) } ` )
14
- )
7
+ let output = ''
8
+ let index = 0
9
+ for ( const char of text ) {
10
+ const lastChar = index ? text [ index - 1 ] : ''
11
+ const nextChar = text [ index + 1 ]
12
+ if (
13
+ UC_RE . test ( char ) &&
14
+ ( ! UC_RE . test ( lastChar ) || ( nextChar && ! UC_RE . test ( nextChar ) ) )
15
+ ) {
16
+ output += ' '
17
+ }
18
+ output += char
19
+ index ++
20
+ }
21
+ return capitalise ( output )
15
22
}
16
23
17
24
export function inArray < A > ( items : A | A [ ] ) {
You can’t perform that action at this time.
0 commit comments