observable-slim
observable-slim copied to clipboard
Add parent path to changes map
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
- 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage increased (+0.02%) to 95.262%
Totals | |
---|---|
Change from base Build 89: | 0.02% |
Covered Lines: | 237 |
Relevant Lines: | 237 |
💛 - 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.