Skip to content

Commit

Permalink
feat: Add isSet type guard (silvermine#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
crgwbr committed Jul 26, 2024
1 parent e8b208c commit ad25a69
Show file tree
Hide file tree
Showing 3 changed files with 37 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 @@ -22,6 +22,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';
export * from './utils/is-null';
Expand Down
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;
}
28 changes: 28 additions & 0 deletions tests/utils/is-set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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);
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);
expect(t.isSet(() => {})).to.strictlyEqual(false);
});

});

0 comments on commit ad25a69

Please sign in to comment.