summaryrefslogtreecommitdiff
path: root/lib/string.fnl
blob: 3e6134ebc02db01dc7efc385f4667b2a42d40cc3 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(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.utf8.len str))]
    (table.insert result (must (luna.utf8.sub str i 1))))
  result)

(fn split [str delimiter]
  (if (empty? str)
    []
    (do
      (local result {})
      (local len (# str))
      (var cursor 1)
      (var (start end) (str:find delimiter))
      (while start
        (when (< cursor start)
          (table.insert result (str:sub cursor (- start 1))))
        (set cursor (+ end 1))
        (set (start end) (str:find delimiter cursor)))
      (when (<= cursor len)
        (table.insert result (str:sub cursor len)))
      result)))

(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.utf8.len str)) len)
      str
      (.. (trim (must (luna.utf8.sub str 1 len))) (or ellipsis "...")))
    ""))

(fn insert [str substr pos]
  (.. (str:sub 1 pos) substr (str:sub (+ 1 pos))))

(fn format-price [price]
  (var price-str (tostring price))
  (local dot-position (price-str:find "%."))
  (local price-len (if dot-position
                     (- (pick-values 1 dot-position) 1)
                     (# price-str)))
  (var cursor (- price-len 3))
  (while (< 0 cursor)
    (set price-str (insert price-str " " cursor))
    (set cursor (- cursor 3)))
  price-str)

(fn test-format-price []
  (assert (= (format-price 5123450.50689140) "5 123 450.5068914"))
  (assert (= (format-price 123450.50689140) "123 450.5068914"))
  (assert (= (format-price 1450.50689140) "1 450.5068914"))
  (assert (= (format-price 14350) "14 350"))
  (assert (= (format-price 100) "100"))
  (assert (= (format-price 0) "0"))
  (assert (= (format-price 0.5) "0.5")))

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