Diff Two Objects
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write diff(before, after), where both are plain objects, to report what changed between two versions of some data. Return an object { added, removed, changed }: three arrays of paths, with the keys along each path joined by dots, each array sorted with JavaScript's default sort. A key is added when only after has it, removed when only before has it, and changed when both have it with different values. A key counts as there when it's an own key, even if its value is undefined. When both sides hold plain objects, or both hold arrays, under the same key, compare inside them instead, using array positions as keys, so a changed second tag is tags.1 and an extra third one is added as tags.2. Anything else is reported at its own path: an added or removed object as one path, not one per key inside it, and a value that changes kind, say from an object to a number or from an array to an object, as changed. Compare all other values with ===.
Your code must pass
- Lists added, removed and changed keys
- Reports nested changes as dotted paths
- Compares arrays position by position
- Reports a change of kind at its own path
- Sorts each list of paths
- Counts a key holding undefined as there
- Returns empty lists for equal objects