diff --git a/services/frontend/src/utils/forms.ts b/services/frontend/src/utils/forms.ts index 931a359..9704779 100644 --- a/services/frontend/src/utils/forms.ts +++ b/services/frontend/src/utils/forms.ts @@ -1,3 +1,5 @@ +import lodash from "lodash"; + export const checkArray = (array: any, name: string): T => { if (!Array.isArray(array)) { throw new Error( @@ -6,3 +8,19 @@ export const checkArray = (array: any, name: string): T => { } return array as unknown as T; }; + +export const pruneArray = (array: (T | undefined)[]): T[] | undefined => { + const result = array.filter(Boolean); + if (array.length === 0) { + return undefined; + } + return result as T[]; +}; + +export const pruneObject = (object: T): unknown | undefined => { + const result = lodash.pickBy(object ?? {}, (value) => value !== undefined); + if (Object.keys(result).length === 0) { + return undefined; + } + return result as unknown; +};