Skip to content

Commit

Permalink
fix: remove_last was eating an extra character
Browse files Browse the repository at this point in the history
  • Loading branch information
ebobby committed Aug 11, 2023
1 parent 3bd8d2e commit 5351051
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/filters/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function remove_last (v: string, l: string) {
const pattern = String(l)
const index = str.lastIndexOf(pattern)
if (index === -1) return str
return str.substring(0, index) + str.substring(index + pattern.length + 1)
return str.substring(0, index) + str.substring(index + pattern.length)
}

export function rstrip (str: string, chars?: string) {
Expand Down
4 changes: 4 additions & 0 deletions test/integration/filters/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ describe('filters/string', function () {
return test('{{ "I strained to see the train through the rain" | remove_last: "rain" }}',
'I strained to see the train through the ')
})
it('should remove the last occurrence of substring in the middle of a string', function () {
return test('{{ "I strained to see the train through the rain and fog" | remove_last: "rain" }}',
'I strained to see the train through the and fog')
})
it('should handle substring not found', function () {
return test('{{ "I strained to see the train through the rain" | remove_last: "no such thing" }}',
'I strained to see the train through the rain')
Expand Down

0 comments on commit 5351051

Please sign in to comment.