diff --git a/services/frontend/src/utils/forms.ts b/services/frontend/src/utils/forms.ts index 9704779..560ea3b 100644 --- a/services/frontend/src/utils/forms.ts +++ b/services/frontend/src/utils/forms.ts @@ -17,10 +17,98 @@ export const pruneArray = (array: (T | undefined)[]): T[] | undefined => { return result as T[]; }; -export const pruneObject = (object: T): unknown | undefined => { +export const pruneObject = (object: T): R | undefined => { const result = lodash.pickBy(object ?? {}, (value) => value !== undefined); if (Object.keys(result).length === 0) { return undefined; } - return result as unknown; + return result as unknown as R; }; + +export const pruneString = (value?: string): string | undefined => { + if (!value) { + return undefined; + } + return value; +}; + +export const splitKVPairs = ( + text: string, + seperator: string, + optional = true +): [string, string] => { + const firstIndex = text.indexOf(seperator); + if (firstIndex < 0) { + if (optional) { + return [text, ""]; + } else { + throw new Error("Malformed document!"); + } + } + const key = text.substring(0, firstIndex); + const value = text.substring(firstIndex + 1); + return [key, value]; +}; + +export type TTransformFunction = ( + item: string, + index: number +) => Record; + +export const loadObjectOrArrayAsObject = ( + transform: TTransformFunction, + object?: any +): any => { + if (!object) { + return undefined; + } + + if (Array.isArray(object)) { + return object.map(transform); + } + + return object; +}; + +export const loadArrayAsObject = ( + transform: TTransformFunction, + array?: string[] +) => { + if (!array) { + return undefined; + } + + if (!Array.isArray(array)) { + throw new Error("Malformed document. Expected an array at this point."); + } + + return array.map(transform); +}; + +export const extractObjectOrArray = ( + seperator: string, + keyName: string, + valueName: string, + object: any +): any => + loadObjectOrArrayAsObject((item: string) => { + const [key, value] = splitKVPairs(item, seperator); + return { + [keyName]: key, + [valueName]: value + }; + }, object); + +export const extractArray = ( + seperator: string, + keyName: string, + valueName: string, + object: any +): any => + loadArrayAsObject((item: string) => { + const [key, value] = splitKVPairs(item, seperator); + return { + [keyName]: key, + [valueName]: value + }; + }, object);