From be5e09369775bbefd378f2e0393c8c505cfce493 Mon Sep 17 00:00:00 2001 From: dfahlander Date: Mon, 28 Aug 2023 10:14:54 +0200 Subject: [PATCH] Bugfix: Support single-valued compound keys Javascript is a bit unsuprisingly forgiving when passing array of string instead of a string: ```js const obj = {id: 1} Object.hasOwnProperty(obj, "a"); // returns true as expected. Object.hasOwnProperty(obj, ["a"]); // Also returns true (!) obj["id"] // returns 1 as expected. obj[["id"]] // also returns 1 (!) ``` This commit makes sure to check typeof keyPath before passing it to getOwn() (alias for `(x, a) => Object.prototype.getOwnProperty.call(x, a))`` --- src/functions/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions/utils.ts b/src/functions/utils.ts index 8e799ecc9..97368addf 100644 --- a/src/functions/utils.ts +++ b/src/functions/utils.ts @@ -113,7 +113,7 @@ export function tryCatch(fn: (...args: any[])=>void, onerror, args?) : void { export function getByKeyPath(obj, keyPath) { // http://www.w3.org/TR/IndexedDB/#steps-for-extracting-a-key-from-a-value-using-a-key-path - if (hasOwn(obj, keyPath)) return obj[keyPath]; // This line is moved from last to first for optimization purpose. + if (typeof keyPath === 'string' && hasOwn(obj, keyPath)) return obj[keyPath]; // This line is moved from last to first for optimization purpose. if (!keyPath) return obj; if (typeof keyPath !== 'string') { var rv = [];