Propose special syntax for mergeObject to delete keys from an existing object during the merge
API Proposal
As you likely can tell by now, I love my mergeObject
helper and I use it a lot to simplify code in many places. Despite it's advantages one key limitation of this approach has been the resulting inability to remove existing keys from an object by way of a merge operation.
Example Problem
const obj = {foo: "bar", delete: "me"};
mergeObject(obj, {delete: null}); // No matter what you do
mergeObject(obj, {foo: "baz"}); // obj.delete still exists
Proposed Solution In order to get around this and allow the possibility to remove expired or deprecated data from an existing object in the data model, we can add a special syntax to designate a key to be deleted instead of merged. We can choose some syntax that is very unlikely (but not impossible) to collide with normal usage.
Example Solution
Prefix a key with -=
AND set it's value to null
to remove it from the object.
const obj = {foo: "bar", delete: "me"};
mergeObject(obj, {"-=delete": null}); // Returns {foo: "bar"}
Thoughts? Concerns? Comments? Alternative syntax you think would be better?
Practical Example An example of how you might use this in practice -- currently if you set a flag on an entity, there is never any way to remove that flag key - you can only null it out. So for example
actor.setFlag("module", "bogus", "oops"); // now you have actor.data.flags.module = {bogus: "oops"}
Before this change the best you could do was set it to null
or false
actor.setFlag("module", "bogus", null); // now you have actor.data.flags.module = {bogus: null}
Now you can actually remove the "bogus" flag with
actor.setFlag("module", "-=bogus", null); // now you have actor.data.flags.module = {}