parent
f9c9855462
commit
9ba521839e
2 changed files with 7 additions and 51 deletions
@ -1,35 +0,0 @@ |
||||
type IsEmptyTypeMap = Pick< |
||||
MapToType, |
||||
'number' | 'object' | 'string' | 'undefined' |
||||
>; |
||||
|
||||
type MapToValueIsEmptyFunction = { |
||||
[TypeName in keyof IsEmptyTypeMap]: ( |
||||
value: IsEmptyTypeMap[TypeName], |
||||
) => boolean; |
||||
}; |
||||
|
||||
const MAP_TO_VALUE_IS_EMPTY_FUNCTION: MapToValueIsEmptyFunction = { |
||||
number: (value = 0) => value === 0, |
||||
object: (value) => Object.keys(value).length === 0, |
||||
string: (value = '') => value.trim().length === 0, |
||||
undefined: () => true, |
||||
}; |
||||
|
||||
const isEmpty = <TypeName extends keyof IsEmptyTypeMap>( |
||||
values: Array<IsEmptyTypeMap[TypeName]>, |
||||
{ not, fn = 'every' }: { not?: boolean; fn?: 'every' | 'some' } = {}, |
||||
): boolean => |
||||
values[fn]((value) => { |
||||
const type = typeof value as TypeName; |
||||
|
||||
let result = MAP_TO_VALUE_IS_EMPTY_FUNCTION[type](value); |
||||
|
||||
if (not) { |
||||
result = !result; |
||||
} |
||||
|
||||
return result; |
||||
}); |
||||
|
||||
export default isEmpty; |
Loading…
Reference in new issue