Skip to content

Commit

Permalink
fix: _substringAfter worked wrong for longer delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Apr 6, 2024
1 parent 5d2f532 commit fd54abc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/string/string.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ test('substringBefore, substringAfter', () => {
const s2 = '/Users/lalala/someFile.test.ts'

expect(_substringBefore(s1, '.')).toBe('someFile')
expect(_substringBefore(s1, '.ts')).toBe('someFile.test')
expect(_substringAfter(s1, '.')).toBe('test.ts')
expect(_substringAfter(s1, 'test.')).toBe('ts')
expect(_substringBeforeLast(s2, '/')).toBe(`/Users/lalala`)
expect(_substringAfterLast(s2, '/')).toBe(`someFile.test.ts`)
expect(_substringAfterLast(s2, 'la')).toBe(`/someFile.test.ts`)
})

test('_substringBetweenLast', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/string/string.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export function _substringBeforeLast(s: string, delimiter: string): string {

export function _substringAfter(s: string, delimiter: string): string {
const pos = s.indexOf(delimiter)
return pos !== -1 ? s.slice(pos + 1) : s
return pos !== -1 ? s.slice(pos + delimiter.length) : s
}

export function _substringAfterLast(s: string, delimiter: string): string {
const pos = s.lastIndexOf(delimiter)
return pos !== -1 ? s.slice(pos + 1) : s
return pos !== -1 ? s.slice(pos + delimiter.length) : s
}

/**
Expand Down

0 comments on commit fd54abc

Please sign in to comment.