view src/lib/Utils.luan @ 93:d0566cc4a2ac default tip

uploa fix
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Apr 2025 19:47:48 -0600
parents 7ea33179592a
children
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local type = Luan.type or error()
local ipairs = Luan.ipairs or error()
local pairs = Luan.pairs or error()
local set_metatable = Luan.set_metatable or error()
local Http = require "luan:http/Http.luan"


local Utils = {}

function Utils.base_url()
	local request = Http.request
	return request.scheme.."://"..request.headers["Host"]
end

function Utils.to_list(input)
	if input == nil then
		return {}
	elseif type(input) == "table" then
		return input
	else
		return {input}
	end
end

local set_mt = {}
function set_mt.__index(table,key)
	return false
end

local function list_to_set(list)
	local set = {}
	for _, v in ipairs(list) do
		set[v] = true
	end
	set_metatable(set,set_mt)
	return set
end
Utils.list_to_set = list_to_set

function Utils.to_set(obj)
	if obj == nil then
		return {}
	elseif type(obj) == "table" then
		return list_to_set(obj)
	else
		return {[obj]=true}
	end
end

function Utils.set_to_list(set)
	local list = {}
	for k, v in pairs(set) do
		if v then
			list[#list+1] = k
		end
	end
	return list
end

function Utils.is_in_list(el,list)
	for _, v in ipairs(list) do
		if v == el then
			return true
		end
	end
	return false
end

function Utils.shallow_copy(t)
	local rtn = {}
	for key, val in pairs(t) do
		rtn[key] = val
	end
	return rtn
end

return Utils