Skip to content

Commit

Permalink
Bugfix: Support single-valued compound keys (#1795)
Browse files Browse the repository at this point in the history
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))``

Related issue: #1782
  • Loading branch information
dfahlander authored Aug 30, 2023
1 parent 7680f51 commit c2a2aa3
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/functions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down

0 comments on commit c2a2aa3

Please sign in to comment.