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.
api/src/tools/utils/parseStringNullish.ts

26 lines
562 B
TypeScript

/**
* Parse a nullish string:
* - if `string === undefined`, it returns `defaultString`
* - if `string === null`, it returns `null`
* - if `string.length === 0`, it returns `null`
* - else, it returns `string`
* @param defaultString
* @param string
* @returns
*/
export const parseStringNullish = (
defaultString: string | null,
string?: string | null
): string | null => {
if (string === undefined) {
return defaultString
}
if (string === null) {
return null
}
if (string.length === 0) {
return null
}
return string
}