(obj: T | null | undefined)
| 42 | * Recursively normalize an object, converting null/undefined leaves to empty strings. |
| 43 | */ |
| 44 | export function normalizeObject<T extends Record<string, unknown>>(obj: T | null | undefined): T { |
| 45 | if (!obj) return {} as T; |
| 46 | const result: Record<string, unknown> = {}; |
| 47 | for (const [key, value] of Object.entries(obj)) { |
| 48 | if (value === null || value === undefined) { |
| 49 | result[key] = ''; |
| 50 | } else if (typeof value === 'object' && !Array.isArray(value)) { |
| 51 | result[key] = normalizeObject(value as Record<string, unknown>); |
| 52 | } else { |
| 53 | result[key] = value; |
| 54 | } |
| 55 | } |
| 56 | return result as T; |
| 57 | } |
| 58 | |
| 59 | /** Add fields to a record only if they have a defined value. */ |
| 60 | function addOptional(obj: Record<string, unknown>, fields: Record<string, unknown>): void { |
no outgoing calls
no test coverage detected