Skip to content

Commit 9d6eb16

Browse files
author
monaqa
committed
Add functionality related to boolean type
- add `isBoolean` / `ensureBoolean` function - add ability for `isLike` to compare objects with boolean types
1 parent 90e9f3f commit 9d6eb16

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

ensure.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
isArray,
3+
isBoolean,
34
isFunction,
45
isLike,
56
isNone,
@@ -50,6 +51,13 @@ export function ensureNumber(x: unknown): asserts x is number {
5051
return ensure(x, isNumber, "The value must be number");
5152
}
5253

54+
/**
55+
* Ensure if `x` is boolean by raising an `EnsureError` when it's not.
56+
*/
57+
export function ensureBoolean(x: unknown): asserts x is boolean {
58+
return ensure(x, isBoolean, "The value must be boolean");
59+
}
60+
5361
/**
5462
* Ensure if `x` is array by raising an `EnsureError` when it's not.
5563
*/

is.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export function isNumber(x: unknown): x is number {
1414
return typeof x === "number";
1515
}
1616

17+
/**
18+
* Return true if the value is boolean
19+
*/
20+
export function isBoolean(x: unknown): x is boolean {
21+
return typeof x === "boolean";
22+
}
23+
1724
/**
1825
* Return true if the value is array
1926
*/
@@ -81,6 +88,9 @@ export function isLike<R, T extends unknown>(
8188
if (isNumber(ref) && isNumber(x)) {
8289
return true;
8390
}
91+
if (isBoolean(ref) && isBoolean(x)) {
92+
return true;
93+
}
8494
if (isArray(ref, pred) && isArray(x, pred)) {
8595
return ref.length === 0 || (
8696
ref.length === x.length &&

0 commit comments

Comments
 (0)