-
Notifications
You must be signed in to change notification settings - Fork 6
/
boolean.ts
239 lines (221 loc) · 5.14 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* The boolean module contains combinators for working with boolean.
*
* @module number
* @since 2.0.0
*/
import type { Ordering, Sortable } from "./sortable.ts";
import type { Combinable } from "./combinable.ts";
import type { Initializable } from "./initializable.ts";
import type { Comparable } from "./comparable.ts";
import type { Showable } from "./showable.ts";
import { fromSort } from "./sortable.ts";
/**
* A function that always returns true.
*
* @example
* ```ts
* import { constTrue } from "./boolean.ts";
*
* const result = constTrue(); // true
* ```
*
* @since 2.0.0
*/
export const constTrue = () => true;
/**
* A function that always returns false.
*
* @example
* ```ts
* import { constFalse } from "./boolean.ts";
*
* const result = constFalse(); // false
* ```
*
* @since 2.0.0
*/
export const constFalse = () => false;
/**
* A type guard, indicating that a value is a boolean.
*
* @since 2.0.0
*/
export function isBoolean(a: unknown): a is boolean {
return typeof a === "boolean";
}
/**
* Create a match function over boolean
*
* @example
* ```ts
* import { match } from "./boolean.ts";
* import * as O from "./option.ts";
*
* const match1 = match(() => "Tina", () => "Turner");
* const match2 = match(() => O.some("Hi"), () => O.none);
*
* const result1 = match1(true); // "Tina"
* const result2 = match1(false); // "Turner"
* const result3 = match2(true); // Some("Hi")
* const result4 = match2(false); // None
* ```
*
* @since 2.0.0
*/
export function match<A, B>(
onTrue: () => A,
onFalse: () => B,
): (a: boolean) => A | B {
return (a) => a ? onTrue() : onFalse();
}
/**
* Negate a given boolean.
*
* @example
* ```ts
* import { not } from "./boolean.ts";
*
* const result1 = not(true); // false
* const result2 = not(false); // true
* ```
*
* @since 2.0.0
*/
export function not(ua: boolean): boolean {
return !ua;
}
/**
* Test the equality of two booleans.
*
* @example
* ```ts
* import { compare } from "./boolean.ts";
*
* const result1 = compare(true)(false); // false
* const result2 = compare(true)(true); // true
* ```
*
* @since 2.0.0
*/
export function compare(second: boolean): (first: boolean) => boolean {
return (first) => first === second;
}
/**
* Compares two booleans, returning an Ordering. True is greater than
* False in this ordering, generally, but the specifics are always
* decided by the runtime.
*
* @example
* ```ts
* import { sort } from "./boolean.ts";
*
* const result1 = sort(true, true); // 0
* const result2 = sort(true, false); // 1
* const result3 = sort(false, true); // -1
* ```
*
* @since 2.0.0
*/
export function sort(first: boolean, second: boolean): Ordering {
return first < second ? -1 : second < first ? 1 : 0;
}
/**
* A curried form of logical Or.
*
* @example
* ```ts
* import { or } from "./boolean.ts";
*
* const result1 = or(true)(true); // true
* const result2 = or(true)(false); // true
* const result3 = or(false)(false); // false
* ```
*
* @since 2.0.0
*/
export function or(second: boolean): (first: boolean) => boolean {
return (first) => first || second;
}
/**
* A curried form of logical And.
*
* @example
* ```ts
* import { and } from "./boolean.ts";
*
* const result1 = and(true)(true); // true
* const result2 = and(true)(false); // false
* const result3 = and(false)(false); // false
* ```
*
* @since 2.0.0
*/
export function and(second: boolean): (first: boolean) => boolean {
return (first) => first && second;
}
/**
* The canonical implementation of Sortable for boolean. It contains
* the method lt, lte, equals, gte, gt, min, max, clamp, between,
* and compare.
*
* @since 2.0.0
*/
export const SortableBoolean: Sortable<boolean> = fromSort(sort);
/**
* The canonical implementation of Comparable for boolean. It contains
* the method equals.
*
* @since 2.0.0
*/
export const ComparableBoolean: Comparable<boolean> = { compare };
/**
* The canonical implementation of Combinable for boolean that
* combines using the logical and operator. It contains the
* method combine.
*
* @since 2.0.0
*/
export const CombinableBooleanAll: Combinable<boolean> = {
combine: and,
};
/**
* The canonical implementation of Combinable for boolean that
* combines using the logical or operator. It contains the
* method combine.
*
* @since 2.0.0
*/
export const CombinableBooleanAny: Combinable<boolean> = {
combine: or,
};
/**
* The canonical implementation of Initializable for boolean that
* combines using the logical and operator. It contains the
* method combine.
*
* @since 2.0.0
*/
export const InitializableBooleanAll: Initializable<boolean> = {
combine: and,
init: constTrue,
};
/**
* The canonical implementation of Initializable for boolean that
* combines using the logical or operator. It contains the
* method combine.
*
* @since 2.0.0
*/
export const InitializableBooleanAny: Initializable<boolean> = {
combine: or,
init: constFalse,
};
/**
* The canoncial implementation of Showable for boolean. It
* uses JSON.stringify to turn a boolean into a string.
* It contains the method show.
*/
export const ShowableBoolean: Showable<boolean> = {
show: JSON.stringify,
};