Skip to content

Commit

Permalink
fix: deepFilter allows for blobs to pass through (#1524)
Browse files Browse the repository at this point in the history
  • Loading branch information
benstoltz authored May 24, 2024
1 parent 8d2e21f commit b689529
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
48 changes: 18 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions packages/common/src/objects/deepFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ export function deepFilter(
return Object.keys(object).reduce((acc, entry) => {
if (predicate(object[entry])) {
if (isFindable(object[entry])) {
const filteredEntry = deepFilter(object[entry], predicate);
(acc as any)[entry] = filteredEntry;
// Explicilty checking for Blob here, and copying the reference forward so it is maintained
if (typeof Blob !== "undefined" && object[entry] instanceof Blob) {
(acc as any)[entry] = object[entry];
} else {
const filteredEntry = deepFilter(object[entry], predicate);
(acc as any)[entry] = filteredEntry;
}
} else {
(acc as any)[entry] = object[entry];
}
Expand Down
29 changes: 29 additions & 0 deletions packages/common/test/objects/deepFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,35 @@ describe("deepFilter:", () => {
expect(chk.level1.level2.level3a).toBeUndefined();
expect(chk.level1.level2.level3b.status).toBe("not-started");
});
if (typeof Blob !== "undefined") {
it("allows Blobs through and does not filter into the blob", () => {
const blob = new Blob(["a"], { type: "text/plain" });
const test = {
id: "a",
image: {
blob,
name: "my-image",
},
};
const isFieldEmpty = (value: any) => {
let isEmpty = false;
if (typeof value === "string") {
isEmpty = value === "";
} else if (value instanceof Blob) {
isEmpty = false;
} else if (typeof value === "object") {
isEmpty = !Object.keys(value)?.length;
}

return isEmpty;
};
const predicate = (value: any) => !isFieldEmpty(value);
const chk = deepFilter(test, predicate);

expect(chk.id).toBe("a");
expect(chk.image.blob instanceof Blob).toBe(true);
});
}
it("skips dates", () => {
const test = new Date();
const predicate = (obj: any) => obj.id === "b";
Expand Down

0 comments on commit b689529

Please sign in to comment.