export function deleteKeyFromNestedObject()

in glean/src/core/storage.ts [176:198]


export function deleteKeyFromNestedObject(obj: JSONObject, index: StorageIndex): JSONObject {
  if (index.length === 0) {
    return {};
  }

  const returnObject = { ...obj };
  let target = returnObject;

  // Loops through all the keys but the last.
  // The last is the key we will **delete**, not **get**.
  for (const key of index.slice(0, index.length - 1)) {
    const value = target[key];
    if (!isObject(value)) {
      throw Error(`Attempted to delete an entry from an inexistent index: ${JSON.stringify(index)}.`);
    } else {
      target = value;
    }
  }

  const finalKey = index[index.length - 1];
  delete target[finalKey];
  return returnObject;
}