summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/util/injectKey.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/idb-bridge/src/util/injectKey.ts')
-rw-r--r--packages/idb-bridge/src/util/injectKey.ts20
1 files changed, 11 insertions, 9 deletions
diff --git a/packages/idb-bridge/src/util/injectKey.ts b/packages/idb-bridge/src/util/injectKey.ts
index 63c8deda4..02acfaa4c 100644
--- a/packages/idb-bridge/src/util/injectKey.ts
+++ b/packages/idb-bridge/src/util/injectKey.ts
@@ -30,6 +30,11 @@ export function injectKey(
);
}
+ const newValue = structuredClone(value);
+
+ // Position inside the new value where we'll place the key eventually.
+ let ptr = newValue;
+
const identifiers = keyPath.split(".");
if (identifiers.length === 0) {
throw new Error("Assert: identifiers is not empty");
@@ -42,26 +47,23 @@ export function injectKey(
}
for (const identifier of identifiers) {
- if (typeof value !== "object" && !Array.isArray(value)) {
- return false;
+ if (typeof ptr !== "object" && !Array.isArray(ptr)) {
+ throw new Error("can't inject key");
}
const hop = value.hasOwnProperty(identifier);
if (!hop) {
- return true;
+ ptr[identifier] = {};
}
- value = value[identifier];
+ ptr = ptr[identifier];
}
- if (!(typeof value === "object" || Array.isArray(value))) {
+ if (!(typeof ptr === "object" || Array.isArray(ptr))) {
throw new Error("can't inject key");
}
- const newValue = structuredClone(value);
- newValue[lastIdentifier] = structuredClone(key);
+ ptr[lastIdentifier] = structuredClone(key);
return newValue;
}
-
-export default injectKey;