2021-11-26 16:38:17 +00:00
|
|
|
local utilities = {}
|
|
|
|
utilities.deepcopy = function(orig)
|
|
|
|
local orig_type = type(orig)
|
|
|
|
local copy
|
|
|
|
local depth = depth
|
|
|
|
if orig_type == 'table' then
|
|
|
|
copy = {}
|
|
|
|
for orig_key, orig_value in next, orig, nil do
|
|
|
|
copy[utilities.deepcopy(orig_key)] = utilities.deepcopy(orig_value)
|
|
|
|
end
|
|
|
|
setmetatable(copy, utilities.deepcopy(getmetatable(orig)))
|
|
|
|
else -- number, string, boolean, etc
|
|
|
|
copy = orig
|
|
|
|
end
|
|
|
|
return copy
|
|
|
|
end
|
|
|
|
utilities.slice = function(list,start,list_end)
|
2022-05-20 18:20:49 +00:00
|
|
|
local output = {}
|
|
|
|
for I = (start or 1),(list_end or #table) do
|
2022-05-20 21:21:23 +00:00
|
|
|
table.insert(output,list[I])
|
2022-05-20 18:20:49 +00:00
|
|
|
end
|
|
|
|
return output
|
2021-11-26 16:38:17 +00:00
|
|
|
end
|
|
|
|
utilities.shallowcopy = function(orig)
|
2022-05-20 18:20:49 +00:00
|
|
|
local copy = {}
|
|
|
|
for k,v in pairs(orig) do
|
2022-05-20 21:21:23 +00:00
|
|
|
copy[k] = v
|
2022-05-20 18:20:49 +00:00
|
|
|
end
|
|
|
|
return copy
|
2021-11-26 16:38:17 +00:00
|
|
|
end
|
2022-05-20 21:21:23 +00:00
|
|
|
|
|
|
|
utilities.listcopy = function(orig)
|
|
|
|
local list = {}
|
|
|
|
for k,v in pairs(orig) do
|
|
|
|
table.insert(list,v)
|
|
|
|
end
|
|
|
|
return list
|
|
|
|
end
|
2021-11-26 16:38:17 +00:00
|
|
|
--overwrite the original table's properties with new properties
|
|
|
|
utilities.overwrite = function(original,overwrite)
|
2022-05-20 18:20:49 +00:00
|
|
|
local new = utilities.shallowcopy(original)
|
|
|
|
for k,v in pairs(overwrite) do
|
2022-05-20 21:21:23 +00:00
|
|
|
new[k] = v
|
2022-05-20 18:20:49 +00:00
|
|
|
end
|
|
|
|
return new
|
2021-11-26 16:38:17 +00:00
|
|
|
end
|
|
|
|
--merge all objects passed as arguments into a table.
|
|
|
|
--if the object is a table, merge all of it's contents with the table
|
|
|
|
utilities.merge = function(...)
|
2022-05-20 18:20:49 +00:00
|
|
|
local args = {...}
|
|
|
|
local new = {}
|
|
|
|
for k,v in pairs(args) do
|
2022-05-20 21:21:23 +00:00
|
|
|
if type(v) == "table" then
|
|
|
|
for k2,v2 in pairs(v) do
|
|
|
|
table.insert(new,v2)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
table.insert(new,v)
|
2022-05-20 18:20:49 +00:00
|
|
|
end
|
2021-11-26 16:38:17 +00:00
|
|
|
end
|
2022-05-20 18:20:49 +00:00
|
|
|
return new
|
2021-11-26 16:38:17 +00:00
|
|
|
end
|
|
|
|
utilities.remove_value = function(tb,v)
|
|
|
|
local id_to_remove = nil
|
|
|
|
for k,f in pairs(tb) do
|
|
|
|
if f == v then
|
|
|
|
id_to_remove = k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if id_to_remove then
|
|
|
|
table.remove(tb,id_to_remove)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
utilities.count = function(tb)
|
|
|
|
local count = 0
|
|
|
|
for k,v in pairs(tb) do
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
utilities.exists = function(tb,vc)
|
|
|
|
local kout
|
|
|
|
for k,v in pairs(tb) do
|
|
|
|
if v == vc then
|
|
|
|
kout = k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return kout
|
|
|
|
end
|
|
|
|
return utilities
|