summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-10-07 20:20:10 +0600
committerunwox <me@unwox.com>2024-10-08 14:06:42 +0600
commit152b046be82ab70bc77ef9fc0e1bb3e9c027bc86 (patch)
treeebed519f158f16e479de3e80ac3bc3eaaed1c730 /lib
parent4cf902d6f0f41befeb2e028cb394cb454c2ba73c (diff)
add string.truncate function
Diffstat (limited to 'lib')
-rw-r--r--lib/string.fnl13
1 files changed, 12 insertions, 1 deletions
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}