Skip to content

Commit 0bf3e39

Browse files
committed
feat: Make isEmpty work for Sets (#31)
1 parent 03a368b commit 0bf3e39

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/utils/is-empty.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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 { isSet } from './is-set';
56

67
/**
78
* Checks if `o` is an empty object. An object is "empty" if it:
@@ -19,6 +20,9 @@ export function isEmpty(o: unknown): boolean {
1920
if (isArray(o) || isString(o) || isArguments(o)) {
2021
return o.length === 0;
2122
}
23+
if (isSet(o)) {
24+
return o.size === 0;
25+
}
2226
// Non-object arguments passed into Object.keys are coerced into objects (the only
2327
// exception being undefined or null, which is handled above). Therefore, it's ok to
2428
// assume the input is an object because it will be once passed through. See also:

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)