aboutsummaryrefslogtreecommitdiff
path: root/packages/merchant-backoffice-ui/src/utils/table.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/merchant-backoffice-ui/src/utils/table.ts')
-rw-r--r--packages/merchant-backoffice-ui/src/utils/table.ts16
1 files changed, 16 insertions, 0 deletions
diff --git a/packages/merchant-backoffice-ui/src/utils/table.ts b/packages/merchant-backoffice-ui/src/utils/table.ts
index 4ebd95807..199e5fda5 100644
--- a/packages/merchant-backoffice-ui/src/utils/table.ts
+++ b/packages/merchant-backoffice-ui/src/utils/table.ts
@@ -35,3 +35,19 @@ export function buildActions<T extends WithId>(instances: T[], selected: string[
.filter(notEmpty)
.map(id => ({ element: id, type: action }))
}
+
+/**
+ * For any object or array, return the same object if is not empty.
+ * not empty:
+ * - for arrays: at least one element not undefined
+ * - for objects: at least one property not undefined
+ * @param obj
+ * @returns
+ */
+export function undefinedIfEmpty<T extends Record<string, unknown>|Array<unknown>>(obj: T): T | undefined {
+ if (obj === undefined) return undefined
+ return Object.values(obj).some((v) => v !== undefined)
+ ? obj
+ : undefined;
+}
+