Skip to content

Commit

Permalink
fix!: handle lower edges (resolves #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 17, 2021
1 parent f7c6d26 commit bb94236
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Converts first character to lower case
### `splitByCase(str, splitters?)`

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

Expand Down
43 changes: 28 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,51 @@ const STR_SPLITTERS = ['-', '_', '/', '.']
export function splitByCase (str: string, splitters = STR_SPLITTERS): string[] {
const parts: string[] = []

let buff = ''
if (!str || typeof str !== 'string') {
return parts
}

let buff: string = ''

let previusUpper = isUppercase(str[0])
let previousSplitter = splitters.includes(str[0])
let previusUpper = null
let previousSplitter = null

for (const char of str.split('')) {
// Splitter
const isSplitter = splitters.includes(char)
if (isSplitter) {
if (isSplitter === true) {
parts.push(buff)
buff = ''
previusUpper = false
previousSplitter = true
previusUpper = null
continue
}

const isUpper = isUppercase(char)
if (!previousSplitter && (!previusUpper && isUpper /* rising edge */)) {
parts.push(buff)
buff = char
previusUpper = isUpper
previousSplitter = false
continue
if (previousSplitter === false) {
// Case rising edge
if (previusUpper === false && isUpper === true) {
parts.push(buff)
buff = char
previusUpper = isUpper
continue
}
// Case falling edge
if (previusUpper === true && isUpper === false && buff.length > 1) {
const lastChar = buff[buff.length - 1]
parts.push(buff.substr(0, buff.length - 1))
buff = lastChar + char
previusUpper = isUpper
continue
}
}

// Normal char
buff += char
previusUpper = isUpper
previousSplitter = isSplitter
}

if (buff) {
parts.push(buff)
}
parts.push(buff)

return parts
}
Expand Down
6 changes: 4 additions & 2 deletions test/scule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ describe('splitByCase', () => {
FooBarBaz: ['Foo', 'Bar', 'Baz'],
'foo_bar-baz/qux': ['foo', 'bar', 'baz', 'qux'],
'foo--bar-Baz': ['foo', '', 'bar', 'Baz'],
FOOBar: ['FOOBar']
FOOBar: ['FOO', 'Bar'],
ALink: ['A', 'Link']
}

for (const input in tests) {
Expand Down Expand Up @@ -53,7 +54,8 @@ describe('kebabCase', () => {
'foo/Bar': 'foo-bar',
'foo-bAr': 'foo-b-ar',
'foo--bar': 'foo--bar',
FooBAR: 'foo-bar'
FooBAR: 'foo-bar',
ALink: 'a-link'
}

for (const input in tests) {
Expand Down

0 comments on commit bb94236

Please sign in to comment.