Files
2026-10-30 21:16:18 -06:00

93 lines
2.7 KiB
Lua

-- Private -----------------------------------------------------------------------------------------
local private = {}
--[[
Add a value to the end of an array-like table.
Args:
target:array - a table which is being used like an array (i.e., contiguous integer keys)
value:any - the value to be appeneded to the array
Returns:
the target table
]]--
function private.append(target, value)
target[#target + 1] = value
return target
end
--[[
Performs a deep clone of a given table.
This function only clones regular keys, and makes no effort to preserve the metatables of the
resulting tables. It also does not attempt to handle cycles among the given tables. If provided
with a non-table value, then it simply returns the same object given without cloning it.
Args:
source:table - the table to be cloned
Returns:
a new table which the same keys and deeply copied values
]]--
function private.clone(source)
if type(source) ~= "table" then return source end
local target = {}
for key, value in pairs(source) do
target[key] = private.clone(value)
end
return target
end
--[[
Performs a shallow clone of a given table.
This is similar to `clone`, but it does not attempt to deeply copy the values of the given
table, instead creating a new table with the same keys and values as the original.
Args:
source:table - the table to be copied
Returns:
a new table with the same keys and values as the original
]]--
function private.copy(source)
return private.merge({}, source)
end
--[[
Overwrite the contents of a target table with the values in one or more source tables.
Only those keys defined by one of the source tables will be overridden in the target table.
Each successive source table in the given sequence may override values in the earlier source
tables. This function only performs a shallow copy, so any objects in the source tables are only copied
by reference. To create a brand new table with the merged contents of the following tables,
provide nil as the first argument.
Args:
target:table - a table whose values are to be overridden
...:table - a sequence of one or more source tables whose keys are to be copied
Returns:
the target table
]]--
function private.merge(target, ...)
target = target or nil
for _, source in ipairs({...}) do
for key, value in pairs(source) do
target[key] = value
end
end
return target
end
-- Export ------------------------------------------------------------------------------------------
_G.jvmi.table = private.merge({}, _G.table, private)