1
+ /**
2
+ * The monad encapsulates the function that might fail.
3
+ * In case of exception occurring, the monad suppresses it and provides
4
+ * the API to retrieve the result value safely.
5
+ * `Try` acts lazily. So, all methods build a pipeline of execution.
6
+ * Methods `orElse`, `orElseGet`, and `orElseThrow` trigger it.
7
+ */
1
8
export class Try < T > {
2
9
private readonly supplier : ( ) => T ;
3
10
4
11
private constructor ( supplier : ( ) => T ) {
5
12
this . supplier = supplier ;
6
13
}
7
14
15
+ /**
16
+ * Creates the new monad from the given function.
17
+ *
18
+ * @param supplier - function that can provide the value or fail
19
+ * @returns the new monad
20
+ */
8
21
public static of < T > ( supplier : ( ) => T ) : Try < T > {
9
22
return new Try < T > ( supplier ) ;
10
23
}
11
24
25
+ /**
26
+ * Creates the new monad that always succeeds.
27
+ *
28
+ * @param value - value that is stored within the monad
29
+ * @returns the new succeeded monad
30
+ */
12
31
public static success < T > ( value : T ) : Try < T > {
13
32
return new Try < T > ( ( ) => value ) ;
14
33
}
15
34
35
+ /**
36
+ * Creates the new monad that always fails.
37
+ *
38
+ * @param exception - the error that is associated with the monad, might absent
39
+ * @see {@link orElseThrow }
40
+ */
16
41
public static error < T > ( exception ?: Error ) : Try < T > {
17
42
return new Try < T > ( ( ) => {
18
43
if ( exception ) {
@@ -22,14 +47,34 @@ export class Try<T> {
22
47
} ) ;
23
48
}
24
49
50
+ /**
51
+ * Maps the monad value to the new one.
52
+ *
53
+ * @param mapper - value mapper
54
+ * @returns the new monad with mapped value
55
+ */
25
56
public map < U > ( mapper : ( value : T ) => U ) : Try < U > {
26
57
return new Try < U > ( ( ) => mapper ( this . supplier ( ) ) ) ;
27
58
}
28
59
60
+ /**
61
+ * Maps the monad value to the new one returned by the other `Try`.
62
+ *
63
+ * @param mapper - mapper that provides the other `Try` monad
64
+ * @returns the new monad with the mapped value
65
+ */
29
66
public flatMap < U > ( mapper : ( value : T ) => Try < U > ) : Try < U > {
30
67
return new Try < U > ( ( ) => mapper ( this . supplier ( ) ) . orElseThrow ( ) ) ;
31
68
}
32
69
70
+ /**
71
+ * Applies the filtering function to the monad value.
72
+ * If the filtering passes, return the current monad.
73
+ * Otherwise returns the failed one.
74
+ *
75
+ * @param predicate - filtering predicate
76
+ * @returns the current monad or the failed one
77
+ */
33
78
public filter ( predicate : ( value : T ) => boolean ) : Try < T > {
34
79
return new Try < T > ( ( ) => {
35
80
const value = this . supplier ( ) ;
@@ -40,6 +85,14 @@ export class Try<T> {
40
85
} ) ;
41
86
}
42
87
88
+ /**
89
+ * If the monad value execution fails,
90
+ * creates a new one from the provided `elseCondition`.
91
+ * If the monad value execution does not fail, simply returns it.
92
+ *
93
+ * @param elseCondition - the source for the new monad in case of failure
94
+ * @returns the current monad or the one built from `elseCondition`
95
+ */
43
96
public orElseTry ( elseCondition : ( ) => T ) : Try < T > {
44
97
return new Try < T > ( ( ) => {
45
98
try {
@@ -50,6 +103,16 @@ export class Try<T> {
50
103
} ) ;
51
104
}
52
105
106
+ /**
107
+ * Calculates the value and returns it.
108
+ * If the calculation fails, returns the default value.
109
+ *
110
+ * @remarks
111
+ * This is a terminal operation.
112
+ *
113
+ * @param value - the default value
114
+ * @returns the calculated value or the default one
115
+ */
53
116
public orElse ( value : T ) : T {
54
117
try {
55
118
return this . supplier ( ) ;
@@ -58,6 +121,17 @@ export class Try<T> {
58
121
}
59
122
}
60
123
124
+ /**
125
+ * Calculates the value and returns it.
126
+ * If the calculation fails, returns the one provided by `elseValueSupplier`.
127
+ *
128
+ * @remarks
129
+ * This is a terminal operation.
130
+ *
131
+ * @param elseValueSupplier - returns the default value
132
+ * and accepts the error that occurred as a parameter
133
+ * @returns the calculated value or the default one
134
+ */
61
135
public orElseGet ( elseValueSupplier : ( error : unknown ) => T ) : T {
62
136
try {
63
137
return this . supplier ( ) ;
@@ -66,6 +140,18 @@ export class Try<T> {
66
140
}
67
141
}
68
142
143
+ /**
144
+ * Calculates the values and returns it.
145
+ * If the calculation fails, throws an exception provided by `errorSupplier`.
146
+ * If `errorSupplier` is `undefined`, just throws the one that occurred.
147
+ *
148
+ * @remarks
149
+ * This is a terminal operation.
150
+ *
151
+ * @param errorSupplier - error supplier
152
+ * @returns the calculated value
153
+ * @throws error in case of calculation issues
154
+ */
69
155
public orElseThrow ( errorSupplier ?: ( ) => Error ) : T {
70
156
try {
71
157
return this . supplier ( ) ;
0 commit comments