summaryrefslogtreecommitdiff
path: root/lib/string.fnl
blob: ca29da091670548335d14783a7ef43623504718c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(local {: must} (require :lib.utils))

(fn empty? [str]
  (or (= str nil) (= (# str) 0)))

(fn letters [str]
  (assert
    (= "string" (type str))
    (string.format "letters(): str must be string, %s given" (type str)))
  (var result [])
  (for [i 1 (must (luna.utf.len str))]
    (table.insert result (must (luna.utf.sub str i 1))))
  result)

(fn split [str]
  (accumulate [res [] v _ (str:gmatch "%S+")]
    (do
      (table.insert res v)
      res)))

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

(fn trim [str]
  (str:match "^%s*(.-)%s*$"))

(fn truncate [str len ellipsis]
  (if (and (= (type str) "string")
           (< 0 (# str)))
    (if (< (must (luna.utf.len str)) len)
      str
      (.. (trim (must (luna.utf.sub str 1 len))) (or ellipsis "...")))
    ""))

{: letters : empty? : split : ends-with : trim : truncate}