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