mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 19:54:09 +00:00
fix: check none before patching mutable tables
This commit is contained in:
parent
189ae0f168
commit
422ee43a94
2 changed files with 19 additions and 13 deletions
|
|
@ -127,4 +127,10 @@ test("patches mutable state", function()
|
||||||
assert(targetState.qaz == "qux", "should add the new value")
|
assert(targetState.qaz == "qux", "should add the new value")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
test("removes table from mutable state", function()
|
||||||
|
local targetState = { foo = { bar = "baz" } }
|
||||||
|
patch.applyMutable(targetState, { foo = { __none = "__none" } })
|
||||||
|
assert(targetState.foo == nil, "should remove the table")
|
||||||
|
end)
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
|
||||||
|
|
@ -112,22 +112,22 @@ local function apply(oldState: any, diffs: any): any
|
||||||
end
|
end
|
||||||
|
|
||||||
local function applyMutable(targetState: any, sourceState: any): any
|
local function applyMutable(targetState: any, sourceState: any): any
|
||||||
if type(targetState) == "table" and type(sourceState) == "table" then
|
if isNone(sourceState) then
|
||||||
local targetIsArray = targetState[1] ~= nil
|
|
||||||
|
|
||||||
for key, value in sourceState do
|
|
||||||
if targetIsArray and type(key) == "string" then
|
|
||||||
key = tonumber(key) or key
|
|
||||||
end
|
|
||||||
targetState[key] = applyMutable(targetState[key], value)
|
|
||||||
end
|
|
||||||
|
|
||||||
return targetState
|
|
||||||
elseif isNone(sourceState) then
|
|
||||||
return nil
|
return nil
|
||||||
else
|
elseif type(targetState) ~= "table" or type(sourceState) ~= "table" then
|
||||||
return sourceState
|
return sourceState
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local targetIsArray = targetState[1] ~= nil
|
||||||
|
|
||||||
|
for key, value in sourceState do
|
||||||
|
if targetIsArray and type(key) == "string" then
|
||||||
|
key = tonumber(key) or key
|
||||||
|
end
|
||||||
|
targetState[key] = applyMutable(targetState[key], value)
|
||||||
|
end
|
||||||
|
|
||||||
|
return targetState
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue