Skip to content

Commit 16b1413

Browse files
committed
refactor(analytics): clean up filtering code
1 parent 08a964e commit 16b1413

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/v1/providers/analytics.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -182,29 +182,19 @@ export class UserDimensions {
182182
copyField(wireFormat, this, "userProperties", (r: unknown) => {
183183
const entries = Object.entries(r as Record<string, unknown>)
184184
.filter(([, v]) => {
185-
if (v === null || v === undefined) {
185+
// Property must be a non-empty object.
186+
if (v == null || typeof v !== "object" || Object.keys(v).length === 0) {
186187
return false;
187188
}
188-
if (typeof v !== "object") {
189-
return false;
190-
}
191-
const vObj = v as Record<string, unknown>;
192-
// Filter out empty objects
193-
if (Object.keys(vObj).length === 0) {
194-
return false;
189+
// If 'value' field exists, it must not be null, undefined, or an empty object.
190+
if (!("value" in v)) {
191+
return true;
195192
}
196-
// Filter if 'value' property exists but is null/undefined/empty object
197-
if ("value" in vObj) {
198-
const value = vObj.value;
199-
if (
200-
value === null ||
201-
value === undefined ||
202-
(typeof value === "object" && value !== null && Object.keys(value).length === 0)
203-
) {
204-
return false;
205-
}
206-
}
207-
return true;
193+
194+
const value = (v as { value: unknown }).value;
195+
const isEmptyObject =
196+
typeof value === "object" && value !== null && Object.keys(value).length === 0;
197+
return value != null && !isEmptyObject;
208198
})
209199
.map(([k, v]) => [k, new UserPropertyValue(v)]);
210200
return Object.fromEntries(entries);

0 commit comments

Comments
 (0)