comparison src/lib/Utils.luan @ 4:2da10ece826f

add Chat
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 27 Oct 2024 20:39:18 -0600
parents 2c63b10781e1
children 7230c821c368
comparison
equal deleted inserted replaced
3:2c63b10781e1 4:2da10ece826f
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local type = Luan.type or error()
4 local ipairs = Luan.ipairs or error()
5 local set_metatable = Luan.set_metatable or error()
3 local Http = require "luan:http/Http.luan" 6 local Http = require "luan:http/Http.luan"
4 7
5 8
6 local Utils = {} 9 local Utils = {}
7 10
8 function Utils.base_url() 11 function Utils.base_url()
9 local request = Http.request 12 local request = Http.request
10 return request.scheme.."://"..request.headers["Host"] 13 return request.scheme.."://"..request.headers["Host"]
11 end 14 end
12 15
16 function Utils.to_list(input)
17 if input == nil then
18 return {}
19 elseif type(input) == "table" then
20 return input
21 else
22 return {input}
23 end
24 end
25
26 local set_mt = {}
27 function set_mt.__index(table,key)
28 return false
29 end
30
31 local function list_to_set(list)
32 local set = {}
33 for _, v in ipairs(list) do
34 set[v] = true
35 end
36 set_metatable(set,set_mt)
37 return set
38 end
39 Utils.list_to_set = list_to_set
40
41 function Utils.to_set(obj)
42 if obj == nil then
43 return {}
44 elseif type(obj) == "table" then
45 return list_to_set(obj)
46 else
47 return {[obj]=true}
48 end
49 end
50
13 return Utils 51 return Utils