feat: add replace_last filter

This commit is contained in:
James Prior
2022-12-02 23:48:50 +08:00
committed by Jun Yang
parent 6f0a68080c
commit b4d1e27420
2 changed files with 19 additions and 0 deletions
+9
View File
@@ -82,6 +82,15 @@ export function replace_first (v: string, arg1: string, arg2: string) {
return stringify(v).replace(String(arg1), arg2)
}
export function replace_last (v: string, arg1: string, arg2: string) {
const str = stringify(v)
const pattern = String(arg1)
const index = str.lastIndexOf(pattern)
if (index === -1) return str
const replacement = String(arg2)
return str.substring(0, index) + replacement + str.substring(index + pattern.length)
}
export function truncate (v: string, l = 50, o = '...') {
v = stringify(v)
if (v.length <= l) return v