This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
website/hooks/useForm/replaceEmptyStringInObjectToNull.ts

20 lines
441 B
TypeScript

import type { ObjectAny } from '../../tools/types'
export const replaceEmptyStringInObjectToNull = (
object: ObjectAny,
required: string[] = []
): ObjectAny => {
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]
})
)
}