Skip to content

Commit 5b1dc3d

Browse files
committed
chore(release): 2.0.0
feat: add fromTry, fromPromise, unwrapOr, unwrapOrElse, make initial and pending constants
1 parent 4e72037 commit 5b1dc3d

File tree

7 files changed

+262
-112
lines changed

7 files changed

+262
-112
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4+
5+
## 2.0.0 (2023-04-03)
6+
7+
8+
### Features
9+
10+
* add changelog ([c4038a1](https://github.com/mokkapps/changelog-generator-demo/commits/c4038a18640d5a59bc85056f5f91f340150c93e3))
11+
* update Nx, add conventional naming ([edd8cc7](https://github.com/mokkapps/changelog-generator-demo/commits/edd8cc7f00e1565b1197e7fe3a95053556025212))
12+
13+
14+
### Bug Fixes
15+
16+
* next generator used for CHANGELOG ([4e72037](https://github.com/mokkapps/changelog-generator-demo/commits/4e720370d1168677713cb4db2b6c7172c6dfeee7))
17+
118
# Changelog
219

320
All notable changes to this project will be documented in this file.

README.md

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const user = getUser(1).map(({ email }) => email);
4646
- [`failure`](#failure)
4747
- [`success`](#success)
4848
- [`from`](#from)
49+
- [`fromPromise`](#fromPromise)
50+
- [`fromTry`](#fromTry)
51+
- [`from`](#from)
4952
- [`fromMaybe`](#frommaybe)
5053
- [`fromEither`](#fromeither)
5154
- [`isResult`](#isresult)
@@ -109,16 +112,16 @@ function merge<F1, S1, F2, S2, F3, S3>(
109112
Example:
110113

111114
```typescript
112-
const v1 = initial<TypeError, number>(); // Result<TypeError, number>.Initial
113-
const v2 = pending<TypeError, number>(); // Result<TypeError, number>.Pending
115+
const v1 = initial; // Result<never, never>.Initial
116+
const v2 = pending; // Result<never, never>.Pending
114117
const v3 = success<TypeError, number>(2); // Result<TypeError, number>.Success
115118
const v4 = success<ReferenceError, string>("test"); // Result<ReferenceError, string>.Success
116119
const v5 = failure<Error, boolean>(new Error()); // Result<Error, boolean>.Failure
117120

118-
const r1 = merge([v1, v2]); // Result<TypeError, [number, number]>.Initial
119-
const r2 = merge([v2, v5]); // Result<TypeError | Error, [number, boolean]>.Pending
121+
const r1 = merge([v1, v2]); // Result<never, [number, number]>.Initial
122+
const r2 = merge([v2, v5]); // Result<Error, [never, boolean]>.Pending
120123
const r3 = merge([v3, v4]); // Result<TypeError | ReferenceError, [number, string]>.Success
121-
const r4 = merge([v3, v4, v5]); // Result<TypeError | ReferenceError | Error, [number, string, boolean]>.Failure
124+
const r4 = merge([v3, v4, v5]); // Result<TypeError | Error | ReferenceError, [number, string, boolean]>.Failure
122125
```
123126

124127
#### `mergeInOne`
@@ -138,8 +141,8 @@ function merge<F1, S1, F2, S2, F3, S3>(
138141
Example:
139142

140143
```typescript
141-
const v1 = initial<TypeError, number>(); // Result<TypeError, number>.Initial
142-
const v2 = pending<TypeError, number>(); // Result<TypeError, number>.Pending
144+
const v1 = initial; // Result<TypeError, number>.Initial
145+
const v2 = pending; // Result<TypeError, number>.Pending
143146
const v3 = success<TypeError, number>(2); // Result<TypeError, number>.Success
144147
const v4 = success<ReferenceError, string>("test"); // Result<ReferenceError, string>.Success
145148
const v5 = failure<Error, boolean>(new Error()); // Result<Error, boolean>.Failure
@@ -178,31 +181,29 @@ merge([v1, v2, v3]); // Result<Array<TypeError | ReferenceError | Error>, [numbe
178181
#### `initial`
179182

180183
```typescript
181-
function initial<F, S>(): Result<F, S>;
184+
const initial: Result<never, never>;
182185
```
183186

184187
- Returns `Result` with `Initial` state which does not contain value.
185188

186189
Example:
187190

188191
```typescript
189-
const v1 = initial(); // Result<undefined, never>.Initial
190-
const v2 = initial<Error, number>(); // Result<Error, number>.Initial
192+
const v1 = initial; // Result<undefined, never>.Initial
191193
```
192194

193195
#### `pending`
194196

195197
```typescript
196-
function pending<F, S>(): Result<F, S>;
198+
const pending: Result<F, S>;
197199
```
198200

199201
- Returns `Result` with `Pending` state which does not contain value.
200202

201203
Example:
202204

203205
```typescript
204-
const v1 = pending(); // Result<undefined, never>.Initial
205-
const v2 = pending<Error, number>(); // Result<Error, number>.Initial
206+
const v1 = pending; // Result<never, never>.Initial
206207
```
207208

208209
#### `failure`
@@ -251,6 +252,35 @@ Example:
251252
from(2); // Result<never, number>.Success
252253
```
253254

255+
#### `fromTry`
256+
257+
Returns `Success` with function result or `Failure` if function execution throws an error.
258+
259+
```typescript
260+
function fromTry<L, R>(fn: () => R): Result<L, R>;
261+
```
262+
263+
```typescript
264+
fromTry(() => 2); // Result<unknown, number>.Success
265+
fromTry(() => {
266+
throw new Error("test");
267+
}); // Result<unknown, never>.Failure
268+
```
269+
270+
271+
#### `fromPromise`
272+
273+
Returns promise of `Success` if the provided promise fulfilled or `Failure` with the error value if the provided promise rejected.
274+
275+
```typescript
276+
function fromPromise<R>(promise: Promise<R>): Promise<Result<unknown, R>>;
277+
```
278+
279+
```typescript
280+
fromPromise(Promise.resolve(2)); // Promise<Result<unknown, number>.Right>
281+
fromPromise(Promise.reject(new Error("test"))); // Promise<Result<unknown, never>.Left>
282+
```
283+
254284
#### `fromMaybe`
255285

256286
```typescript
@@ -571,7 +601,7 @@ Example:
571601
```typescript
572602
const v1 = success<Error, number>(2);
573603
const v2 = failure<Error, number>(new Error());
574-
const v3 = initial<Error, number>();
604+
const v3 = initial;
575605

576606
// Result<Error | TypeError, string>.Success with value "2"
577607
const newVal1 = v1.chain(a => success<TypeError, string>(a.toString()));
@@ -581,7 +611,7 @@ const newVal2 = v1.chain(a => failure<TypeError, string>(new TypeError()));
581611
const newVal3 = v2.chain(a => success<TypeError, string>(a.toString()));
582612
// Result<Error | TypeError, string>.Failure with value new Error()
583613
const newVal4 = v2.chain(a => failure<TypeError, string>(new TypeError()));
584-
// Result<Error | TypeError, string>.Initial with no value
614+
// Result<TypeError, string>.Initial with no value
585615
const newVal5 = v3.chain(a => failure<TypeError, string>(new TypeError()));
586616
```
587617

@@ -598,7 +628,7 @@ Example:
598628
```typescript
599629
const v1 = success<Error, number>(2);
600630
const v2 = failure<Error, number>(new Error());
601-
const v3 = initial<Error, number>();
631+
const v3 = initial;
602632

603633
// Promise<Result<Error | TypeError, string>.Success> with value "2"
604634
const newVal1 = v1.asyncChain(a => Promise.resolve(right<TypeError, string>(a.toString())));
@@ -683,8 +713,8 @@ Example:
683713
684714
```typescript
685715
success<string, number>(10).unwrap(); // number
686-
initial<Error, number>().unwrap(); // throws default (Error)
687-
pending<Error, number>().unwrap({ failure: () => new Error('Custom')}); // throws custom (Error)
716+
initial.unwrap(); // throws default (Error)
717+
pending.unwrap({ failure: () => new Error('Custom')}); // throws custom (Error)
688718
```
689719
690720
#### `Result#fold`
@@ -725,6 +755,13 @@ const { value } = failure(new Error()); // Error | undefined
725755
success(2).unwrap() // number
726756
failure(new TypeError()).unwrap() // throws
727757
failure(2).unwrap() // throws (don't do this)
758+
759+
failure(2).unwrapOr(3) // returns 3
760+
success(2).unwrapOr(3) // returns 2
761+
762+
failure(2).unwrapOrElse(num => num * 2) // returns 4
763+
success(2).unwrapOrElse(num => num * 2) // returns 2
764+
728765
```
729766
730767
## Development

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
},
2929
"private": false,
3030
"dependencies": {
31-
"@sweet-monads/either": "3.1.0",
32-
"@sweet-monads/interfaces": "3.1.0",
33-
"@sweet-monads/maybe": "3.1.0",
31+
"@sweet-monads/either": "3.2.0",
32+
"@sweet-monads/interfaces": "3.2.0",
33+
"@sweet-monads/maybe": "3.2.0",
3434
"tslib": "2.3.0"
3535
},
3636
"devDependencies": {

packages/ts-result/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lonli-lokli/ts-result",
3-
"version": "1.3.1",
3+
"version": "2.0.0",
44
"private": false,
55
"sideEffects": false,
66
"main": "./cjs/index.js",
@@ -23,8 +23,8 @@
2323
"Monad"
2424
],
2525
"dependencies": {
26-
"@sweet-monads/either": "^3.1.0",
27-
"@sweet-monads/maybe": "^3.1.0"
26+
"@sweet-monads/either": "^3.2.0",
27+
"@sweet-monads/maybe": "^3.2.0"
2828
},
2929
"author": "Lonli-Lokli",
3030
"license": "MIT"

0 commit comments

Comments
 (0)