fix(strings): keep last char when truncate separator is absent

str.rfind returns -1 when a string separator is not present in the
truncated text. Using that -1 as a slice end dropped an extra trailing
character before appending the omission. Only adjust the truncation point
when the separator is actually found, matching the regex-separator branch
and lodash behaviour.
This commit is contained in:
dualfroz committed 2026-09-04 09:04:55 +02:00
1 parent 0e75093e29
commit 7fc28b38e1
3 files changed
+90 -1

No files matched your search

+3 -1
View File
@@ -2064,7 +2064,9 @@ def truncate(
trunc_len = len(text)
if pyd.is_string(separator):
trunc_len = text.rfind(separator)
separator_index = text.rfind(separator)
if separator_index >= 0:
trunc_len = separator_index
elif pyd.is_reg_exp(separator):
last = None
for match in separator.finditer(text):