fix: remove_last was eating an extra character

This commit is contained in:
Francisco Soto
2023-08-17 21:43:21 +08:00
committed by Jun Yang
parent 3bd8d2e557
commit fc2731376f
2 changed files with 5 additions and 1 deletions
+1 -1
View File
@@ -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) {
+4
View File
@@ -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')