This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
Files
react-component-form/src/utils/handleOptionalEmptyStringToNull.ts
2022-08-25 23:24:40 +02:00

18 lines
380 B
TypeScript

export const handleOptionalEmptyStringToNull = <K>(
object: K,
required: string[] = []
): K => {
return Object.fromEntries(
Object.entries(object).map(([key, value]) => {
if (
typeof value === 'string' &&
value.length === 0 &&
!required.includes(key)
) {
return [key, null]
}
return [key, value]
})
) as K
}