@@ -2,6 +2,32 @@ import { isArray } from './is-array';
22import { isString } from './is-string' ;
33import { isArguments } from './is-arguments' ;
44import { isUndefined } from './is-undefined' ;
5+ import { isNull } from './is-null' ;
6+ import { isBoolean } from './is-boolean' ;
7+ import { isNumber } from './is-number' ;
8+ import { isSet } from './is-set' ;
9+ import { isObject } from './is-object' ;
10+
11+
12+ interface IEmptyArguments extends IArguments {
13+ length : 0 ;
14+ }
15+
16+ interface IEmptyObj {
17+ [ s : string ] : never ;
18+ }
19+
20+ type IEmptyTypes = (
21+ null |
22+ undefined |
23+ boolean |
24+ number |
25+ never [ ] |
26+ '' |
27+ IEmptyArguments |
28+ Set < never > |
29+ IEmptyObj
30+ ) ;
531
632/**
733 * Checks if `o` is an empty object. An object is "empty" if it:
@@ -12,16 +38,15 @@ import { isUndefined } from './is-undefined';
1238 *
1339 * @returns `true` if `o` is empty
1440 */
15- export function isEmpty ( o : unknown ) : boolean {
16- if ( o === null || isUndefined ( o ) ) {
41+ export function isEmpty ( o : unknown ) : o is IEmptyTypes {
42+ if ( isNull ( o ) || isUndefined ( o ) || isBoolean ( o ) || isNumber ( o ) ) {
1743 return true ;
1844 }
1945 if ( isArray ( o ) || isString ( o ) || isArguments ( o ) ) {
2046 return o . length === 0 ;
2147 }
22- // Non-object arguments passed into Object.keys are coerced into objects (the only
23- // exception being undefined or null, which is handled above). Therefore, it's ok to
24- // assume the input is an object because it will be once passed through. See also:
25- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#using_object.keys_on_primitives
26- return Object . keys ( o as object ) . length === 0 ;
48+ if ( isSet ( o ) ) {
49+ return o . size === 0 ;
50+ }
51+ return isObject ( o ) && Object . keys ( o ) . length === 0 ;
2752}
0 commit comments