forked from silvermine/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add isSet type guard (silvermine#31)
- Loading branch information
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |