Skip to content

Commit ad25a69

Browse files
committed
feat: Add isSet type guard (#31)
1 parent e8b208c commit ad25a69

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export * from './utils/is-number';
2222
export * from './utils/is-object';
2323
export * from './utils/is-promise';
2424
export * from './utils/is-promise-like';
25+
export * from './utils/is-set';
2526
export * from './utils/is-string';
2627
export * from './utils/is-undefined';
2728
export * from './utils/is-null';

src/utils/is-set.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Type guard for `Set`s.
3+
*
4+
* @returns `true` if `o` is a `Set`, regardless of the types that it contains
5+
*/
6+
export function isSet(o: unknown): o is Set<unknown> {
7+
return o instanceof Set;
8+
}

tests/utils/is-set.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect } from 'chai';
2+
import * as t from '../../src/index';
3+
4+
5+
describe('isSet', () => {
6+
7+
it('correctly classifies sets', () => {
8+
expect(t.isSet(new Set([]))).to.strictlyEqual(true);
9+
expect(t.isSet(new Set([ 'a', 'b', 'c' ]))).to.strictlyEqual(true);
10+
expect(t.isSet(new Set([ 4 ]))).to.strictlyEqual(true);
11+
expect(t.isSet(new Set([ 'a', 'b', 'c', 4 ]))).to.strictlyEqual(true);
12+
expect(t.isSet(new Set())).to.strictlyEqual(true);
13+
});
14+
15+
it('correctly classifies non-sets', () => {
16+
expect(t.isSet([])).to.strictlyEqual(false);
17+
expect(t.isSet({})).to.strictlyEqual(false);
18+
expect(t.isSet(4)).to.strictlyEqual(false);
19+
expect(t.isSet('')).to.strictlyEqual(false);
20+
expect(t.isSet('a')).to.strictlyEqual(false);
21+
expect(t.isSet(true)).to.strictlyEqual(false);
22+
expect(t.isSet(undefined)).to.strictlyEqual(false);
23+
expect(t.isSet(null)).to.strictlyEqual(false);
24+
expect(t.isSet({ length: 0 })).to.strictlyEqual(false);
25+
expect(t.isSet(() => {})).to.strictlyEqual(false);
26+
});
27+
28+
});

0 commit comments

Comments
 (0)