-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboolean.ts
103 lines (96 loc) · 1.94 KB
/
boolean.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { object_type } from './object'
/**
* Convert value to boolean
*
* @param value - value
* @returns boolean
*
* @example ```ts
* to_boolean(42) // true
* to_boolean(NaN) // false
* ```
*/
export function to_boolean(value: any): boolean {
return Boolean(value)
}
/**
* Test value is boolean type
*
* @param value - value
* @returns value is boolean type or not
*
* @example```ts
* is_boolean(false) // true
* is_boolean(Boolean()) // true
* is_boolean(null) // false
* ```
*/
export function is_boolean(value: any): value is boolean {
return 'boolean' === typeof value || 'Boolean' === object_type(value)
}
/**
* Test a boolean value is true
*
* @param bool - boolean
* @returns test result
*
* @example```ts
* is_true(true) // true
* is_true(Boolean()) // false
* ```
*/
export function is_true(bool: boolean): bool is true {
return true === bool
}
/**
* Test a boolean value is false
*
* @param bool - boolean
* @returns test result
*
* @example```ts
* is_false(false) // true
* is_false(Boolean()) // true
* is_false(true) // false
* ```
*/
export function is_false(bool: boolean): bool is false {
return false === bool
}
/**
* Falsy type
*
* @todo loss NaN
*/
export type Falsy = 0 | -0 | false | null | undefined | ''
/**
* Test a value is a falsy value, one of `0`, `-0`, `false`, `null`, `undefined`, `NaN`, `""`
*
* @param value value
* @returns test result
*
* @example```ts
* is_falsy(null) // true
* is_falsy([]) // false
* is_falsy({}) // false
* is_falsy(42) // false
* ```
*/
export function is_falsy(value: any): boolean {
return false === to_boolean(value)
}
/**
* Test a value is not a falsy value
*
* @param value value
* @returns test result
*
* @example ```ts
* is_truthy(42) // true
* is_truthy({}) // true
* is_truthy(false) // false
* ```
*/
export function is_truthy(value: any): boolean {
return !is_falsy(value)
}