@consolidados/results 0.1.5 → 0.2.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 +593 -253
- package/dist/helpers/match.cjs +28 -5
- package/dist/helpers/match.cjs.map +1 -1
- package/dist/helpers/match.d.cts +30 -3
- package/dist/helpers/match.d.ts +30 -3
- package/dist/helpers/match.js +28 -5
- package/dist/helpers/match.js.map +1 -1
- package/dist/index.cjs +310 -151
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +309 -150
- package/dist/index.js.map +1 -1
- package/dist/option/index.cjs +321 -18
- package/dist/option/index.cjs.map +1 -1
- package/dist/option/index.d.cts +1 -1
- package/dist/option/index.d.ts +1 -1
- package/dist/option/index.js +321 -18
- package/dist/option/index.js.map +1 -1
- package/dist/option/option.cjs +321 -18
- package/dist/option/option.cjs.map +1 -1
- package/dist/option/option.d.cts +5 -4
- package/dist/option/option.d.ts +5 -4
- package/dist/option/option.js +321 -18
- package/dist/option/option.js.map +1 -1
- package/dist/option-B_KKIecf.d.cts +352 -0
- package/dist/option-B_KKIecf.d.ts +352 -0
- package/dist/result/index.cjs +86 -23
- package/dist/result/index.cjs.map +1 -1
- package/dist/result/index.d.cts +1 -2
- package/dist/result/index.d.ts +1 -2
- package/dist/result/index.js +86 -23
- package/dist/result/index.js.map +1 -1
- package/dist/result/result.cjs +86 -23
- package/dist/result/result.cjs.map +1 -1
- package/dist/result/result.d.cts +4 -12
- package/dist/result/result.d.ts +4 -12
- package/dist/result/result.js +86 -23
- package/dist/result/result.js.map +1 -1
- package/dist/types/globals.d.cts +26 -146
- package/dist/types/globals.d.ts +26 -146
- package/package.json +5 -5
- package/dist/option-DpT8KCGE.d.cts +0 -96
- package/dist/option-DpT8KCGE.d.ts +0 -96
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import './types/globals.cjs';
|
|
2
2
|
export { match } from './helpers/match.cjs';
|
|
3
3
|
export { None, Some } from './option/option.cjs';
|
|
4
4
|
export { Err, Ok } from './result/result.cjs';
|
|
5
|
-
export { O as Option } from './option-
|
|
5
|
+
export { O as Option, R as Result } from './option-B_KKIecf.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import './types/globals.js';
|
|
2
2
|
export { match } from './helpers/match.js';
|
|
3
3
|
export { None, Some } from './option/option.js';
|
|
4
4
|
export { Err, Ok } from './result/result.js';
|
|
5
|
-
export { O as Option } from './option-
|
|
5
|
+
export { O as Option, R as Result } from './option-B_KKIecf.js';
|
package/dist/index.js
CHANGED
|
@@ -1,159 +1,57 @@
|
|
|
1
1
|
// src/helpers/match.ts
|
|
2
|
-
function
|
|
3
|
-
if ("
|
|
2
|
+
function match(matcher, cases, discriminant) {
|
|
3
|
+
if (typeof matcher === "string" || typeof matcher === "number" || typeof matcher === "symbol") {
|
|
4
|
+
const handler = cases[matcher];
|
|
5
|
+
if (handler) {
|
|
6
|
+
return handler();
|
|
7
|
+
}
|
|
8
|
+
if (cases.default) {
|
|
9
|
+
return cases.default();
|
|
10
|
+
}
|
|
11
|
+
throw new Error(`No case found for value: ${String(matcher)}`);
|
|
12
|
+
}
|
|
13
|
+
if (discriminant && typeof matcher === "object" && matcher !== null) {
|
|
14
|
+
const discriminantValue = matcher[discriminant];
|
|
15
|
+
const handler = cases[discriminantValue];
|
|
16
|
+
if (handler) {
|
|
17
|
+
return handler(matcher);
|
|
18
|
+
}
|
|
19
|
+
if (cases.default) {
|
|
20
|
+
return cases.default(matcher);
|
|
21
|
+
}
|
|
22
|
+
throw new Error(
|
|
23
|
+
`No case found for discriminant value: ${String(discriminantValue)}`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
if (typeof matcher === "object" && matcher !== null && "isOk" in matcher && matcher.isOk()) {
|
|
4
27
|
if (!cases.Ok) throw new Error("Missing case for Ok");
|
|
5
28
|
return cases.Ok(matcher.unwrap());
|
|
6
29
|
}
|
|
7
|
-
if ("isErr" in matcher && matcher.isErr()) {
|
|
30
|
+
if (typeof matcher === "object" && matcher !== null && "isErr" in matcher && matcher.isErr()) {
|
|
8
31
|
if (!cases.Err) throw new Error("Missing case for Err");
|
|
9
32
|
return cases.Err(matcher.unwrapErr());
|
|
10
33
|
}
|
|
11
|
-
if ("isSome" in matcher && matcher.isSome()) {
|
|
34
|
+
if (typeof matcher === "object" && matcher !== null && "isSome" in matcher && matcher.isSome()) {
|
|
12
35
|
if (!cases.Some) throw new Error("Missing case for Some");
|
|
13
36
|
return cases.Some(matcher.unwrap());
|
|
14
37
|
}
|
|
15
|
-
if ("isNone" in matcher && matcher.isNone()) {
|
|
38
|
+
if (typeof matcher === "object" && matcher !== null && "isNone" in matcher && matcher.isNone()) {
|
|
16
39
|
if (!cases.None) throw new Error("Missing case for None");
|
|
17
40
|
return cases.None();
|
|
18
41
|
}
|
|
19
42
|
throw new Error("Invalid matcher or missing case");
|
|
20
43
|
}
|
|
21
|
-
global.match =
|
|
22
|
-
|
|
23
|
-
// src/option/__internal__/return-types/some.ts
|
|
24
|
-
var Some2 = class _Some {
|
|
25
|
-
/**
|
|
26
|
-
* Creates a new `Some` option with the given value.
|
|
27
|
-
* @param value The value to be wrapped in the `Some` option.
|
|
28
|
-
*/
|
|
29
|
-
constructor(value) {
|
|
30
|
-
this.value = value;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Checks if this option is a `Some`.
|
|
34
|
-
* @returns `true` if this option is a `Some`, otherwise `false`.
|
|
35
|
-
*/
|
|
36
|
-
isSome() {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Checks if this option is a `None`.
|
|
41
|
-
* @returns `false` because this is a `Some`.
|
|
42
|
-
*/
|
|
43
|
-
isNone() {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Unwraps the value held by this `Some` option.
|
|
48
|
-
* @returns The value held by this `Some` option.
|
|
49
|
-
*/
|
|
50
|
-
unwrap() {
|
|
51
|
-
return this.value;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.
|
|
55
|
-
* @template U The type of the transformed value.
|
|
56
|
-
* @param fn The transformation function to apply to the value.
|
|
57
|
-
* @returns A new `Some` option containing the transformed value.
|
|
58
|
-
*/
|
|
59
|
-
map(fn) {
|
|
60
|
-
return new _Some(fn(this.value));
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Applies a transformation function that returns an `Option` to the value held by this `Some` option.
|
|
64
|
-
* @template U The type of the value in the resulting `Option`.
|
|
65
|
-
* @param fn The transformation function to apply to the value.
|
|
66
|
-
* @returns The result of applying the transformation function.
|
|
67
|
-
*/
|
|
68
|
-
flatMap(fn) {
|
|
69
|
-
return fn(this.value);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Returns the value held by this `Some` option, ignoring the default value provided.
|
|
73
|
-
* @param _ A default value (ignored in this implementation).
|
|
74
|
-
* @returns The value held by this `Some` option.
|
|
75
|
-
*/
|
|
76
|
-
unwrapOr(_) {
|
|
77
|
-
return this.value;
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// src/option/__internal__/return-types/none.ts
|
|
82
|
-
var None2 = class _None {
|
|
83
|
-
/**
|
|
84
|
-
* Checks if this option is a `Some`.
|
|
85
|
-
* @returns `false` because this is a `None`.
|
|
86
|
-
*/
|
|
87
|
-
isSome() {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Checks if this option is a `None`.
|
|
92
|
-
* @returns `true` because this is a `None`.
|
|
93
|
-
*/
|
|
94
|
-
isNone() {
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Attempts to unwrap the value from this `None` option.
|
|
99
|
-
* @throws An error because `None` has no value.
|
|
100
|
-
*/
|
|
101
|
-
unwrap() {
|
|
102
|
-
throw new Error("Called unwrap on a None value");
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Applies a transformation function to the value (which does not exist) of this `None` option.
|
|
106
|
-
* @template U The type of the value that would have been returned.
|
|
107
|
-
* @param _fn The transformation function (ignored in this implementation).
|
|
108
|
-
* @returns A new `None` option.
|
|
109
|
-
*/
|
|
110
|
-
map(_fn) {
|
|
111
|
-
return new _None();
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.
|
|
115
|
-
* @template U The type of the value in the resulting `Option`.
|
|
116
|
-
* @param _fn The transformation function (ignored in this implementation).
|
|
117
|
-
* @returns A new `None` option.
|
|
118
|
-
*/
|
|
119
|
-
flatMap(_fn) {
|
|
120
|
-
return new _None();
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Returns the default value provided, since `None` has no value.
|
|
124
|
-
* @template T The type of the default value.
|
|
125
|
-
* @param defaultValue The value to return.
|
|
126
|
-
* @returns The default value provided.
|
|
127
|
-
*/
|
|
128
|
-
unwrapOr(defaultValue) {
|
|
129
|
-
return defaultValue;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
// src/option/option.ts
|
|
134
|
-
function Some3(value) {
|
|
135
|
-
return new Some2(value);
|
|
136
|
-
}
|
|
137
|
-
function None3() {
|
|
138
|
-
return new None2();
|
|
139
|
-
}
|
|
140
|
-
global.Some = Some3;
|
|
141
|
-
global.None = None3;
|
|
44
|
+
global.match = match;
|
|
142
45
|
|
|
143
46
|
// src/result/__internal__/return-types/err.ts
|
|
144
|
-
var Err = class _Err
|
|
47
|
+
var Err = class _Err {
|
|
145
48
|
error;
|
|
146
49
|
/**
|
|
147
50
|
* Creates a new `Err` instance with the given error value.
|
|
148
|
-
* @param error The error to wrap in the `Err` instance.
|
|
51
|
+
* @param error The error to wrap in the `Err` instance. Can be any type.
|
|
149
52
|
*/
|
|
150
53
|
constructor(error) {
|
|
151
|
-
|
|
152
|
-
this.error = typeof error === "string" ? new Error(error) : error;
|
|
153
|
-
Object.setPrototypeOf(this, _Err.prototype);
|
|
154
|
-
if (Error.captureStackTrace) {
|
|
155
|
-
Error.captureStackTrace(this, _Err);
|
|
156
|
-
}
|
|
54
|
+
this.error = error;
|
|
157
55
|
}
|
|
158
56
|
/**
|
|
159
57
|
* Checks if this result is an `Ok`.
|
|
@@ -187,7 +85,7 @@ var Err = class _Err extends Error {
|
|
|
187
85
|
}
|
|
188
86
|
/**
|
|
189
87
|
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
190
|
-
* @template U The type of the transformed error.
|
|
88
|
+
* @template U The type of the transformed error. Can be any type.
|
|
191
89
|
* @param fn The transformation function to apply to the error value.
|
|
192
90
|
* @returns A new `Err` containing the transformed error.
|
|
193
91
|
*/
|
|
@@ -197,8 +95,9 @@ var Err = class _Err extends Error {
|
|
|
197
95
|
/**
|
|
198
96
|
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
199
97
|
* @template U The type of the value in the resulting `Result`.
|
|
98
|
+
* @template F The type of the error in the resulting `Result`.
|
|
200
99
|
* @param _fn The transformation function (ignored in this implementation).
|
|
201
|
-
* @returns The original `Err` instance.
|
|
100
|
+
* @returns The original `Err` instance cast to the new error type.
|
|
202
101
|
*/
|
|
203
102
|
flatMap(_fn) {
|
|
204
103
|
return this;
|
|
@@ -210,6 +109,42 @@ var Err = class _Err extends Error {
|
|
|
210
109
|
unwrapErr() {
|
|
211
110
|
return this.error;
|
|
212
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.
|
|
114
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
115
|
+
* @returns The error value contained in this `Err`.
|
|
116
|
+
*/
|
|
117
|
+
value() {
|
|
118
|
+
return this.error;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
122
|
+
* @template U The type of the default value.
|
|
123
|
+
* @param defaultValue The value to return since this is an `Err`.
|
|
124
|
+
* @returns The provided default value.
|
|
125
|
+
*/
|
|
126
|
+
unwrapOr(defaultValue) {
|
|
127
|
+
return defaultValue;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
131
|
+
* @template U The type of the default value.
|
|
132
|
+
* @param fn The function to compute the default value.
|
|
133
|
+
* @returns The result of calling the provided function with the error.
|
|
134
|
+
*/
|
|
135
|
+
unwrapOrElse(fn) {
|
|
136
|
+
return fn(this.error);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.
|
|
140
|
+
* @template T The type of the alternative success value.
|
|
141
|
+
* @template F The type of the alternative error.
|
|
142
|
+
* @param fn The function to compute the alternative result.
|
|
143
|
+
* @returns The result of calling the provided function with the error.
|
|
144
|
+
*/
|
|
145
|
+
orElse(fn) {
|
|
146
|
+
return fn(this.error);
|
|
147
|
+
}
|
|
213
148
|
/**
|
|
214
149
|
* Converts `Result` type to `Option` type.
|
|
215
150
|
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
@@ -223,10 +158,10 @@ var Err = class _Err extends Error {
|
|
|
223
158
|
var Ok = class _Ok {
|
|
224
159
|
/**
|
|
225
160
|
* Creates a new `Ok` instance with the given value.
|
|
226
|
-
* @param
|
|
161
|
+
* @param _value The value to wrap in the `Ok` instance.
|
|
227
162
|
*/
|
|
228
|
-
constructor(
|
|
229
|
-
this.
|
|
163
|
+
constructor(_value) {
|
|
164
|
+
this._value = _value;
|
|
230
165
|
}
|
|
231
166
|
/**
|
|
232
167
|
* Checks if this result is an `Ok`.
|
|
@@ -247,7 +182,15 @@ var Ok = class _Ok {
|
|
|
247
182
|
* @returns The value contained in this `Ok`.
|
|
248
183
|
*/
|
|
249
184
|
unwrap() {
|
|
250
|
-
return this.
|
|
185
|
+
return this._value;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.
|
|
189
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
190
|
+
* @returns The value contained in this `Ok`.
|
|
191
|
+
*/
|
|
192
|
+
value() {
|
|
193
|
+
return this._value;
|
|
251
194
|
}
|
|
252
195
|
/**
|
|
253
196
|
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
@@ -256,25 +199,24 @@ var Ok = class _Ok {
|
|
|
256
199
|
* @returns A new `Ok` containing the transformed value.
|
|
257
200
|
*/
|
|
258
201
|
map(fn) {
|
|
259
|
-
return new _Ok(fn(this.
|
|
202
|
+
return new _Ok(fn(this._value));
|
|
260
203
|
}
|
|
261
204
|
/**
|
|
262
205
|
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
263
206
|
* @template U The type of the value in the resulting `Result`.
|
|
207
|
+
* @template E The type of the error in the resulting `Result`.
|
|
264
208
|
* @param fn The transformation function to apply to the value.
|
|
265
209
|
* @returns The result of applying the transformation function.
|
|
266
210
|
*/
|
|
267
211
|
flatMap(fn) {
|
|
268
|
-
return fn(this.
|
|
212
|
+
return fn(this._value);
|
|
269
213
|
}
|
|
270
214
|
/**
|
|
271
215
|
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
272
|
-
* @template U The type of the error (ignored for `Ok`).
|
|
216
|
+
* @template U The type of the error (ignored for `Ok`). Can be any type.
|
|
273
217
|
* @param _fn The mapping function for errors (not used).
|
|
274
218
|
* @returns The original `Ok` instance.
|
|
275
219
|
*/
|
|
276
|
-
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
|
|
277
|
-
// return this;
|
|
278
220
|
mapErr(_fn) {
|
|
279
221
|
return this;
|
|
280
222
|
}
|
|
@@ -285,15 +227,39 @@ var Ok = class _Ok {
|
|
|
285
227
|
unwrapErr() {
|
|
286
228
|
throw new Error("Called unwrapErr on an Ok value");
|
|
287
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
232
|
+
* @template U The type of the default value.
|
|
233
|
+
* @param defaultValue The value to return if this is an `Err`.
|
|
234
|
+
* @returns The value contained in this `Ok`.
|
|
235
|
+
*/
|
|
236
|
+
unwrapOr(defaultValue) {
|
|
237
|
+
return this._value;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
241
|
+
* @template U The type of the default value.
|
|
242
|
+
* @param fn The function to compute the default value.
|
|
243
|
+
* @returns The value contained in this `Ok`.
|
|
244
|
+
*/
|
|
245
|
+
unwrapOrElse(fn) {
|
|
246
|
+
return this._value;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.
|
|
250
|
+
* @template F The type of the alternative error.
|
|
251
|
+
* @param fn The function to compute the alternative result.
|
|
252
|
+
* @returns The original `Ok` instance.
|
|
253
|
+
*/
|
|
254
|
+
orElse(fn) {
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
288
257
|
/**
|
|
289
258
|
* Converts `Result` type to `Option` type.
|
|
290
259
|
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
291
260
|
*/
|
|
292
261
|
ok() {
|
|
293
|
-
return
|
|
294
|
-
Ok: (v) => Some(v),
|
|
295
|
-
Err: (_) => None()
|
|
296
|
-
});
|
|
262
|
+
return Some(this._value);
|
|
297
263
|
}
|
|
298
264
|
};
|
|
299
265
|
|
|
@@ -307,6 +273,199 @@ function Err2(error) {
|
|
|
307
273
|
global.Ok = Ok2;
|
|
308
274
|
global.Err = Err2;
|
|
309
275
|
|
|
310
|
-
|
|
276
|
+
// src/option/__internal__/return-types/some.ts
|
|
277
|
+
var Some2 = class _Some {
|
|
278
|
+
/**
|
|
279
|
+
* Creates a new `Some` option with the given value.
|
|
280
|
+
* @param _value The value to be wrapped in the `Some` option.
|
|
281
|
+
*/
|
|
282
|
+
constructor(_value) {
|
|
283
|
+
this._value = _value;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Checks if this option is a `Some`.
|
|
287
|
+
* @returns `true` if this option is a `Some`, otherwise `false`.
|
|
288
|
+
*/
|
|
289
|
+
isSome() {
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Checks if this option is a `None`.
|
|
294
|
+
* @returns `false` because this is a `Some`.
|
|
295
|
+
*/
|
|
296
|
+
isNone() {
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Unwraps the value held by this `Some` option.
|
|
301
|
+
* @returns The value held by this `Some` option.
|
|
302
|
+
*/
|
|
303
|
+
unwrap() {
|
|
304
|
+
return this._value;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Returns the inner value. After an `isSome()` type guard, TypeScript narrows the return type to `T`.
|
|
308
|
+
* Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).
|
|
309
|
+
* @returns The value held by this `Some` option.
|
|
310
|
+
*/
|
|
311
|
+
value() {
|
|
312
|
+
return this._value;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.
|
|
316
|
+
* @template U The type of the transformed value.
|
|
317
|
+
* @param fn The transformation function to apply to the value.
|
|
318
|
+
* @returns A new `Some` option containing the transformed value.
|
|
319
|
+
*/
|
|
320
|
+
map(fn) {
|
|
321
|
+
return new _Some(fn(this._value));
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Applies a transformation function that returns an `Option` to the value held by this `Some` option.
|
|
325
|
+
* @template U The type of the value in the resulting `Option`.
|
|
326
|
+
* @param fn The transformation function to apply to the value.
|
|
327
|
+
* @returns The result of applying the transformation function.
|
|
328
|
+
*/
|
|
329
|
+
flatMap(fn) {
|
|
330
|
+
return fn(this._value);
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Returns the value held by this `Some` option, ignoring the default value provided.
|
|
334
|
+
* @param _ A default value (ignored in this implementation).
|
|
335
|
+
* @returns The value held by this `Some` option.
|
|
336
|
+
*/
|
|
337
|
+
unwrapOr(_) {
|
|
338
|
+
return this._value;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Returns the value held by this `Some` option, ignoring the function provided.
|
|
342
|
+
* @template U The type of the default value.
|
|
343
|
+
* @param _fn A function to compute the default value (ignored in this implementation).
|
|
344
|
+
* @returns The value held by this `Some` option.
|
|
345
|
+
*/
|
|
346
|
+
unwrapOrElse(_fn) {
|
|
347
|
+
return this._value;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Converts this `Option` to a `Result`, using the provided error value if this is `None`.
|
|
351
|
+
* @template E The type of the error.
|
|
352
|
+
* @param _error The error value to use if this is `None` (ignored since this is `Some`).
|
|
353
|
+
* @returns An `Ok` result containing the value from this `Some`.
|
|
354
|
+
*/
|
|
355
|
+
okOr(_error) {
|
|
356
|
+
return Ok2(this._value);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Filters this `Option` based on a predicate function.
|
|
360
|
+
* @param predicate The function to test the value.
|
|
361
|
+
* @returns This `Some` if the predicate returns true, otherwise `None`.
|
|
362
|
+
*/
|
|
363
|
+
filter(predicate) {
|
|
364
|
+
return predicate(this._value) ? this : None2();
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// src/option/__internal__/return-types/none.ts
|
|
369
|
+
var None3 = class {
|
|
370
|
+
/**
|
|
371
|
+
* Checks if this option is a `Some`.
|
|
372
|
+
* @returns `false` because this is a `None`.
|
|
373
|
+
*/
|
|
374
|
+
isSome() {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Checks if this option is a `None`.
|
|
379
|
+
* @returns `true` because this is a `None`.
|
|
380
|
+
*/
|
|
381
|
+
isNone() {
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Attempts to unwrap the value from this `None` option.
|
|
386
|
+
* @throws An error because `None` has no value.
|
|
387
|
+
*/
|
|
388
|
+
unwrap() {
|
|
389
|
+
throw new Error("Called unwrap on a None value");
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Returns `undefined` for a `None` option. After an `isNone()` type guard, TypeScript narrows the return type to `undefined`.
|
|
393
|
+
* Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).
|
|
394
|
+
* Unlike `unwrap()`, this method does not throw an error.
|
|
395
|
+
* @returns `undefined` because this is a `None`.
|
|
396
|
+
*/
|
|
397
|
+
value() {
|
|
398
|
+
return void 0;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Applies a transformation function to the value (which does not exist) of this `None` option.
|
|
402
|
+
* @template U The type of the value that would have been returned.
|
|
403
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
404
|
+
* @returns The singleton `None` instance.
|
|
405
|
+
*/
|
|
406
|
+
map(_fn) {
|
|
407
|
+
return None2();
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.
|
|
411
|
+
* @template U The type of the value in the resulting `Option`.
|
|
412
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
413
|
+
* @returns The singleton `None` instance.
|
|
414
|
+
*/
|
|
415
|
+
flatMap(_fn) {
|
|
416
|
+
return None2();
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Returns the default value provided, since `None` has no value.
|
|
420
|
+
* @template T The type of the default value.
|
|
421
|
+
* @param defaultValue The value to return.
|
|
422
|
+
* @returns The default value provided.
|
|
423
|
+
*/
|
|
424
|
+
unwrapOr(defaultValue) {
|
|
425
|
+
return defaultValue;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Computes and returns the default value using the provided function, since `None` has no value.
|
|
429
|
+
* @template T The type of the default value.
|
|
430
|
+
* @param fn The function to compute the default value.
|
|
431
|
+
* @returns The result of calling the provided function.
|
|
432
|
+
*/
|
|
433
|
+
unwrapOrElse(fn) {
|
|
434
|
+
return fn();
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Converts this `Option` to a `Result`, using the provided error value since this is `None`.
|
|
438
|
+
* @template E The type of the error.
|
|
439
|
+
* @param error The error value to use.
|
|
440
|
+
* @returns An `Err` result containing the provided error.
|
|
441
|
+
*/
|
|
442
|
+
okOr(error) {
|
|
443
|
+
return Err2(error);
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Filters this `Option` based on a predicate function.
|
|
447
|
+
* @param _predicate The function to test the value (ignored since this is `None`).
|
|
448
|
+
* @returns `None` since there is no value to filter.
|
|
449
|
+
*/
|
|
450
|
+
filter(_predicate) {
|
|
451
|
+
return None2();
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// src/option/option.ts
|
|
456
|
+
function Some3(value) {
|
|
457
|
+
return new Some2(value);
|
|
458
|
+
}
|
|
459
|
+
var NONE_INSTANCE = null;
|
|
460
|
+
function None2() {
|
|
461
|
+
if (!NONE_INSTANCE) {
|
|
462
|
+
NONE_INSTANCE = new None3();
|
|
463
|
+
}
|
|
464
|
+
return NONE_INSTANCE;
|
|
465
|
+
}
|
|
466
|
+
global.Some = Some3;
|
|
467
|
+
global.None = None2;
|
|
468
|
+
|
|
469
|
+
export { Err2 as Err, None2 as None, Ok2 as Ok, Some3 as Some, match };
|
|
311
470
|
//# sourceMappingURL=index.js.map
|
|
312
471
|
//# sourceMappingURL=index.js.map
|