-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parent path to changes map #36
base: master
Are you sure you want to change the base?
Conversation
With an arbitrary set of object changes, including keys that contain dots or slashes, it can be difficult to retrieve, via dot notation, an object whose property is changing. Consider this set of operations: ``` let test; let p; const ObservableSlim = require("./observable-slim.js"); test = {}; p = ObservableSlim.create( test, false, (changes) => { console.log(JSON.stringify(changes, null, " ")); }); p.hello = "world"; p["a.b"] = { c : {} }; p["a.b"].c["e.f"] = 42; ``` The output of the final change is: ``` [ { "type": "add", "target": { "e.f": 42 }, "property": "e.f", "newValue": 42, "currentPath": "a.b.c.e.f", "jsonPointer": "/a/b/c/e/f", "proxy": { "e.f": 42 } } ] ``` Note that it is not exactly trivial to figure out how to update a remote object based on this information. There is nothing here that would tell you that of currentPath "a.b.c.e.f", the "a.b" part is a single key. To help resolve this problem, I have added the parent path to the change map. With the parent path, an array, it is easy to iterate through the array beginning at the top-level object, to then add/update/whatever the given property. With the change, the above test yields, as the final change, the following, which includes the `parent` array member: ``` [ { "type": "add", "target": { "e.f": 42 }, "parent": [ "a.b", "c" ], "property": "e.f", "newValue": 42, "currentPath": "a.b.c.e.f", "jsonPointer": "/a/b/c/e/f", "proxy": { "e.f": 42 } } ] ```
Pull Request Test Coverage Report for Build 101
💛 - Coveralls |
@derrell My sincere apologies for not seeing this until now! I'll take a look at this contribution this week or by early next week. One thing that I noticed right off the bat is the use of spaces instead of tabs for indentation. Would you be able to switch it to tabs on your fork? |
@ElliotNB Sorry about the formatting issue. I try to be careful to match the format of code I'm changing, but obviously missed it here. Fixed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking cool!
With an arbitrary set of object changes, including keys that contain dots or
slashes, it can be difficult to retrieve, via dot notation, an object whose
property is changing. Consider this set of operations:
The output of the final change is:
Note that it is not exactly trivial to figure out how to update a remote
object based on this information. There is nothing here that would tell you
that of currentPath "a.b.c.e.f", the "a.b" part is a single key.
To help resolve this problem, I have added the parent path to the change
map. With the parent path, an array, it is easy to iterate through the array
beginning at the top-level object, to then add/update/whatever the given
property. With the change, the above test yields, as the final change, the
following, which includes the
parent
array member: