From 152b046be82ab70bc77ef9fc0e1bb3e9c027bc86 Mon Sep 17 00:00:00 2001 From: unwox Date: Mon, 7 Oct 2024 20:20:10 +0600 Subject: add string.truncate function --- lib/string.fnl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/string.fnl b/lib/string.fnl index 466dac9..d82c658 100644 --- a/lib/string.fnl +++ b/lib/string.fnl @@ -8,4 +8,15 @@ (fn ends-with [string end] (= (string.sub string (- (# end))) end)) -{: split : ends-with} +(fn trim [str] + (str:match "^%s*(.-)%s*$")) + +(fn truncate [str len ellipsis] + (if (and (= (type str) "string") + (< 0 (# str))) + (if (< (# str) len) + str + (.. (trim (luna.utf.sub str 1 len)) (or ellipsis "..."))) + "")) + +{: split : ends-with : truncate} -- cgit v1.2.3