Skip to content

Commit

Permalink
Bugfix: Support single-valued compound keys
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))``
  • Loading branch information
dfahlander committed Aug 30, 2023
1 parent cb00358 commit 93ea5eb
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 93ea5eb

Please sign in to comment.