Skip to content

Commit

Permalink
Merge pull request #57 from crgwbr/crweber/new-guards
Browse files Browse the repository at this point in the history
Add isBoolean and isSet type guards
  • Loading branch information
onebytegone authored Jul 26, 2024
2 parents dc99c6f + ad25a69 commit 9503bc9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export * from './utils/has-defined';
export * from './utils/is-arguments';
export * from './utils/is-array';
export * from './utils/is-array-of-strings';
export * from './utils/is-boolean';
export * from './utils/is-empty';
export * from './utils/is-enum-value';
export * from './utils/is-map-with-values-of-type';
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
3 changes: 3 additions & 0 deletions src/utils/is-boolean.ts
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;
}
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;
}
16 changes: 16 additions & 0 deletions tests/utils/is-boolean.test.ts
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);
});

});
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 9503bc9

Please sign in to comment.