mini-func.js
2.26 KB
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
'use strict';
var AmeMiniFunc;
(function (AmeMiniFunc) {
class Some {
constructor(value) {
this.value = value;
}
get() {
return this.value;
}
isDefined() {
return true;
}
isEmpty() {
return false;
}
nonEmpty() {
return true;
}
map(f) {
return new Some(f(this.value));
}
flatMap(f) {
return f(this.value);
}
filter(f) {
return f(this.value) ? this : AmeMiniFunc.none;
}
forEach(f) {
f(this.value);
}
orElse(alternative) {
return this;
}
getOrElse(alternative) {
return this.value;
}
toArray() {
return [this.value];
}
orNull() {
return this.value;
}
}
class None {
map(f) {
return this;
}
get() {
throw new Error('Cannot get value from None');
}
isDefined() {
return false;
}
isEmpty() {
return true;
}
nonEmpty() {
return false;
}
filter(f) {
return this;
}
forEach(f) {
}
orElse(alternative) {
return alternative();
}
getOrElse(alternative) {
return alternative();
}
orNull() {
return null;
}
flatMap(f) {
return this;
}
toArray() {
return [];
}
}
AmeMiniFunc.none = new None();
function some(value) {
return new Some(value);
}
AmeMiniFunc.some = some;
function lift(options, f) {
const areAllDefined = options.every((opt) => opt.isDefined());
if (areAllDefined) {
const unwrappedValues = options.map((opt) => opt.get());
return some(f(...unwrappedValues));
}
else {
return AmeMiniFunc.none;
}
}
AmeMiniFunc.lift = lift;
})(AmeMiniFunc || (AmeMiniFunc = {}));
//# sourceMappingURL=mini-func.js.map