-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (66 loc) · 1.7 KB
/
index.js
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
export const None = null;
export function of(value) {
return value ?? None;
}
export function isNone(value) {
return value == None;
}
export function isSome(value) {
return !isNone(value);
}
export function map(optional, callback) {
return callback
? isSome(optional)
? callback(optional)
: optional
: (anotherOptional) => map(anotherOptional, optional);
}
export function or(optional, fallback) {
return arguments.length === 1
? (anotherOptional) => or(anotherOptional, optional)
: optional ?? fallback;
}
export function orElse(optional, fallback) {
return fallback
? optional ?? fallback()
: (anotherOptional) => orElse(anotherOptional, optional);
}
export function apply(optional, optionalWithCallback) {
return arguments.length === 1
? (anotherOptional) => apply(anotherOptional, optional)
: map(zip(optional, optionalWithCallback), ([value, callback]) =>
callback(value),
);
}
export function filter(optional, predicate) {
return predicate
? map(optional, (value) => (predicate(value) ? value : None))
: (anotherOptional) => filter(anotherOptional, optional);
}
export function zip(first, second) {
return arguments.length === 1
? (anotherOptional) => zip(anotherOptional, first)
: map(first, (first) => map(second, (second) => [first, second]));
}
export function unzip(optional) {
return optional ?? [None, None];
}
export function transpose(optional, lift) {
return arguments.length === 1
? (anotherOptional) => transpose(anotherOptional, optional)
: optional ?? lift(None);
}
export default {
of,
or,
map,
zip,
unzip,
apply,
isNone,
isSome,
orElse,
filter,
Default: None,
transpose,
};