annotate src/lib/Utils.luan @ 40:7ea33179592a

email notification
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 27 Feb 2025 16:44:20 -0700
parents 7230c821c368
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
3 local type = Luan.type or error()
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
4 local ipairs = Luan.ipairs or error()
17
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
5 local pairs = Luan.pairs or error()
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
6 local set_metatable = Luan.set_metatable or error()
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Http = require "luan:http/Http.luan"
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Utils = {}
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 function Utils.base_url()
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local request = Http.request
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 return request.scheme.."://"..request.headers["Host"]
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 end
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
4
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
17 function Utils.to_list(input)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
18 if input == nil then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
19 return {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
20 elseif type(input) == "table" then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
21 return input
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
22 else
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
23 return {input}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
24 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
25 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
26
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
27 local set_mt = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
28 function set_mt.__index(table,key)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
29 return false
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
30 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
31
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
32 local function list_to_set(list)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
33 local set = {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
34 for _, v in ipairs(list) do
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
35 set[v] = true
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
36 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
37 set_metatable(set,set_mt)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
38 return set
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
39 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
40 Utils.list_to_set = list_to_set
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
41
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
42 function Utils.to_set(obj)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
43 if obj == nil then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
44 return {}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
45 elseif type(obj) == "table" then
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
46 return list_to_set(obj)
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
47 else
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
48 return {[obj]=true}
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
49 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
50 end
2da10ece826f add Chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
51
17
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
52 function Utils.set_to_list(set)
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
53 local list = {}
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
54 for k, v in pairs(set) do
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
55 if v then
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
56 list[#list+1] = k
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
57 end
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
58 end
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
59 return list
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
60 end
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
61
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
62 function Utils.is_in_list(el,list)
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
63 for _, v in ipairs(list) do
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
64 if v == el then
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
65 return true
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
66 end
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
67 end
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
68 return false
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
69 end
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
70
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
71 function Utils.shallow_copy(t)
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
72 local rtn = {}
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
73 for key, val in pairs(t) do
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
74 rtn[key] = val
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
75 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
76 return rtn
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
77 end
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
78
3
2c63b10781e1 add login
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 return Utils