Diff Two Objects
Hard · Algorithms
Write diff(a, b) that returns the list of Patch operations that turn plain object a into plain object b. A path is made of /key parts, e.g. "/user/name".
For an object pair (starting with a and b):
1. Go through the keys of the OLD object in their order. If the key is missing in the new object, emit remove. If both values are plain objects (not arrays, not null), recurse into them right here. Otherwise, if the values are not deeply equal, emit replace with the new value. Arrays are compared as whole values.
2. Then go through the keys that exist only in the NEW object, in their order, and emit add with the new value.
Return [] when nothing changed.
Next: Apply Patch Operations