diff options
author | Florian Dold <florian@dold.me> | 2021-02-16 11:34:50 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-02-16 13:47:01 +0100 |
commit | db59275b6b43f8fa7f36899ae81cb7139a2e80cb (patch) | |
tree | 66ef429005a0ab239a212c39ad4501b29f727f97 /packages/idb-bridge/src/util/injectKey.ts | |
parent | d1f00aeaa26af6835ea3f47ac280b1e67d672fa2 (diff) | |
download | wallet-core-db59275b6b43f8fa7f36899ae81cb7139a2e80cb.tar.gz wallet-core-db59275b6b43f8fa7f36899ae81cb7139a2e80cb.tar.bz2 wallet-core-db59275b6b43f8fa7f36899ae81cb7139a2e80cb.zip |
add more tests and fix various issues
Diffstat (limited to 'packages/idb-bridge/src/util/injectKey.ts')
-rw-r--r-- | packages/idb-bridge/src/util/injectKey.ts | 20 |
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; |