From b689529242b177124508657f2fc73e64c8469c40 Mon Sep 17 00:00:00 2001 From: Ben Stoltz Date: Fri, 24 May 2024 08:29:38 -0400 Subject: [PATCH] fix: deepFilter allows for blobs to pass through (#1524) --- package-lock.json | 48 +++++++------------ packages/common/src/objects/deepFilter.ts | 9 +++- .../common/test/objects/deepFilter.test.ts | 29 +++++++++++ 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index a1d3359d1cd..a3fb5973e9a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22688,10 +22688,9 @@ }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash._baseindexof": { "version": "3.1.0", - "dev": true, + "extraneous": true, "inBundle": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash._baseuniq": { "version": "4.6.0", @@ -22706,24 +22705,21 @@ }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash._bindcallback": { "version": "3.0.1", - "dev": true, + "extraneous": true, "inBundle": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash._cacheindexof": { "version": "3.0.2", - "dev": true, + "extraneous": true, "inBundle": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash._createcache": { "version": "3.1.2", - "dev": true, + "extraneous": true, "inBundle": true, "license": "MIT", - "peer": true, "dependencies": { "lodash._getnative": "^3.0.0" } @@ -22737,10 +22733,9 @@ }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash._getnative": { "version": "3.9.1", - "dev": true, + "extraneous": true, "inBundle": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash._root": { "version": "3.0.1", @@ -22758,10 +22753,9 @@ }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash.restparam": { "version": "3.6.1", - "dev": true, + "extraneous": true, "inBundle": true, - "license": "MIT", - "peer": true + "license": "MIT" }, "node_modules/cz-lerna-changelog/node_modules/npm/node_modules/lodash.union": { "version": "4.6.0", @@ -65016,7 +65010,7 @@ }, "packages/common": { "name": "@esri/hub-common", - "version": "14.133.4", + "version": "14.134.0", "license": "Apache-2.0", "dependencies": { "@terraformer/arcgis": "^2.1.2", @@ -83471,8 +83465,7 @@ "lodash._baseindexof": { "version": "3.1.0", "bundled": true, - "dev": true, - "peer": true + "extraneous": true }, "lodash._baseuniq": { "version": "4.6.0", @@ -83487,20 +83480,17 @@ "lodash._bindcallback": { "version": "3.0.1", "bundled": true, - "dev": true, - "peer": true + "extraneous": true }, "lodash._cacheindexof": { "version": "3.0.2", "bundled": true, - "dev": true, - "peer": true + "extraneous": true }, "lodash._createcache": { "version": "3.1.2", "bundled": true, - "dev": true, - "peer": true, + "extraneous": true, "requires": { "lodash._getnative": "^3.0.0" } @@ -83514,8 +83504,7 @@ "lodash._getnative": { "version": "3.9.1", "bundled": true, - "dev": true, - "peer": true + "extraneous": true }, "lodash._root": { "version": "3.0.1", @@ -83532,8 +83521,7 @@ "lodash.restparam": { "version": "3.6.1", "bundled": true, - "dev": true, - "peer": true + "extraneous": true }, "lodash.union": { "version": "4.6.0", diff --git a/packages/common/src/objects/deepFilter.ts b/packages/common/src/objects/deepFilter.ts index fdfa5b4dc55..983cbcec343 100644 --- a/packages/common/src/objects/deepFilter.ts +++ b/packages/common/src/objects/deepFilter.ts @@ -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]; } diff --git a/packages/common/test/objects/deepFilter.test.ts b/packages/common/test/objects/deepFilter.test.ts index d93dbc3a750..a48f1634ee5 100644 --- a/packages/common/test/objects/deepFilter.test.ts +++ b/packages/common/test/objects/deepFilter.test.ts @@ -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";