-
Notifications
You must be signed in to change notification settings - Fork 13
Description
Is your feature request related to a problem? Please describe.
Currently items in lists are compared by index.
In some cases, list patches might be much smaller if we would use unique identifier present in items to distinguish if they were moved around. For example:
original = [
%{id: 1, name: "test1"},
%{id: 2, name: "test2"},
%{id: 3, name: "test3"},
%{id: 4, name: "test4"},
%{id: 5, name: "test5"},
%{id: 6, name: "test6"},
]
updated = List.insert_at(original, 0, %{id: 123, name: "test123"})
Jsonpatch.diff(original, updated)Output:
[
%{value: %{id: 6, name: "test6"}, path: "/6", op: "add"},
%{value: "test5", path: "/5/name", op: "replace"},
%{value: 5, path: "/5/id", op: "replace"},
%{value: "test4", path: "/4/name", op: "replace"},
%{value: 4, path: "/4/id", op: "replace"},
%{value: "test3", path: "/3/name", op: "replace"},
%{value: 3, path: "/3/id", op: "replace"},
%{value: "test2", path: "/2/name", op: "replace"},
%{value: 2, path: "/2/id", op: "replace"},
%{value: "test1", path: "/1/name", op: "replace"},
%{value: 1, path: "/1/id", op: "replace"},
%{value: "test123", path: "/0/name", op: "replace"},
%{value: 123, path: "/0/id", op: "replace"}
]Describe the solution you'd like
I'd love to have additional option to diff being a function returning unique identifier for a given object &object_hash/1 that would trigger different diffing mechanism for lists.
Output I'd like to get for the previous example:
Jsonpatch.diff(original, updated, object_hash: fn %{id: id} -> id end)
[
%{value: %{id: 123, name: "test123"}, path: "/0", op: "add"}
]Describe alternatives you've considered
Some JS libraries support such option, eg jsondiffpatch
Additional context
I'm planning to use json_patch in LiveVue to reduce payload size. Already playing with it. It's very common that items in lists have unique identifiers (eg. id) that should make it possible to create much more optimized patches.