Skip to content

Commit b6c7968

Browse files
Merge pull request #21 from SimonHarmonicMinor/docs/#20-try-ts-docs
Docs/#20 try ts docs
2 parents 91c340e + b3b02e4 commit b6c7968

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

.eslintrc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ parserOptions:
1313
plugins:
1414
- '@typescript-eslint'
1515
- unicorn
16+
- 'eslint-plugin-tsdoc'
1617
rules:
1718
prettier/prettier:
1819
- "error"
@@ -111,4 +112,5 @@ rules:
111112
"unicorn/require-number-to-fixed-digits-argument": "error"
112113
"unicorn/require-post-message-target-origin": "error"
113114
"unicorn/string-content": "off"
114-
"unicorn/throw-new-error": "error"
115+
"unicorn/throw-new-error": "error"
116+
tsdoc/syntax: error

package-lock.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"eslint": "^7.30.0",
4141
"eslint-config-prettier": "^8.3.0",
4242
"eslint-plugin-prettier": "^3.4.0",
43+
"eslint-plugin-tsdoc": "^0.2.14",
4344
"eslint-plugin-unicorn": "^34.0.1",
4445
"husky": "^7.0.1",
4546
"jest": "^27.0.6",

src/try.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
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+
*/
18
export class Try<T> {
29
private readonly supplier: () => T;
310

411
private constructor(supplier: () => T) {
512
this.supplier = supplier;
613
}
714

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+
*/
821
public static of<T>(supplier: () => T): Try<T> {
922
return new Try<T>(supplier);
1023
}
1124

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+
*/
1231
public static success<T>(value: T): Try<T> {
1332
return new Try<T>(() => value);
1433
}
1534

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+
*/
1641
public static error<T>(exception?: Error): Try<T> {
1742
return new Try<T>(() => {
1843
if (exception) {
@@ -22,14 +47,34 @@ export class Try<T> {
2247
});
2348
}
2449

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+
*/
2556
public map<U>(mapper: (value: T) => U): Try<U> {
2657
return new Try<U>(() => mapper(this.supplier()));
2758
}
2859

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+
*/
2966
public flatMap<U>(mapper: (value: T) => Try<U>): Try<U> {
3067
return new Try<U>(() => mapper(this.supplier()).orElseThrow());
3168
}
3269

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+
*/
3378
public filter(predicate: (value: T) => boolean): Try<T> {
3479
return new Try<T>(() => {
3580
const value = this.supplier();
@@ -40,6 +85,14 @@ export class Try<T> {
4085
});
4186
}
4287

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+
*/
4396
public orElseTry(elseCondition: () => T): Try<T> {
4497
return new Try<T>(() => {
4598
try {
@@ -50,6 +103,16 @@ export class Try<T> {
50103
});
51104
}
52105

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+
*/
53116
public orElse(value: T): T {
54117
try {
55118
return this.supplier();
@@ -58,6 +121,17 @@ export class Try<T> {
58121
}
59122
}
60123

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+
*/
61135
public orElseGet(elseValueSupplier: (error: unknown) => T): T {
62136
try {
63137
return this.supplier();
@@ -66,6 +140,18 @@ export class Try<T> {
66140
}
67141
}
68142

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+
*/
69155
public orElseThrow(errorSupplier?: () => Error): T {
70156
try {
71157
return this.supplier();

0 commit comments

Comments
 (0)