Skip to content

Commit

Permalink
feat: Make isEmpty work for Sets (silvermine#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
crgwbr committed Jun 22, 2021
1 parent d9a5eb5 commit 00e2024
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from './utils/is-number';
export * from './utils/is-object';
export * from './utils/is-promise';
export * from './utils/is-promise-like';
export * from './utils/is-set';
export * from './utils/is-string';
export * from './utils/is-undefined';

Expand Down
4 changes: 4 additions & 0 deletions src/utils/is-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isArray } from './is-array';
import { isString } from './is-string';
import { isArguments } from './is-arguments';
import { isUndefined } from './is-undefined';
import { isSet } from './is-set';

/**
* Checks if `o` is an empty object. An object is "empty" if it:
Expand All @@ -19,5 +20,8 @@ export function isEmpty(o: any): boolean {
if (isArray(o) || isString(o) || isArguments(o)) {
return o.length === 0;
}
if (isSet(o)) {
return o.size === 0;
}
return Object.keys(o).length === 0;
}
8 changes: 8 additions & 0 deletions src/utils/is-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Type guard for `Set`s.
*
* @returns `true` if `o` is a `Set`, regardless of the types that it contains
*/
export function isSet(o: unknown): o is Set<unknown> {
return o instanceof Set;
}
2 changes: 2 additions & 0 deletions tests/utils/is-empty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('isEmpty', () => {
let o;

expect(t.isEmpty([])).to.strictlyEqual(true);
expect(t.isEmpty(new Set([]))).to.strictlyEqual(true);
// eslint-disable-next-line
expect(t.isEmpty(new Array())).to.strictlyEqual(true);
expect(t.isEmpty({})).to.strictlyEqual(true);
Expand Down Expand Up @@ -42,6 +43,7 @@ describe('isEmpty', () => {
expect(t.isEmpty({ length: 0 })).to.strictlyEqual(false);
expect(t.isEmpty([ 1 ])).to.strictlyEqual(false);
expect(t.isEmpty(new Array(10))).to.strictlyEqual(false);
expect(t.isEmpty(new Set([ 1 ]))).to.strictlyEqual(false);
});

});
30 changes: 30 additions & 0 deletions tests/utils/is-set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from 'chai';
import * as t from '../../src/index';


describe('isSet', () => {

it('correctly classifies sets', () => {
expect(t.isSet(new Set([]))).to.strictlyEqual(true);
expect(t.isSet(new Set([ 'a', 'b', 'c' ]))).to.strictlyEqual(true);
expect(t.isSet(new Set([ 4 ]))).to.strictlyEqual(true);
expect(t.isSet(new Set([ 'a', 'b', 'c', 4 ]))).to.strictlyEqual(true);
// eslint-disable-next-line
expect(t.isSet(new Set())).to.strictlyEqual(true);
});

it('correctly classifies non-sets', () => {
expect(t.isSet([])).to.strictlyEqual(false);
expect(t.isSet({})).to.strictlyEqual(false);
expect(t.isSet(4)).to.strictlyEqual(false);
expect(t.isSet('')).to.strictlyEqual(false);
expect(t.isSet('a')).to.strictlyEqual(false);
expect(t.isSet(true)).to.strictlyEqual(false);
expect(t.isSet(undefined)).to.strictlyEqual(false);
expect(t.isSet(null)).to.strictlyEqual(false);
expect(t.isSet({ length: 0 })).to.strictlyEqual(false);
// eslint-disable-next-line no-empty-function
expect(t.isSet(() => {})).to.strictlyEqual(false);
});

});

0 comments on commit 00e2024

Please sign in to comment.