forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isEmpty.ts
51 lines (49 loc) · 1.57 KB
/
isEmpty.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { isArray } from './isArray';
import { isObject } from './isObject';
import { isString } from './isString';
/**
* A function that checks if the passed parameter is empty.
*
* `undefined` is also considered empty, but only when it's in a union with a
* `string` or string-like type.
*
* This guard doesn't work negated because of typescript limitations! If you
* need to check that an array is *not* empty, use `R.hasAtLeast(data, 1)`
* and not `!R.isEmpty(data)`. For strings and objects there's no way in
* typescript to narrow the result to a non-empty type.
*
* @param data the variable to check
* @signature
* R.isEmpty(data)
* @returns true if the passed input is empty, false otherwise
* @example
* R.isEmpty(undefined) //=>true
* R.isEmpty('') //=> true
* R.isEmpty([]) //=> true
* R.isEmpty({}) //=> true
* R.isEmpty('test') //=> false
* R.isEmpty([1, 2, 3]) //=> false
* R.isEmpty({ length: 0 }) //=> false
* @category Guard
*/
export function isEmpty<T extends string | undefined>(
data: T
): data is
| ('' extends T ? '' : never)
| (undefined extends T ? undefined : never);
export function isEmpty(data: ReadonlyArray<unknown> | []): data is [];
export function isEmpty<T extends Readonly<Record<PropertyKey, unknown>>>(
data: T
): data is Record<keyof T, never>;
export function isEmpty(data: unknown): boolean {
if (data === undefined) {
return true;
}
if (isArray(data) || isString(data)) {
return data.length === 0;
}
if (isObject(data)) {
return Object.keys(data).length === 0;
}
return false;
}