@madkarma/result 2.0.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -8
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/option.d.ts +148 -0
- package/dist/option.d.ts.map +1 -0
- package/dist/option.js +160 -0
- package/dist/option.js.map +1 -0
- package/dist/result.d.ts +29 -18
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +42 -26
- package/dist/result.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# @madkarma/result
|
|
2
2
|
|
|
3
|
-
Rust's Result
|
|
3
|
+
Rust's `Result` and `Option` types, for TypeScript.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This library mimics the Rust `Result` and `Option` enums and includes some of their chainable methods adapted for TypeScript, with full type safety. For full documentation on the usage of the available methods, please refer to the official Rust docs (or read the JSDocs directly in your editor).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
> Inspired by [this video](https://www.youtube.com/watch?v=ovnyeq-Xxrc) by **Web Dev Simplified** and [vultix/ts-results](https://github.com/vultix/ts-results).
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
@@ -20,6 +20,10 @@ yarn add @madkarma/result
|
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
|
+
### Result
|
|
24
|
+
|
|
25
|
+
[Official Rust Result documentation](https://doc.rust-lang.org/std/result/enum.Result.html)
|
|
26
|
+
|
|
23
27
|
```typescript
|
|
24
28
|
import { Ok, Err } from '@madkarma/result';
|
|
25
29
|
|
|
@@ -39,14 +43,12 @@ const parseUserId = (id: string) => {
|
|
|
39
43
|
|
|
40
44
|
const fetchUser = (id: number) => {
|
|
41
45
|
if (id === 13) return Err({ code: 'NOT_FOUND', message: 'User not found' });
|
|
42
|
-
|
|
43
46
|
return Ok({ id, name: 'Alice', role: 'admin' });
|
|
44
47
|
};
|
|
45
48
|
|
|
46
|
-
// Processing an input by chaining methods
|
|
47
49
|
const { value: user, error } = parseUserId('10')
|
|
48
|
-
.map((id) => id + 3)
|
|
49
|
-
.andThen(fetchUser);
|
|
50
|
+
.map((id) => id + 3)
|
|
51
|
+
.andThen(fetchUser);
|
|
50
52
|
|
|
51
53
|
if (error) {
|
|
52
54
|
switch (error.code) {
|
|
@@ -63,9 +65,30 @@ if (error) {
|
|
|
63
65
|
}
|
|
64
66
|
```
|
|
65
67
|
|
|
68
|
+
### Option
|
|
69
|
+
|
|
70
|
+
[Official Rust Option documentation](https://doc.rust-lang.org/std/option/enum.Option.html)
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { Some, None } from '@madkarma/result';
|
|
74
|
+
|
|
75
|
+
const parseNickname = (nickname?: string) => {
|
|
76
|
+
if (!nickname) return None();
|
|
77
|
+
|
|
78
|
+
const trimmed = nickname.trim();
|
|
79
|
+
return trimmed.length > 0 ? Some(trimmed) : None();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const displayName = parseNickname(' Ada ')
|
|
83
|
+
.map((name) => name.toUpperCase())
|
|
84
|
+
.unwrapOr('ANONYMOUS');
|
|
85
|
+
|
|
86
|
+
console.log(displayName);
|
|
87
|
+
```
|
|
88
|
+
|
|
66
89
|
## How this differs from Rust
|
|
67
90
|
|
|
68
|
-
Instead of Rust's `match` expressions, check `{ value, error }` directly and use a `switch` on `error.code` to emulate pattern matching.
|
|
91
|
+
Instead of Rust's `match` expressions, check `Result` values as `{ value, error }` directly and use a `switch` on `error.code` to emulate pattern matching. `Option` values are handled directly with `isSome()` / `isNone()` checks.
|
|
69
92
|
|
|
70
93
|
## Contributing
|
|
71
94
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
|
package/dist/option.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { type Result, type ResultError } from './result';
|
|
2
|
+
/**
|
|
3
|
+
* Represents some value of type `T`.
|
|
4
|
+
*/
|
|
5
|
+
export type SomeOption<T> = {
|
|
6
|
+
readonly value: T;
|
|
7
|
+
} & OptionMethods<T>;
|
|
8
|
+
/**
|
|
9
|
+
* Represents the absence of a value of type `T`.
|
|
10
|
+
*/
|
|
11
|
+
export type NoneOption<T> = {
|
|
12
|
+
readonly value: null;
|
|
13
|
+
} & OptionMethods<T>;
|
|
14
|
+
export type Option<T> = SomeOption<T> | NoneOption<T>;
|
|
15
|
+
interface OptionMethods<T> {
|
|
16
|
+
/**
|
|
17
|
+
* Returns `true` if the option is a `Some` value.
|
|
18
|
+
*/
|
|
19
|
+
isSome(): this is SomeOption<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Returns `true` if the option is a `Some` and the value inside of it matches a predicate.
|
|
22
|
+
*/
|
|
23
|
+
isSomeAnd(f: (val: T) => boolean): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Returns `true` if the option is a `None` value.
|
|
26
|
+
*/
|
|
27
|
+
isNone(): this is NoneOption<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Returns `true` if the option is a `None` or the value inside of it matches a predicate.
|
|
30
|
+
*/
|
|
31
|
+
isNoneOr(f: (val: T) => boolean): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the contained `Some` value.
|
|
34
|
+
*
|
|
35
|
+
* @throws Error if the value is a `None` with a custom error message provided by `msg`.
|
|
36
|
+
*/
|
|
37
|
+
expect(msg: string): T;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the contained `Some` value.
|
|
40
|
+
*
|
|
41
|
+
* @throws Error if the value is a `None`.
|
|
42
|
+
*/
|
|
43
|
+
unwrap(): T;
|
|
44
|
+
/**
|
|
45
|
+
* Returns the contained `Some` value or a provided default.
|
|
46
|
+
*/
|
|
47
|
+
unwrapOr(defaultVal: T): T;
|
|
48
|
+
/**
|
|
49
|
+
* Returns the contained `Some` value or computes it from a closure.
|
|
50
|
+
*/
|
|
51
|
+
unwrapOrElse(f: () => T): T;
|
|
52
|
+
/**
|
|
53
|
+
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained value.
|
|
54
|
+
*/
|
|
55
|
+
map<U>(f: (val: T) => U): Option<U>;
|
|
56
|
+
/**
|
|
57
|
+
* Calls the provided closure with a reference to the contained value (if `Some`).
|
|
58
|
+
*/
|
|
59
|
+
inspect(f: (val: T) => void): Option<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Returns the provided default result (if none), or applies a function to the contained value (if any).
|
|
62
|
+
*/
|
|
63
|
+
mapOr<U>(defaultVal: U, f: (val: T) => U): U;
|
|
64
|
+
/**
|
|
65
|
+
* Computes a default function result (if none), or applies a different function to the contained value (if any).
|
|
66
|
+
*/
|
|
67
|
+
mapOrElse<U>(defaultF: () => U, f: (val: T) => U): U;
|
|
68
|
+
/**
|
|
69
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err)`.
|
|
70
|
+
*/
|
|
71
|
+
okOr<E extends ResultError>(err: E): Result<T, E>;
|
|
72
|
+
/**
|
|
73
|
+
* Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
|
|
74
|
+
*/
|
|
75
|
+
okOrElse<E extends ResultError>(errF: () => E): Result<T, E>;
|
|
76
|
+
/**
|
|
77
|
+
* Returns an iterator over the possibly contained value.
|
|
78
|
+
*/
|
|
79
|
+
iter(): IterableIterator<T>;
|
|
80
|
+
/**
|
|
81
|
+
* Returns `None` if the option is `None`, otherwise returns `optb`.
|
|
82
|
+
*/
|
|
83
|
+
and<U>(optb: Option<U>): Option<U>;
|
|
84
|
+
/**
|
|
85
|
+
* Returns `None` if the option is `None`, otherwise calls `f` with the wrapped value and returns the result.
|
|
86
|
+
*/
|
|
87
|
+
andThen<U>(f: (val: T) => Option<U>): Option<U>;
|
|
88
|
+
/**
|
|
89
|
+
* Returns `None` if the option is `None`, otherwise calls `predicate` with the wrapped value and returns:
|
|
90
|
+
* - `Some(t)` if `predicate` returns `true` (where `t` is the wrapped value), and
|
|
91
|
+
* - `None` if `predicate` returns `false`.
|
|
92
|
+
*/
|
|
93
|
+
filter(predicate: (val: T) => boolean): Option<T>;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the option if it contains a value, otherwise returns `optb`.
|
|
96
|
+
*/
|
|
97
|
+
or<T2>(optb: Option<T2>): Option<T | T2>;
|
|
98
|
+
/**
|
|
99
|
+
* Returns the option if it contains a value, otherwise calls `f` and returns the result.
|
|
100
|
+
*/
|
|
101
|
+
orElse<T2>(f: () => Option<T2>): Option<T | T2>;
|
|
102
|
+
/**
|
|
103
|
+
* Returns `Some` if exactly one of `this`, `optb` is `Some`, otherwise returns `None`.
|
|
104
|
+
*/
|
|
105
|
+
xor<T2>(optb: Option<T2>): Option<T | T2>;
|
|
106
|
+
/**
|
|
107
|
+
* Inserts `value` into the option, then returns a reference to it.
|
|
108
|
+
*/
|
|
109
|
+
insert(value: T): T;
|
|
110
|
+
/**
|
|
111
|
+
* Inserts `value` into the option if it is `None`, then returns a reference to the contained value.
|
|
112
|
+
*/
|
|
113
|
+
getOrInsert(value: T): T;
|
|
114
|
+
/**
|
|
115
|
+
* Inserts a value computed from `f` into the option if it is `None`, then returns a reference to the contained value.
|
|
116
|
+
*/
|
|
117
|
+
getOrInsertWith(f: () => T): T;
|
|
118
|
+
/**
|
|
119
|
+
* Takes the value out of the option, leaving a `None` in its place.
|
|
120
|
+
*/
|
|
121
|
+
take(): Option<T>;
|
|
122
|
+
/**
|
|
123
|
+
* Takes the value out of the option, but only if the predicate evaluates to `true` on the value.
|
|
124
|
+
*/
|
|
125
|
+
takeIf(predicate: (val: T) => boolean): Option<T>;
|
|
126
|
+
/**
|
|
127
|
+
* Replaces the actual value in the option by the value given in parameter, returning the old value if present,
|
|
128
|
+
* leaving a `Some` in its place.
|
|
129
|
+
*/
|
|
130
|
+
replace(value: T): Option<T>;
|
|
131
|
+
/**
|
|
132
|
+
* Converts from `Option<Option<T>>` to `Option<T>`.
|
|
133
|
+
*/
|
|
134
|
+
flatten<U>(this: Option<Option<U>>): Option<U>;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Some value of type `T`.
|
|
138
|
+
* @param value The value to be wrapped in a `Some`.
|
|
139
|
+
* @returns An `Option` representing the presence of a value.
|
|
140
|
+
*/
|
|
141
|
+
export declare const Some: <T>(value: T) => Option<T>;
|
|
142
|
+
/**
|
|
143
|
+
* No value.
|
|
144
|
+
* @returns An `Option` representing the absence of a value.
|
|
145
|
+
*/
|
|
146
|
+
export declare const None: <T = never>() => Option<T>;
|
|
147
|
+
export {};
|
|
148
|
+
//# sourceMappingURL=option.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"option.d.ts","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,WAAW,EAAW,MAAM,UAAU,CAAC;AAElE;;GAEG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;IACxB,QA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,EAAE,MAAM,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;IAE1C;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAEpB;;OAEG;IACH,WAAW,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAEzB;;OAEG;IACH,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAE/B;;OAEG;IACH,IAAI,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC;IAElB;;OAEG;IACH,MAAM,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAElD;;;OAGG;IACH,OAAO,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAE7B;;OAEG;IACH,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;CAClD;AAiKD;;;;GAIG;AACH,eAAO,MAAM,IAAI,GAAI,CAAC,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,CAAC,CAE1C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,IAAI,GAAI,CAAC,GAAG,KAAK,OAAK,MAAM,CAAC,CAAC,CAE1C,CAAC"}
|
package/dist/option.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Ok, Err } from './result';
|
|
2
|
+
class OptionImpl {
|
|
3
|
+
#value;
|
|
4
|
+
constructor(value) {
|
|
5
|
+
this.#value = value;
|
|
6
|
+
}
|
|
7
|
+
get value() {
|
|
8
|
+
return this.#value;
|
|
9
|
+
}
|
|
10
|
+
isSome() {
|
|
11
|
+
return this.value !== null;
|
|
12
|
+
}
|
|
13
|
+
isSomeAnd(f) {
|
|
14
|
+
return this.isSome() && f(this.value);
|
|
15
|
+
}
|
|
16
|
+
isNone() {
|
|
17
|
+
return this.value === null;
|
|
18
|
+
}
|
|
19
|
+
isNoneOr(f) {
|
|
20
|
+
return this.value === null || f(this.value);
|
|
21
|
+
}
|
|
22
|
+
expect(msg) {
|
|
23
|
+
if (this.isNone())
|
|
24
|
+
throw new Error(msg);
|
|
25
|
+
return this.value;
|
|
26
|
+
}
|
|
27
|
+
unwrap() {
|
|
28
|
+
if (this.isNone())
|
|
29
|
+
throw new Error('called `Option.unwrap()` on a `None` value');
|
|
30
|
+
return this.value;
|
|
31
|
+
}
|
|
32
|
+
unwrapOr(defaultVal) {
|
|
33
|
+
return this.isSome() ? this.value : defaultVal;
|
|
34
|
+
}
|
|
35
|
+
unwrapOrElse(f) {
|
|
36
|
+
return this.isSome() ? this.value : f();
|
|
37
|
+
}
|
|
38
|
+
map(f) {
|
|
39
|
+
if (this.isSome())
|
|
40
|
+
return Some(f(this.value));
|
|
41
|
+
return None();
|
|
42
|
+
}
|
|
43
|
+
inspect(f) {
|
|
44
|
+
if (this.isSome())
|
|
45
|
+
f(this.value);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
mapOr(defaultVal, f) {
|
|
49
|
+
return this.isSome() ? f(this.value) : defaultVal;
|
|
50
|
+
}
|
|
51
|
+
mapOrElse(defaultF, f) {
|
|
52
|
+
return this.isSome() ? f(this.value) : defaultF();
|
|
53
|
+
}
|
|
54
|
+
okOr(err) {
|
|
55
|
+
if (this.isSome())
|
|
56
|
+
return Ok(this.value);
|
|
57
|
+
return Err(err);
|
|
58
|
+
}
|
|
59
|
+
okOrElse(errF) {
|
|
60
|
+
if (this.isSome())
|
|
61
|
+
return Ok(this.value);
|
|
62
|
+
return Err(errF());
|
|
63
|
+
}
|
|
64
|
+
*iter() {
|
|
65
|
+
if (this.isSome())
|
|
66
|
+
yield this.value;
|
|
67
|
+
}
|
|
68
|
+
and(optb) {
|
|
69
|
+
if (this.isSome())
|
|
70
|
+
return optb;
|
|
71
|
+
return None();
|
|
72
|
+
}
|
|
73
|
+
andThen(f) {
|
|
74
|
+
if (this.isSome())
|
|
75
|
+
return f(this.value);
|
|
76
|
+
return None();
|
|
77
|
+
}
|
|
78
|
+
filter(predicate) {
|
|
79
|
+
if (this.isSome() && predicate(this.value))
|
|
80
|
+
return this;
|
|
81
|
+
return None();
|
|
82
|
+
}
|
|
83
|
+
or(optb) {
|
|
84
|
+
if (this.isSome())
|
|
85
|
+
return this;
|
|
86
|
+
return optb;
|
|
87
|
+
}
|
|
88
|
+
orElse(f) {
|
|
89
|
+
if (this.isSome())
|
|
90
|
+
return this;
|
|
91
|
+
return f();
|
|
92
|
+
}
|
|
93
|
+
xor(optb) {
|
|
94
|
+
const thisIsSome = this.isSome();
|
|
95
|
+
const optbIsSome = optb.isSome();
|
|
96
|
+
if (thisIsSome && !optbIsSome)
|
|
97
|
+
return this;
|
|
98
|
+
if (!thisIsSome && optbIsSome)
|
|
99
|
+
return optb;
|
|
100
|
+
return None();
|
|
101
|
+
}
|
|
102
|
+
insert(value) {
|
|
103
|
+
this.#value = value;
|
|
104
|
+
return this.#value;
|
|
105
|
+
}
|
|
106
|
+
getOrInsert(value) {
|
|
107
|
+
if (this.isNone())
|
|
108
|
+
this.#value = value;
|
|
109
|
+
return this.#value;
|
|
110
|
+
}
|
|
111
|
+
getOrInsertWith(f) {
|
|
112
|
+
if (this.isNone())
|
|
113
|
+
this.#value = f();
|
|
114
|
+
return this.#value;
|
|
115
|
+
}
|
|
116
|
+
take() {
|
|
117
|
+
if (this.isSome()) {
|
|
118
|
+
const val = this.value;
|
|
119
|
+
this.#value = null;
|
|
120
|
+
return Some(val);
|
|
121
|
+
}
|
|
122
|
+
return None();
|
|
123
|
+
}
|
|
124
|
+
takeIf(predicate) {
|
|
125
|
+
if (this.isSome() && predicate(this.value)) {
|
|
126
|
+
const val = this.value;
|
|
127
|
+
this.#value = null;
|
|
128
|
+
return Some(val);
|
|
129
|
+
}
|
|
130
|
+
return None();
|
|
131
|
+
}
|
|
132
|
+
replace(value) {
|
|
133
|
+
const old = this.#value;
|
|
134
|
+
this.#value = value;
|
|
135
|
+
if (old !== null)
|
|
136
|
+
return Some(old);
|
|
137
|
+
return None();
|
|
138
|
+
}
|
|
139
|
+
flatten() {
|
|
140
|
+
if (this.isSome())
|
|
141
|
+
return this.value;
|
|
142
|
+
return None();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Some value of type `T`.
|
|
147
|
+
* @param value The value to be wrapped in a `Some`.
|
|
148
|
+
* @returns An `Option` representing the presence of a value.
|
|
149
|
+
*/
|
|
150
|
+
export const Some = (value) => {
|
|
151
|
+
return new OptionImpl(value);
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* No value.
|
|
155
|
+
* @returns An `Option` representing the absence of a value.
|
|
156
|
+
*/
|
|
157
|
+
export const None = () => {
|
|
158
|
+
return new OptionImpl(null);
|
|
159
|
+
};
|
|
160
|
+
//# sourceMappingURL=option.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"option.js","sourceRoot":"","sources":["../src/option.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,EAAE,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAuKlE,MAAM,UAAU;IACZ,MAAM,CAAW;IAEjB,YAAY,KAAe;QACvB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,MAAM;QACF,OAAO,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC;IAC/B,CAAC;IAED,SAAS,CAAC,CAAsB;QAC5B,OAAO,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,MAAM;QACF,OAAO,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC;IAC/B,CAAC;IAED,QAAQ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AAO,IAAI,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC;QACD,OAAO,IAAI,EAAK,CAAC;IACrB,CAAC;IAED,OAAO,CAAC,KAAQ;QACZ,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,GAAG,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC;QACnC,OAAO,IAAI,EAAK,CAAC;IACrB,CAAC;IAED,OAAO;QACH,IAAI,IAAI,CAAC,MAAM,EAAE;YAAE,OAAO,IAAI,CAAC,KAAkB,CAAC;QAClD,OAAO,IAAI,EAAK,CAAC;IACrB,CAAC;CACJ;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAI,KAAQ,EAAa,EAAE;IAC3C,OAAO,IAAI,UAAU,CAAI,KAAK,CAAc,CAAC;AACjD,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,GAAyB,EAAE;IAC3C,OAAO,IAAI,UAAU,CAAI,IAAI,CAAc,CAAC;AAChD,CAAC,CAAC"}
|
package/dist/result.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Option } from './option';
|
|
1
2
|
/**
|
|
2
3
|
* The base interface for all errors returned by a `Result`.
|
|
3
4
|
* It requires a `code` property which can be used to identify the error type.
|
|
@@ -41,7 +42,6 @@ export type ErrResult<T, E extends ResultError> = {
|
|
|
41
42
|
* else console.log(value);
|
|
42
43
|
* ```
|
|
43
44
|
*
|
|
44
|
-
* @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
|
|
45
45
|
* @template T - Contains the success value.
|
|
46
46
|
* @template E - Contains the error value. Must have a `code` property of type `string`.
|
|
47
47
|
*/
|
|
@@ -54,7 +54,7 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
54
54
|
/**
|
|
55
55
|
* Returns `true` if the result is `Ok` and the value inside of it matches a predicate.
|
|
56
56
|
*/
|
|
57
|
-
isOkAnd(
|
|
57
|
+
isOkAnd(f: (val: T) => boolean): this is OkResult<T, E>;
|
|
58
58
|
/**
|
|
59
59
|
* Returns `true` if the result is `Err`.
|
|
60
60
|
*/
|
|
@@ -62,47 +62,59 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
62
62
|
/**
|
|
63
63
|
* Returns `true` if the result is `Err` and the value inside of it matches a predicate.
|
|
64
64
|
*/
|
|
65
|
-
isErrAnd(
|
|
65
|
+
isErrAnd(f: (err: E) => boolean): this is ErrResult<T, E>;
|
|
66
|
+
/**
|
|
67
|
+
* Converts from `Result<T, E>` to `Option<T>`.
|
|
68
|
+
*
|
|
69
|
+
* Converts self into an `Option<T>`, consuming self, and converting the error to `None`, if any.
|
|
70
|
+
*/
|
|
71
|
+
ok(): Option<T>;
|
|
72
|
+
/**
|
|
73
|
+
* Converts from `Result<T, E>` to `Option<E>`.
|
|
74
|
+
*
|
|
75
|
+
* Converts self into an `Option<E>`, consuming self, and discarding the success value, if any.
|
|
76
|
+
*/
|
|
77
|
+
err(): Option<E>;
|
|
66
78
|
/**
|
|
67
79
|
* Maps a `Result<T, E>` to `Result<U, E>` by applying a function to a contained `Ok` value, leaving an `Err` value untouched.
|
|
68
80
|
*
|
|
69
81
|
* This function can be used to compose the results of two functions.
|
|
70
82
|
*/
|
|
71
|
-
map<U>(
|
|
83
|
+
map<U>(f: (val: T) => U): Result<U, E>;
|
|
72
84
|
/**
|
|
73
85
|
* Returns the provided default (if `Err`), or applies a function to the contained value (if `Ok`).
|
|
74
86
|
*
|
|
75
87
|
* Arguments passed to `mapOr` are eagerly evaluated; if you are passing the result of a function call, it is recommended to use `mapOrElse`, which is lazily evaluated.
|
|
76
88
|
*/
|
|
77
|
-
mapOr<U>(fallback: U,
|
|
89
|
+
mapOr<U>(fallback: U, f: (val: T) => U): U;
|
|
78
90
|
/**
|
|
79
|
-
* Maps a `Result<T, E>` to `U` by applying fallback function `fallbackFn` to a contained `Err` value, or function `
|
|
91
|
+
* Maps a `Result<T, E>` to `U` by applying fallback function `fallbackFn` to a contained `Err` value, or function `f` to a contained `Ok` value.
|
|
80
92
|
*
|
|
81
93
|
* This function can be used to unpack a successful result while handling an error.
|
|
82
94
|
*/
|
|
83
|
-
mapOrElse<U>(fallbackFn: (err: E) => U,
|
|
95
|
+
mapOrElse<U>(fallbackFn: (err: E) => U, f: (val: T) => U): U;
|
|
84
96
|
/**
|
|
85
97
|
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained `Err` value, leaving an `Ok` value untouched.
|
|
86
98
|
*
|
|
87
99
|
* This function can be used to pass through a successful result while handling an error.
|
|
88
100
|
*/
|
|
89
|
-
mapErr<F extends ResultError>(
|
|
101
|
+
mapErr<F extends ResultError>(f: (err: E) => F): Result<T, F>;
|
|
90
102
|
/**
|
|
91
103
|
* Calls a function with a reference to the contained value if `Ok`.
|
|
92
104
|
*
|
|
93
105
|
* Returns the original result.
|
|
94
106
|
*/
|
|
95
|
-
inspect(
|
|
107
|
+
inspect(f: (val: T) => void): Result<T, E>;
|
|
96
108
|
/**
|
|
97
109
|
* Calls a function with a reference to the contained value if `Err`.
|
|
98
110
|
*
|
|
99
111
|
* Returns the original result.
|
|
100
112
|
*/
|
|
101
|
-
inspectErr(
|
|
113
|
+
inspectErr(f: (err: E) => void): Result<T, E>;
|
|
102
114
|
/**
|
|
103
115
|
* Returns an iterator over the possibly contained value.
|
|
104
116
|
*
|
|
105
|
-
* The iterator yields one value if the result is `
|
|
117
|
+
* The iterator yields one value if the result is `Ok`, otherwise none.
|
|
106
118
|
*/
|
|
107
119
|
iter(): Iterable<T>;
|
|
108
120
|
/**
|
|
@@ -136,11 +148,11 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
136
148
|
*/
|
|
137
149
|
and<U, E2 extends ResultError>(res: Result<U, E2>): Result<U, E | E2>;
|
|
138
150
|
/**
|
|
139
|
-
* Calls `
|
|
151
|
+
* Calls `f` if the result is `Ok`, otherwise returns the `Err` value of `self`.
|
|
140
152
|
*
|
|
141
153
|
* This function can be used for control flow based on `Result` values.
|
|
142
154
|
*/
|
|
143
|
-
andThen<U, F extends ResultError>(
|
|
155
|
+
andThen<U, F extends ResultError>(f: (val: T) => Result<U, F>): Result<U, E | F>;
|
|
144
156
|
/**
|
|
145
157
|
* Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`.
|
|
146
158
|
*
|
|
@@ -148,11 +160,11 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
148
160
|
*/
|
|
149
161
|
or<T2, F extends ResultError>(res: Result<T2, F>): Result<T | T2, F>;
|
|
150
162
|
/**
|
|
151
|
-
* Calls `
|
|
163
|
+
* Calls `f` if the result is `Err`, otherwise returns the `Ok` value of `self`.
|
|
152
164
|
*
|
|
153
165
|
* This function can be used for control flow based on result values.
|
|
154
166
|
*/
|
|
155
|
-
orElse<T2, F extends ResultError>(
|
|
167
|
+
orElse<T2, F extends ResultError>(f: (err: E) => Result<T2, F>): Result<T | T2, F>;
|
|
156
168
|
/**
|
|
157
169
|
* Returns the contained `Ok` value or a provided default.
|
|
158
170
|
*
|
|
@@ -162,7 +174,8 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
162
174
|
/**
|
|
163
175
|
* Returns the contained `Ok` value or computes it from a closure.
|
|
164
176
|
*/
|
|
165
|
-
unwrapOrElse<T2>(
|
|
177
|
+
unwrapOrElse<T2>(f: (err: E) => T2): T | T2;
|
|
178
|
+
flatten<U, F extends ResultError>(this: Result<Result<U, F>, E>): Result<U, E | F>;
|
|
166
179
|
}
|
|
167
180
|
/**
|
|
168
181
|
* Contains the success value.
|
|
@@ -172,7 +185,6 @@ interface ResultMethods<T, E extends ResultError> {
|
|
|
172
185
|
* const { value } = Ok(42);
|
|
173
186
|
* ```
|
|
174
187
|
*
|
|
175
|
-
* @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
|
|
176
188
|
* @param value - The value to wrap in a successful result.
|
|
177
189
|
* @returns A `Result` representing a successful outcome.
|
|
178
190
|
*/
|
|
@@ -185,7 +197,6 @@ export declare const Ok: <T, E extends ResultError = never>(value: T) => Result<
|
|
|
185
197
|
* const { error } = Err({ code: 'NOT_FOUND' });
|
|
186
198
|
* ```
|
|
187
199
|
*
|
|
188
|
-
* @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
|
|
189
200
|
* @param error - The error to wrap in a failed result. Must have a `code` property of type `string`.
|
|
190
201
|
* @returns A `Result` representing a failed outcome.
|
|
191
202
|
*/
|
package/dist/result.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,IAAI;IAC7C,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAClB,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC;CACxB,GAAG,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAExB;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,IAAI;IAC9C,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CACrB,GAAG,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAExB
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAc,MAAM,UAAU,CAAC;AAEnD;;;GAGG;AACH,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,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;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,GAAG,GACZ,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,CAAC,SAAS,WAAW,GAAG;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,EAEnC,OAAO,CAAC,KACT,MAAM,CAAC,KAAK,EAAE,CAAC,CAEjB,CAAC"}
|
package/dist/result.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Some, None } from './option';
|
|
1
2
|
class ResultImpl {
|
|
2
3
|
// will error at runtime if trying to access # fields
|
|
3
4
|
#value;
|
|
@@ -17,44 +18,53 @@ class ResultImpl {
|
|
|
17
18
|
isOk() {
|
|
18
19
|
return this.error === null;
|
|
19
20
|
}
|
|
20
|
-
isOkAnd(
|
|
21
|
-
return this.isOk() &&
|
|
21
|
+
isOkAnd(f) {
|
|
22
|
+
return this.isOk() && f(this.value);
|
|
22
23
|
}
|
|
23
24
|
isErr() {
|
|
24
25
|
return this.value === null;
|
|
25
26
|
}
|
|
26
|
-
isErrAnd(
|
|
27
|
-
return this.isErr() &&
|
|
27
|
+
isErrAnd(f) {
|
|
28
|
+
return this.isErr() && f(this.error);
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
if (this.
|
|
30
|
+
ok() {
|
|
31
|
+
if (this.isOk())
|
|
32
|
+
return Some(this.value);
|
|
33
|
+
return None();
|
|
34
|
+
}
|
|
35
|
+
err() {
|
|
36
|
+
if (this.isErr())
|
|
37
|
+
return Some(this.error);
|
|
38
|
+
return None();
|
|
39
|
+
}
|
|
40
|
+
map(f) {
|
|
41
|
+
if (this.isErr())
|
|
31
42
|
return new ResultImpl(null, this.error);
|
|
32
|
-
|
|
33
|
-
return new ResultImpl(fn(this.value), null);
|
|
43
|
+
return new ResultImpl(f(this.value), null);
|
|
34
44
|
}
|
|
35
|
-
mapOr(fallback,
|
|
45
|
+
mapOr(fallback, f) {
|
|
36
46
|
if (this.isErr())
|
|
37
47
|
return fallback;
|
|
38
|
-
return
|
|
48
|
+
return f(this.value);
|
|
39
49
|
}
|
|
40
|
-
mapOrElse(fallbackFn,
|
|
50
|
+
mapOrElse(fallbackFn, f) {
|
|
41
51
|
if (this.isErr())
|
|
42
52
|
return fallbackFn(this.error);
|
|
43
|
-
return
|
|
53
|
+
return f(this.value);
|
|
44
54
|
}
|
|
45
|
-
mapErr(
|
|
55
|
+
mapErr(f) {
|
|
46
56
|
if (this.isErr())
|
|
47
|
-
return new ResultImpl(null,
|
|
57
|
+
return new ResultImpl(null, f(this.error));
|
|
48
58
|
return new ResultImpl(this.value, null);
|
|
49
59
|
}
|
|
50
|
-
inspect(
|
|
60
|
+
inspect(f) {
|
|
51
61
|
if (this.isOk())
|
|
52
|
-
|
|
62
|
+
f(this.value);
|
|
53
63
|
return this;
|
|
54
64
|
}
|
|
55
|
-
inspectErr(
|
|
65
|
+
inspectErr(f) {
|
|
56
66
|
if (this.isErr())
|
|
57
|
-
|
|
67
|
+
f(this.error);
|
|
58
68
|
return this;
|
|
59
69
|
}
|
|
60
70
|
*iter() {
|
|
@@ -86,9 +96,9 @@ class ResultImpl {
|
|
|
86
96
|
return res;
|
|
87
97
|
return new ResultImpl(null, this.error);
|
|
88
98
|
}
|
|
89
|
-
andThen(
|
|
99
|
+
andThen(f) {
|
|
90
100
|
if (this.isOk())
|
|
91
|
-
return
|
|
101
|
+
return f(this.value);
|
|
92
102
|
return new ResultImpl(null, this.error);
|
|
93
103
|
}
|
|
94
104
|
or(res) {
|
|
@@ -96,9 +106,9 @@ class ResultImpl {
|
|
|
96
106
|
return res;
|
|
97
107
|
return new ResultImpl(this.value, null);
|
|
98
108
|
}
|
|
99
|
-
orElse(
|
|
109
|
+
orElse(f) {
|
|
100
110
|
if (this.isErr())
|
|
101
|
-
return
|
|
111
|
+
return f(this.error);
|
|
102
112
|
return new ResultImpl(this.value, null);
|
|
103
113
|
}
|
|
104
114
|
unwrapOr(fallback) {
|
|
@@ -106,11 +116,19 @@ class ResultImpl {
|
|
|
106
116
|
return fallback;
|
|
107
117
|
return this.value;
|
|
108
118
|
}
|
|
109
|
-
unwrapOrElse(
|
|
119
|
+
unwrapOrElse(f) {
|
|
110
120
|
if (this.isErr())
|
|
111
|
-
return
|
|
121
|
+
return f(this.error);
|
|
112
122
|
return this.value;
|
|
113
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
|
|
126
|
+
*/
|
|
127
|
+
flatten() {
|
|
128
|
+
if (this.isOk())
|
|
129
|
+
return this.value;
|
|
130
|
+
return new ResultImpl(null, this.error);
|
|
131
|
+
}
|
|
114
132
|
}
|
|
115
133
|
/**
|
|
116
134
|
* Contains the success value.
|
|
@@ -120,7 +138,6 @@ class ResultImpl {
|
|
|
120
138
|
* const { value } = Ok(42);
|
|
121
139
|
* ```
|
|
122
140
|
*
|
|
123
|
-
* @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
|
|
124
141
|
* @param value - The value to wrap in a successful result.
|
|
125
142
|
* @returns A `Result` representing a successful outcome.
|
|
126
143
|
*/
|
|
@@ -135,7 +152,6 @@ export const Ok = (value) => {
|
|
|
135
152
|
* const { error } = Err({ code: 'NOT_FOUND' });
|
|
136
153
|
* ```
|
|
137
154
|
*
|
|
138
|
-
* @see https://www.youtube.com/watch?v=ovnyeq-Xxrc
|
|
139
155
|
* @param error - The error to wrap in a failed result. Must have a `code` property of type `string`.
|
|
140
156
|
* @returns A `Result` representing a failed outcome.
|
|
141
157
|
*/
|
package/dist/result.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,IAAI,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAgMnD,MAAM,UAAU;IACZ,qDAAqD;IACrD,MAAM,CAAW;IACjB,MAAM,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AAC;IAED,YAAY,CAAK,CAAiB;QAC9B,IAAI,IAAI,CAAC,KAAK,EAAE;YAAE,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACvC,OAAO,IAAI,CAAC,KAAU,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,OAAO;QAGH,IAAI,IAAI,CAAC,IAAI,EAAE;YAAE,OAAO,IAAI,CAAC,KAAyB,CAAC;QACvD,OAAO,IAAI,UAAU,CAAW,IAAI,EAAE,IAAI,CAAC,KAAK,CAAqB,CAAC;IAC1E,CAAC;CACJ;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,CACd,KAAQ,EACI,EAAE;IACd,OAAO,IAAI,UAAU,CAAO,KAAK,EAAE,IAAI,CAAiB,CAAC;AAC7D,CAAC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CAIf,KAAQ,EACQ,EAAE;IAClB,OAAO,IAAI,UAAU,CAAW,IAAI,EAAE,KAAK,CAAqB,CAAC;AACrE,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@madkarma/result",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Rust's Result
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Rust's Result and Option types, for TypeScript",
|
|
5
5
|
"devDependencies": {
|
|
6
|
+
"@types/bun": "^1.3.14",
|
|
6
7
|
"prettier": "3.8.3",
|
|
7
8
|
"typescript": "^6.0.3",
|
|
8
9
|
"vitest": "^4.1.6"
|