Skip to content

Commit bb94236

Browse files
committedAug 17, 2021
fix!: handle lower edges (resolves #4)
1 parent f7c6d26 commit bb94236

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Converts first character to lower case
6767
### `splitByCase(str, splitters?)`
6868

6969
- Splits string by the splitters provided (default: `['-', '_', '/', '.]`)
70-
- Splits when case changes from lower to upper (only rising edges)
70+
- Splits when case changes from lower to upper or upper to lower
7171
- Case is preserved in returned value
7272
- Is an irreversible function since splitters are omitted
7373

‎src/index.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,51 @@ const STR_SPLITTERS = ['-', '_', '/', '.']
77
export function splitByCase (str: string, splitters = STR_SPLITTERS): string[] {
88
const parts: string[] = []
99

10-
let buff = ''
10+
if (!str || typeof str !== 'string') {
11+
return parts
12+
}
13+
14+
let buff: string = ''
1115

12-
let previusUpper = isUppercase(str[0])
13-
let previousSplitter = splitters.includes(str[0])
16+
let previusUpper = null
17+
let previousSplitter = null
1418

1519
for (const char of str.split('')) {
20+
// Splitter
1621
const isSplitter = splitters.includes(char)
17-
if (isSplitter) {
22+
if (isSplitter === true) {
1823
parts.push(buff)
1924
buff = ''
20-
previusUpper = false
21-
previousSplitter = true
25+
previusUpper = null
2226
continue
2327
}
2428

2529
const isUpper = isUppercase(char)
26-
if (!previousSplitter && (!previusUpper && isUpper /* rising edge */)) {
27-
parts.push(buff)
28-
buff = char
29-
previusUpper = isUpper
30-
previousSplitter = false
31-
continue
30+
if (previousSplitter === false) {
31+
// Case rising edge
32+
if (previusUpper === false && isUpper === true) {
33+
parts.push(buff)
34+
buff = char
35+
previusUpper = isUpper
36+
continue
37+
}
38+
// Case falling edge
39+
if (previusUpper === true && isUpper === false && buff.length > 1) {
40+
const lastChar = buff[buff.length - 1]
41+
parts.push(buff.substr(0, buff.length - 1))
42+
buff = lastChar + char
43+
previusUpper = isUpper
44+
continue
45+
}
3246
}
3347

48+
// Normal char
3449
buff += char
3550
previusUpper = isUpper
3651
previousSplitter = isSplitter
3752
}
3853

39-
if (buff) {
40-
parts.push(buff)
41-
}
54+
parts.push(buff)
4255

4356
return parts
4457
}

‎test/scule.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ describe('splitByCase', () => {
99
FooBarBaz: ['Foo', 'Bar', 'Baz'],
1010
'foo_bar-baz/qux': ['foo', 'bar', 'baz', 'qux'],
1111
'foo--bar-Baz': ['foo', '', 'bar', 'Baz'],
12-
FOOBar: ['FOOBar']
12+
FOOBar: ['FOO', 'Bar'],
13+
ALink: ['A', 'Link']
1314
}
1415

1516
for (const input in tests) {
@@ -53,7 +54,8 @@ describe('kebabCase', () => {
5354
'foo/Bar': 'foo-bar',
5455
'foo-bAr': 'foo-b-ar',
5556
'foo--bar': 'foo--bar',
56-
FooBAR: 'foo-bar'
57+
FooBAR: 'foo-bar',
58+
ALink: 'a-link'
5759
}
5860

5961
for (const input in tests) {

0 commit comments

Comments
 (0)
Please sign in to comment.