summaryrefslogtreecommitdiff
path: root/lib/string.fnl
blob: 9ac6eddd7b447cb75f67019c5d171156d55e87c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(fn split [string]
  (accumulate [res [] v _
               (string:gmatch "%S+")]
    (do
      (table.insert res v)
      res)))

(fn ends-with [string end]
  (= (string.sub string (- (# end))) end))

(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 : trim : truncate}