mirror of
https://github.com/littensy/charm.git
synced 2026-10-23 05:46:27 +00:00
fix: handle sparse array patches (#10)
This commit is contained in:
parent
9f28457452
commit
a5a183c3a6
2 changed files with 24 additions and 0 deletions
|
|
@ -44,8 +44,16 @@ local function apply(state: any, patches: any): any
|
|||
end
|
||||
|
||||
local nextState = table.clone(state)
|
||||
local stateIsArray = state[1] ~= nil
|
||||
|
||||
for key, patch in patches do
|
||||
-- Diff-checking an array produces a sparse array, which will not be
|
||||
-- preserved when converted to JSON. To prevent this, we turn string
|
||||
-- keys back into numeric keys.
|
||||
if stateIsArray and type(key) == "string" then
|
||||
key = tonumber(key) or key
|
||||
end
|
||||
|
||||
nextState[key] = apply(nextState[key], patch)
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue