-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
63 lines (56 loc) · 1.63 KB
/
index.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
import { None as NewNone, OptionUnion } from "./base.js";
import { OptionPipe } from "./middleware/middleware.js";
import { isSome } from "./api/is_some.js";
import { isNone } from "./api/is_none.js";
import { unwrap } from "./api/unwrap.js";
import { match } from "./api/match.js";
import { eq } from "./api/eq.js";
export class NewOption<T> {
*[Symbol.iterator]() {
if (isSome(this.inner)) {
yield this.inner.value;
}
}
constructor(public inner: OptionUnion<T>) {}
static None = <T>() => new NewOption<T>(NewNone as OptionUnion<T>);
static Some<T>(value: T) {
return new NewOption({ type: "Some", value });
}
static fromBool(predicate: boolean) {
return predicate ? NewOption.Some(predicate) : NewOption.None();
}
static fromNullable<T>(v?: T | null) {
if (v !== undefined && v !== null) {
return NewOption.Some(v);
}
return NewOption.None();
}
isSome() {
return isSome(this.inner);
}
isNone() {
return isNone(this.inner);
}
unwrap() {
return unwrap(this.inner);
}
clone() {
return isSome(this.inner)
? NewOption.Some(this.inner.value)
: NewOption.None();
}
do<Out>(fn: OptionPipe<T, Out>) {
return fn(this, this.inner);
}
pipe<Args extends unknown[], Out>(fn: (...args: Args) => OptionPipe<T, Out>) {
return (...args: Args) => fn(...args)(this, this.inner);
}
eq<U>(other: NewOption<T>, by = (x: T) => x as unknown as U) {
return this.do(eq(other, by));
}
match<U>(onSome: (v: T) => U, onNone: () => U) {
return this.do(match(onSome, onNone));
}
}
export const Some = NewOption.Some;
export const None = NewOption.None;