Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deepFilter allows for blobs to pass through #1524

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading