Currently, I am developing a Typescript tool that deals with managing state by receiving data in the form of JSON objects and updates to those objects. My goal is to merge these objects together, expecting updates to existing properties most of the time, but also allowing for additions. The challenge lies in not knowing the structure of these objects during design time.
For instance, consider starting from an initial object like:
{
"kitchen": {
"door": "closed",
"cooker": "off",
"objects": [ "saucepan", "fork", "chopping board", "fresh coriander"]
"people": {}
}
}
Subsequently, receiving a series of updates resembling the following:
// update 1
{
"kitchen": {
"door": "open";
}
}
// update 2
{
"kitchen" : {
"people": { "basil": { "wearing": ["hat", "glasses"], "carrying": [] } }
}
}
// update 3
{
"kitchen" : {
"people": { "basil": { "carrying": [ "fork" ] } }
"objects": [ "saucepan", "chopping board", "fresh coriander"]
}
}
And so forth.
The desired end result of this process is to have the object structured as follows:
{
"kitchen": {
"door": "open",
"cooker": "off",
"people": { "basil": { "wearing": ["hat", "glasses"], "carrying": [ "fork" ] } }
"objects": [ "saucepan", "chopping board", "fresh coriander"]
}
}
It is important to note that the incoming and outgoing objects will be pure data represented in JSON format.
In order to achieve this, I aim to analyze the structure of the object, pinpointing changes and updates. While this task may be straightforward in Javascript, TypeScript requires a more structured approach which poses some limitations, especially since predefined interfaces won't be available within this module. The emphasis remains on ensuring new properties are added when necessary and existing ones are updated accordingly.
My inquiry is: What would be the most effective method to tackle this issue in Typescript? I seek a means to dissect the objects, treating them as nested dictionaries for comparison purposes, or recommendations on existing NPM modules that could assist in this specific scenario. It wouldn't come as a surprise if there already exists a module catered towards this purpose which has eluded my search thus far.