aboutsummaryrefslogtreecommitdiff
path: root/packages/merchant-backoffice-ui/src/utils
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-11-04 11:18:30 -0300
committerSebastian <sebasjm@gmail.com>2022-11-04 11:56:05 -0300
commita69e559890d888d10739c8e29549ff37b788743a (patch)
tree87c0c3976f64ece357e2de2b9727c07ee87dcf78 /packages/merchant-backoffice-ui/src/utils
parent80ab8ccce9a9ce46b445d0857769aadd640073a3 (diff)
downloadwallet-core-a69e559890d888d10739c8e29549ff37b788743a.tar.xz
better type signature for undefinedIfEmpty
Diffstat (limited to 'packages/merchant-backoffice-ui/src/utils')
-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;
+}
+