Skip to content

Commit

Permalink
Merge pull request #74 from lukebrody/master
Browse files Browse the repository at this point in the history
Fix error when adding undefined or null children in indexed objects
  • Loading branch information
dumbmatter authored Jun 8, 2022
2 parents 04ef1ac + 7740401 commit c0a7304
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib/extractKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const extractKey = (keyPath: KeyPath, value: Value) => {
remainingKeyPath = null;
}

if (!object.hasOwnProperty(identifier)) {
if (
object === undefined ||
object === null ||
!object.hasOwnProperty(identifier)
) {
return;
}

Expand Down
39 changes: 39 additions & 0 deletions src/test/fakeIndexedDB/fakeIndexedDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,43 @@ describe("fakeIndexedDB Tests", () => {
};
});
});

it("can use deep index keypaths on undefined objects", async () => {
const indexedDB = new FDBFactory();

function idb(): Promise<FDBDatabase> {
return new Promise((resolve, reject) => {
indexedDB.deleteDatabase("deepPath").onsuccess = () => {
const openreq = indexedDB.open("deepPath");
openreq.onupgradeneeded = event => {
const db: FDBDatabase = event.target.result;
const test = db.createObjectStore("test");
test.createIndex("deep", "foo.bar");
};
openreq.onsuccess = event => {
const db: FDBDatabase = event.target.result;
resolve(db);
};
openreq.onerror = reject;
};
});
}

const db1 = await idb();

const put = db1
.transaction(["test"], "readwrite")
.objectStore("test")
.put({ foo: undefined }, "key");

return new Promise((resolve, reject) => {
put.onsuccess = () => {
resolve();
};

put.onerror = () => {
reject(put.error);
};
});
});
});

0 comments on commit c0a7304

Please sign in to comment.