mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(table/merge): add option to replace duplicate number keys (#581)
This commit is contained in:
@@ -70,16 +70,18 @@ end
|
|||||||
|
|
||||||
---@param t1 table
|
---@param t1 table
|
||||||
---@param t2 table
|
---@param t2 table
|
||||||
|
---@param addDuplicateNumbers boolean? add duplicate number keys together if true, replace if false. Defaults to true.
|
||||||
---@return table
|
---@return table
|
||||||
---Merges two tables together. Duplicate keys will be added together if they are numbers, or otherwise overwritten.
|
---Merges two tables together. Defaults to adding duplicate keys together if they are numbers, otherwise they are overriden.
|
||||||
local function table_merge(t1, t2)
|
local function table_merge(t1, t2, addDuplicateNumbers)
|
||||||
|
if addDuplicateNumbers == nil then addDuplicateNumbers = true end
|
||||||
for k, v in pairs(t2) do
|
for k, v in pairs(t2) do
|
||||||
local type1 = type(t1[k])
|
local type1 = type(t1[k])
|
||||||
local type2 = type(v)
|
local type2 = type(v)
|
||||||
|
|
||||||
if type1 == 'table' and type2 == 'table' then
|
if type1 == 'table' and type2 == 'table' then
|
||||||
table_merge(t1[k], v)
|
table_merge(t1[k], v, addDuplicateNumbers)
|
||||||
elseif type1 == 'number' and type2 == 'number' then
|
elseif addDuplicateNumbers and (type1 == 'number' and type2 == 'number') then
|
||||||
t1[k] += v
|
t1[k] += v
|
||||||
else
|
else
|
||||||
t1[k] = v
|
t1[k] = v
|
||||||
|
|||||||
Reference in New Issue
Block a user