3
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
4
|
3 local type = Luan.type or error()
|
|
4 local ipairs = Luan.ipairs or error()
|
17
|
5 local pairs = Luan.pairs or error()
|
4
|
6 local set_metatable = Luan.set_metatable or error()
|
3
|
7 local Http = require "luan:http/Http.luan"
|
|
8
|
|
9
|
|
10 local Utils = {}
|
|
11
|
|
12 function Utils.base_url()
|
|
13 local request = Http.request
|
|
14 return request.scheme.."://"..request.headers["Host"]
|
|
15 end
|
|
16
|
4
|
17 function Utils.to_list(input)
|
|
18 if input == nil then
|
|
19 return {}
|
|
20 elseif type(input) == "table" then
|
|
21 return input
|
|
22 else
|
|
23 return {input}
|
|
24 end
|
|
25 end
|
|
26
|
|
27 local set_mt = {}
|
|
28 function set_mt.__index(table,key)
|
|
29 return false
|
|
30 end
|
|
31
|
|
32 local function list_to_set(list)
|
|
33 local set = {}
|
|
34 for _, v in ipairs(list) do
|
|
35 set[v] = true
|
|
36 end
|
|
37 set_metatable(set,set_mt)
|
|
38 return set
|
|
39 end
|
|
40 Utils.list_to_set = list_to_set
|
|
41
|
|
42 function Utils.to_set(obj)
|
|
43 if obj == nil then
|
|
44 return {}
|
|
45 elseif type(obj) == "table" then
|
|
46 return list_to_set(obj)
|
|
47 else
|
|
48 return {[obj]=true}
|
|
49 end
|
|
50 end
|
|
51
|
17
|
52 function Utils.set_to_list(set)
|
|
53 local list = {}
|
|
54 for k, v in pairs(set) do
|
|
55 if v then
|
|
56 list[#list+1] = k
|
|
57 end
|
|
58 end
|
|
59 return list
|
|
60 end
|
|
61
|
|
62 function Utils.is_in_list(el,list)
|
|
63 for _, v in ipairs(list) do
|
|
64 if v == el then
|
|
65 return true
|
|
66 end
|
|
67 end
|
|
68 return false
|
|
69 end
|
|
70
|
40
|
71 function Utils.shallow_copy(t)
|
|
72 local rtn = {}
|
|
73 for key, val in pairs(t) do
|
|
74 rtn[key] = val
|
|
75 end
|
|
76 return rtn
|
|
77 end
|
|
78
|
3
|
79 return Utils
|