in glean/src/core/storage.ts [129:164]
export function updateNestedObject(
obj: JSONObject,
index: StorageIndex,
transformFn: (v?: JSONValue) => JSONValue
): JSONObject {
if (index.length === 0) {
throw Error("The index must contain at least one property to update.");
}
const returnObject = { ...obj };
let target = returnObject;
// Loops through all the keys but the last.
// The last is the key we will **set**, not **get**.
for (const key of index.slice(0, index.length - 1)) {
if (!isObject(target[key])) {
target[key] = {};
}
// Typescript throws an error below about not being able to
// set target to target[key] because it may not be a JSONObject.
// We make sure by the above conditional that target[key] is a JSONObject,
// thus we can safely ignore Typescripts concerns.
//
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
target = target[key];
}
const finalKey = index[index.length - 1];
const current = target[finalKey];
const value = transformFn(current);
target[finalKey] = value;
return returnObject;
}