diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-06-15 22:44:54 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-06-15 22:44:54 +0200 |
commit | 2ee9431f1ba5bf67546bbf85758a01991c40673f (patch) | |
tree | 4581c4f3c966d742c66ea7f4bae4f9a3f8e2f5ff /packages/idb-bridge/src/util/canInjectKey.ts | |
parent | 65eb8b96f894491d406f91070df53ccbd43d19c9 (diff) | |
download | wallet-core-2ee9431f1ba5bf67546bbf85758a01991c40673f.tar.xz |
idb wip
Diffstat (limited to 'packages/idb-bridge/src/util/canInjectKey.ts')
-rw-r--r-- | packages/idb-bridge/src/util/canInjectKey.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/idb-bridge/src/util/canInjectKey.ts b/packages/idb-bridge/src/util/canInjectKey.ts new file mode 100644 index 000000000..c6c9c24ab --- /dev/null +++ b/packages/idb-bridge/src/util/canInjectKey.ts @@ -0,0 +1,34 @@ +import { KeyPath, Value } from "./types"; + +// http://w3c.github.io/IndexedDB/#check-that-a-key-could-be-injected-into-a-value +const canInjectKey = (keyPath: KeyPath, value: Value) => { + if (Array.isArray(keyPath)) { + // tslint:disable-next-line max-line-length + throw new Error( + "The key paths used in this section are always strings and never sequences, since it is not possible to create a object store which has a key generator and also has a key path that is a sequence.", + ); + } + + const identifiers = keyPath.split("."); + if (identifiers.length === 0) { + throw new Error("Assert: identifiers is not empty"); + } + identifiers.pop(); + + for (const identifier of identifiers) { + if (typeof value !== "object" && !Array.isArray(value)) { + return false; + } + + const hop = value.hasOwnProperty(identifier); + if (!hop) { + return true; + } + + value = value[identifier]; + } + + return typeof value === "object" || Array.isArray(value); +}; + +export default canInjectKey; |