From a69e559890d888d10739c8e29549ff37b788743a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 4 Nov 2022 11:18:30 -0300 Subject: better type signature for undefinedIfEmpty --- packages/merchant-backoffice-ui/src/utils/table.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'packages/merchant-backoffice-ui/src/utils') 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(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|Array>(obj: T): T | undefined { + if (obj === undefined) return undefined + return Object.values(obj).some((v) => v !== undefined) + ? obj + : undefined; +} + -- cgit v1.2.3