/** * Recursively cleans empty string values ("" or whitespace only) to null in form submission payloads, * unless explicitly exempted by sendEmpty configuration. */ export function cleanEmptyStrings(values: T, sendEmptyKeys?: Set | string[]): T { const exemptions = sendEmptyKeys instanceof Set ? sendEmptyKeys : new Set(sendEmptyKeys || []); function clean(val: any, currentKey?: string): any { if (val === null || val === undefined) { return null; } if (typeof val === "string") { if (val.trim() === "") { return currentKey && exemptions.has(currentKey) ? "" : null; } return val; } if (Array.isArray(val)) { return val.map((item) => clean(item, currentKey)); } if (typeof val === "object" && !(val instanceof Date) && !(val instanceof Blob) && !(val instanceof File)) { const cleanedObj: Record = {}; for (const [k, v] of Object.entries(val)) { cleanedObj[k] = clean(v, k); } return cleanedObj; } return val; } return clean(values); }