91 lines
2.5 KiB
Lua
91 lines
2.5 KiB
Lua
-- Imports -----------------------------------------------------------------------------------------
|
|
|
|
local api = _G["jvmi_table"]
|
|
local table = _G["table"]
|
|
|
|
|
|
-- API Functions -----------------------------------------------------------------------------------
|
|
|
|
--[[
|
|
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.
|
|
|
|
Args:
|
|
source:table - the table to be cloned
|
|
|
|
Returns:
|
|
a new table which the same keys and deeply copied values
|
|
]]--
|
|
function api.clone(source)
|
|
if type(source) ~= "table" then return source end
|
|
|
|
local target = {}
|
|
for key, value in pairs(source) do
|
|
target[key] = api.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 api.copy(source)
|
|
return api.merge({}, source)
|
|
end
|
|
|
|
--[[
|
|
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 api.append(target, value)
|
|
target[#target] = value
|
|
return target
|
|
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 api.merge(target, ...)
|
|
target = target or nil
|
|
|
|
for _, source in ipairs(arg) do
|
|
for key, value in pairs(source) do
|
|
target[key] = value
|
|
end
|
|
end
|
|
|
|
return target
|
|
end
|