Skip to content

Commit 3dc5ffd

Browse files
authored
Merge pull request #34 from crgwbr/crweber/improve-is-empty
Improve isEmpty
2 parents 03a368b + b324aec commit 3dc5ffd

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/utils/is-empty.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@ import { isArray } from './is-array';
22
import { isString } from './is-string';
33
import { isArguments } from './is-arguments';
44
import { 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
}

tests/utils/is-empty.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe('isEmpty', () => {
66

77
it('correctly classifies empty things', () => {
88
expect(t.isEmpty([])).to.strictlyEqual(true);
9+
expect(t.isEmpty(new Set([]))).to.strictlyEqual(true);
910
// eslint-disable-next-line
1011
expect(t.isEmpty(new Array())).to.strictlyEqual(true);
1112
expect(t.isEmpty({})).to.strictlyEqual(true);
@@ -41,6 +42,7 @@ describe('isEmpty', () => {
4142
expect(t.isEmpty({ length: 0 })).to.strictlyEqual(false);
4243
expect(t.isEmpty([ 1 ])).to.strictlyEqual(false);
4344
expect(t.isEmpty(new Array(10))).to.strictlyEqual(false);
45+
expect(t.isEmpty(new Set([ 1 ]))).to.strictlyEqual(false);
4446
});
4547

4648
});

0 commit comments

Comments
 (0)