-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from crgwbr/crweber/new-guards
Add isBoolean and isSet type guards
- Loading branch information
Showing
5 changed files
with
57 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,3 @@ | ||
export function isBoolean(o: unknown): o is boolean { | ||
return o === true || o === false; | ||
} |
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,16 @@ | ||
import { expect } from 'chai'; | ||
import * as t from '../../src/index'; | ||
|
||
|
||
describe('isBoolean', () => { | ||
|
||
it('correctly classifies bools', () => { | ||
expect(t.isBoolean(true)).to.strictlyEqual(true); | ||
expect(t.isBoolean(false)).to.strictlyEqual(true); | ||
expect(t.isBoolean(null)).to.strictlyEqual(false); | ||
expect(t.isBoolean(0)).to.strictlyEqual(false); | ||
expect(t.isBoolean('')).to.strictlyEqual(false); | ||
expect(t.isBoolean({})).to.strictlyEqual(false); | ||
}); | ||
|
||
}); |
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); | ||
}); | ||
|
||
}); |