diff options
| author | unwox <me@unwox.com> | 2024-10-07 20:20:10 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2024-10-08 14:06:42 +0600 |
| commit | 152b046be82ab70bc77ef9fc0e1bb3e9c027bc86 (patch) | |
| tree | ebed519f158f16e479de3e80ac3bc3eaaed1c730 | |
| parent | 4cf902d6f0f41befeb2e028cb394cb454c2ba73c (diff) | |
add string.truncate function
| -rw-r--r-- | lib/string.fnl | 13 |
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} |
