This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
export type Operation = "add" | "remove" | "replace" | ||
|
||
export type Patch<K, V> = { | ||
op: Operation, | ||
path: { K }, | ||
value: V, | ||
} | ||
|
||
--[=[ | ||
@within Dictionary | ||
Returns an array of patches that can be applied to `dictionary` to make it equal to `other`. This is a deep comparison. The patches are similar to those used in [JSON Patch](https://jsonpatch.com/). | ||
```lua | ||
local dictionary1 = { | ||
foo = "bar", | ||
qux = { | ||
baz = "quux", | ||
}, | ||
} | ||
local dictionary2 = { | ||
foo = "bar", | ||
qux = { | ||
baz = "quuz", | ||
}, | ||
baz = "quux", | ||
} | ||
patchDiff(dictionary1, dictionary2) --[[ | ||
{ | ||
{ op = "replace", path = { "qux", "baz" }, value = "quuz" }, | ||
{ op = "add", path = { "baz" }, value = "quux" }, | ||
} | ||
]] | ||
``` | ||
]=] | ||
local function patchDiff<K, V, T>(dictionary: { [K]: V }, other: { [K]: V }): { Patch<K, V> } | ||
local out: { any } = {} | ||
|
||
for key, value in dictionary do | ||
if other[key] == nil then | ||
table.insert(out, { | ||
op = "remove", | ||
path = { key }, | ||
value = value, | ||
}) | ||
|
||
continue | ||
end | ||
|
||
if typeof(value) == "table" then | ||
local subpatches = patchDiff(value, other[key]) | ||
|
||
for _, patch in subpatches do | ||
table.insert(out, { | ||
op = patch.op :: any, | ||
path = { key, table.unpack(patch.path) }, | ||
value = patch.value, | ||
}) | ||
end | ||
|
||
continue | ||
end | ||
|
||
if value ~= other[key] then | ||
table.insert(out, { | ||
op = "replace", | ||
path = { key }, | ||
value = other[key], | ||
}) | ||
end | ||
end | ||
|
||
for key, value in other do | ||
if dictionary[key] == nil then | ||
table.insert(out, { | ||
op = "add", | ||
path = { key }, | ||
value = value, | ||
}) | ||
end | ||
end | ||
|
||
return out | ||
end | ||
|
||
return patchDiff |