@consolidados/results 0.1.4 → 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 +312 -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 +89 -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 +88 -23
- package/dist/result/index.js.map +1 -1
- package/dist/result/result.cjs +91 -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 +88 -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 +72 -71
- package/dist/option-DpT8KCGE.d.cts +0 -96
- package/dist/option-DpT8KCGE.d.ts +0 -96
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
type OkType<T> = [T] extends [never] ? never : T;
|
|
2
|
+
type ErrType<E> = [E] extends [never] ? never : E;
|
|
3
|
+
/**
|
|
4
|
+
* Helper type that converts string errors to Error type,
|
|
5
|
+
* but preserves all other types as-is.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
|
|
9
|
+
*/
|
|
10
|
+
type Result<T, E> = Ok<OkType<T>> | Err<ErrType<E>>;
|
|
11
|
+
/**
|
|
12
|
+
* Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
|
|
13
|
+
*/
|
|
14
|
+
interface ResultDefinition<T = never, E = never> {
|
|
15
|
+
isOk(): this is Ok<T>;
|
|
16
|
+
isErr(): this is Err<E>;
|
|
17
|
+
unwrap(): T;
|
|
18
|
+
unwrapErr(): E;
|
|
19
|
+
value(): T | E;
|
|
20
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
|
|
21
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
22
|
+
mapErr<U>(fn: (err: E) => U): ResultDefinition<T, U>;
|
|
23
|
+
unwrapOr<U>(defaultValue: U): T | U;
|
|
24
|
+
unwrapOrElse<U>(fn: (error: E) => U): T | U;
|
|
25
|
+
orElse<F>(fn: (error: E) => ResultDefinition<T, F>): ResultDefinition<T, E | F>;
|
|
26
|
+
ok(): Option<T>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Represents a successful result (`Ok`) that contains a value.
|
|
31
|
+
* @template T The type of the value contained in this `Ok`.
|
|
32
|
+
*/
|
|
33
|
+
declare class Ok<T> implements ResultDefinition<T, never> {
|
|
34
|
+
private _value;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new `Ok` instance with the given value.
|
|
37
|
+
* @param _value The value to wrap in the `Ok` instance.
|
|
38
|
+
*/
|
|
39
|
+
constructor(_value: T);
|
|
40
|
+
/**
|
|
41
|
+
* Checks if this result is an `Ok`.
|
|
42
|
+
* @returns `true` because this is an `Ok`.
|
|
43
|
+
*/
|
|
44
|
+
isOk(): this is Ok<T>;
|
|
45
|
+
/**
|
|
46
|
+
* Checks if this result is an `Err`.
|
|
47
|
+
* @returns `false` because this is an `Ok`.
|
|
48
|
+
*/
|
|
49
|
+
isErr(): this is Err<never>;
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves the value contained in this `Ok`.
|
|
52
|
+
* @returns The value contained in this `Ok`.
|
|
53
|
+
*/
|
|
54
|
+
unwrap(): T;
|
|
55
|
+
/**
|
|
56
|
+
* Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.
|
|
57
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
58
|
+
* @returns The value contained in this `Ok`.
|
|
59
|
+
*/
|
|
60
|
+
value(): T;
|
|
61
|
+
/**
|
|
62
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
63
|
+
* @template U The type of the transformed value.
|
|
64
|
+
* @param fn The transformation function to apply to the value.
|
|
65
|
+
* @returns A new `Ok` containing the transformed value.
|
|
66
|
+
*/
|
|
67
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
|
|
68
|
+
/**
|
|
69
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
70
|
+
* @template U The type of the value in the resulting `Result`.
|
|
71
|
+
* @template E The type of the error in the resulting `Result`.
|
|
72
|
+
* @param fn The transformation function to apply to the value.
|
|
73
|
+
* @returns The result of applying the transformation function.
|
|
74
|
+
*/
|
|
75
|
+
flatMap<U, E = never>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
76
|
+
/**
|
|
77
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
78
|
+
* @template U The type of the error (ignored for `Ok`). Can be any type.
|
|
79
|
+
* @param _fn The mapping function for errors (not used).
|
|
80
|
+
* @returns The original `Ok` instance.
|
|
81
|
+
*/
|
|
82
|
+
mapErr<U>(_fn: (err: never) => U): ResultDefinition<T, U>;
|
|
83
|
+
/**
|
|
84
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
85
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
86
|
+
*/
|
|
87
|
+
unwrapErr(): never;
|
|
88
|
+
/**
|
|
89
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
90
|
+
* @template U The type of the default value.
|
|
91
|
+
* @param defaultValue The value to return if this is an `Err`.
|
|
92
|
+
* @returns The value contained in this `Ok`.
|
|
93
|
+
*/
|
|
94
|
+
unwrapOr<U>(defaultValue: U): T;
|
|
95
|
+
/**
|
|
96
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
97
|
+
* @template U The type of the default value.
|
|
98
|
+
* @param fn The function to compute the default value.
|
|
99
|
+
* @returns The value contained in this `Ok`.
|
|
100
|
+
*/
|
|
101
|
+
unwrapOrElse<U>(fn: (error: never) => U): T;
|
|
102
|
+
/**
|
|
103
|
+
* Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.
|
|
104
|
+
* @template F The type of the alternative error.
|
|
105
|
+
* @param fn The function to compute the alternative result.
|
|
106
|
+
* @returns The original `Ok` instance.
|
|
107
|
+
*/
|
|
108
|
+
orElse<F>(fn: (error: never) => ResultDefinition<T, F>): ResultDefinition<T, F>;
|
|
109
|
+
/**
|
|
110
|
+
* Converts `Result` type to `Option` type.
|
|
111
|
+
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
112
|
+
*/
|
|
113
|
+
ok(): Some<T>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Represents a failed result (`Err`) that contains an error value.
|
|
118
|
+
* @template E The type of the error contained in this `Err`. Can be any type (Error, string, enum, etc).
|
|
119
|
+
*/
|
|
120
|
+
declare class Err<E> implements ResultDefinition<never, E> {
|
|
121
|
+
private error;
|
|
122
|
+
/**
|
|
123
|
+
* Creates a new `Err` instance with the given error value.
|
|
124
|
+
* @param error The error to wrap in the `Err` instance. Can be any type.
|
|
125
|
+
*/
|
|
126
|
+
constructor(error: E);
|
|
127
|
+
/**
|
|
128
|
+
* Checks if this result is an `Ok`.
|
|
129
|
+
* @returns `false` because this is an `Err`.
|
|
130
|
+
*/
|
|
131
|
+
isOk(): this is Ok<never>;
|
|
132
|
+
/**
|
|
133
|
+
* Checks if this result is an `Err`.
|
|
134
|
+
* @returns `true` because this is an `Err`.
|
|
135
|
+
*/
|
|
136
|
+
isErr(): this is Err<E>;
|
|
137
|
+
/**
|
|
138
|
+
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
139
|
+
* @throws An error because `unwrap` is called on an `Err`.
|
|
140
|
+
*/
|
|
141
|
+
unwrap(): never;
|
|
142
|
+
/**
|
|
143
|
+
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
144
|
+
* @template U The type of the value (ignored for `Err`).
|
|
145
|
+
* @param _fn The mapping function for values (not used).
|
|
146
|
+
* @returns The original `Err` instance.
|
|
147
|
+
*/
|
|
148
|
+
map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
|
|
149
|
+
/**
|
|
150
|
+
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
151
|
+
* @template U The type of the transformed error. Can be any type.
|
|
152
|
+
* @param fn The transformation function to apply to the error value.
|
|
153
|
+
* @returns A new `Err` containing the transformed error.
|
|
154
|
+
*/
|
|
155
|
+
mapErr<U>(fn: (err: E) => U): ResultDefinition<never, U>;
|
|
156
|
+
/**
|
|
157
|
+
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
158
|
+
* @template U The type of the value in the resulting `Result`.
|
|
159
|
+
* @template F The type of the error in the resulting `Result`.
|
|
160
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
161
|
+
* @returns The original `Err` instance cast to the new error type.
|
|
162
|
+
*/
|
|
163
|
+
flatMap<U, F = E>(_fn: (value: never) => ResultDefinition<U, F>): ResultDefinition<U, E>;
|
|
164
|
+
/**
|
|
165
|
+
* Retrieves the error value contained in this `Err`.
|
|
166
|
+
* @returns The error value contained in this `Err`.
|
|
167
|
+
*/
|
|
168
|
+
unwrapErr(): E;
|
|
169
|
+
/**
|
|
170
|
+
* Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.
|
|
171
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
172
|
+
* @returns The error value contained in this `Err`.
|
|
173
|
+
*/
|
|
174
|
+
value(): E;
|
|
175
|
+
/**
|
|
176
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
177
|
+
* @template U The type of the default value.
|
|
178
|
+
* @param defaultValue The value to return since this is an `Err`.
|
|
179
|
+
* @returns The provided default value.
|
|
180
|
+
*/
|
|
181
|
+
unwrapOr<U>(defaultValue: U): U;
|
|
182
|
+
/**
|
|
183
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
184
|
+
* @template U The type of the default value.
|
|
185
|
+
* @param fn The function to compute the default value.
|
|
186
|
+
* @returns The result of calling the provided function with the error.
|
|
187
|
+
*/
|
|
188
|
+
unwrapOrElse<U>(fn: (error: E) => U): U;
|
|
189
|
+
/**
|
|
190
|
+
* Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.
|
|
191
|
+
* @template T The type of the alternative success value.
|
|
192
|
+
* @template F The type of the alternative error.
|
|
193
|
+
* @param fn The function to compute the alternative result.
|
|
194
|
+
* @returns The result of calling the provided function with the error.
|
|
195
|
+
*/
|
|
196
|
+
orElse<T, F>(fn: (error: E) => ResultDefinition<T, F>): ResultDefinition<T, F>;
|
|
197
|
+
/**
|
|
198
|
+
* Converts `Result` type to `Option` type.
|
|
199
|
+
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
200
|
+
*/
|
|
201
|
+
ok(): None;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Represents a `Some` option, which holds a value.
|
|
206
|
+
* @template T The type of the value held by this `Some` instance.
|
|
207
|
+
*/
|
|
208
|
+
declare class Some<T> {
|
|
209
|
+
private _value;
|
|
210
|
+
/**
|
|
211
|
+
* Creates a new `Some` option with the given value.
|
|
212
|
+
* @param _value The value to be wrapped in the `Some` option.
|
|
213
|
+
*/
|
|
214
|
+
constructor(_value: T);
|
|
215
|
+
/**
|
|
216
|
+
* Checks if this option is a `Some`.
|
|
217
|
+
* @returns `true` if this option is a `Some`, otherwise `false`.
|
|
218
|
+
*/
|
|
219
|
+
isSome(): this is Some<T>;
|
|
220
|
+
/**
|
|
221
|
+
* Checks if this option is a `None`.
|
|
222
|
+
* @returns `false` because this is a `Some`.
|
|
223
|
+
*/
|
|
224
|
+
isNone(): this is None;
|
|
225
|
+
/**
|
|
226
|
+
* Unwraps the value held by this `Some` option.
|
|
227
|
+
* @returns The value held by this `Some` option.
|
|
228
|
+
*/
|
|
229
|
+
unwrap(): T;
|
|
230
|
+
/**
|
|
231
|
+
* Returns the inner value. After an `isSome()` type guard, TypeScript narrows the return type to `T`.
|
|
232
|
+
* Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).
|
|
233
|
+
* @returns The value held by this `Some` option.
|
|
234
|
+
*/
|
|
235
|
+
value(): T;
|
|
236
|
+
/**
|
|
237
|
+
* Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.
|
|
238
|
+
* @template U The type of the transformed value.
|
|
239
|
+
* @param fn The transformation function to apply to the value.
|
|
240
|
+
* @returns A new `Some` option containing the transformed value.
|
|
241
|
+
*/
|
|
242
|
+
map<U>(fn: (value: T) => U): Option<U>;
|
|
243
|
+
/**
|
|
244
|
+
* Applies a transformation function that returns an `Option` to the value held by this `Some` option.
|
|
245
|
+
* @template U The type of the value in the resulting `Option`.
|
|
246
|
+
* @param fn The transformation function to apply to the value.
|
|
247
|
+
* @returns The result of applying the transformation function.
|
|
248
|
+
*/
|
|
249
|
+
flatMap<U>(fn: (value: T) => Option<U>): Option<U>;
|
|
250
|
+
/**
|
|
251
|
+
* Returns the value held by this `Some` option, ignoring the default value provided.
|
|
252
|
+
* @param _ A default value (ignored in this implementation).
|
|
253
|
+
* @returns The value held by this `Some` option.
|
|
254
|
+
*/
|
|
255
|
+
unwrapOr(_: T): T;
|
|
256
|
+
/**
|
|
257
|
+
* Returns the value held by this `Some` option, ignoring the function provided.
|
|
258
|
+
* @template U The type of the default value.
|
|
259
|
+
* @param _fn A function to compute the default value (ignored in this implementation).
|
|
260
|
+
* @returns The value held by this `Some` option.
|
|
261
|
+
*/
|
|
262
|
+
unwrapOrElse<U>(_fn: () => U): T;
|
|
263
|
+
/**
|
|
264
|
+
* Converts this `Option` to a `Result`, using the provided error value if this is `None`.
|
|
265
|
+
* @template E The type of the error.
|
|
266
|
+
* @param _error The error value to use if this is `None` (ignored since this is `Some`).
|
|
267
|
+
* @returns An `Ok` result containing the value from this `Some`.
|
|
268
|
+
*/
|
|
269
|
+
okOr<E>(_error: E): Ok<T>;
|
|
270
|
+
/**
|
|
271
|
+
* Filters this `Option` based on a predicate function.
|
|
272
|
+
* @param predicate The function to test the value.
|
|
273
|
+
* @returns This `Some` if the predicate returns true, otherwise `None`.
|
|
274
|
+
*/
|
|
275
|
+
filter(predicate: (value: T) => boolean): Option<T>;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Represents a `None` option, which holds no value.
|
|
280
|
+
*/
|
|
281
|
+
declare class None {
|
|
282
|
+
/**
|
|
283
|
+
* Checks if this option is a `Some`.
|
|
284
|
+
* @returns `false` because this is a `None`.
|
|
285
|
+
*/
|
|
286
|
+
isSome(): this is Some<never>;
|
|
287
|
+
/**
|
|
288
|
+
* Checks if this option is a `None`.
|
|
289
|
+
* @returns `true` because this is a `None`.
|
|
290
|
+
*/
|
|
291
|
+
isNone(): this is None;
|
|
292
|
+
/**
|
|
293
|
+
* Attempts to unwrap the value from this `None` option.
|
|
294
|
+
* @throws An error because `None` has no value.
|
|
295
|
+
*/
|
|
296
|
+
unwrap(): never;
|
|
297
|
+
/**
|
|
298
|
+
* Returns `undefined` for a `None` option. After an `isNone()` type guard, TypeScript narrows the return type to `undefined`.
|
|
299
|
+
* Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing).
|
|
300
|
+
* Unlike `unwrap()`, this method does not throw an error.
|
|
301
|
+
* @returns `undefined` because this is a `None`.
|
|
302
|
+
*/
|
|
303
|
+
value(): undefined;
|
|
304
|
+
/**
|
|
305
|
+
* Applies a transformation function to the value (which does not exist) of this `None` option.
|
|
306
|
+
* @template U The type of the value that would have been returned.
|
|
307
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
308
|
+
* @returns The singleton `None` instance.
|
|
309
|
+
*/
|
|
310
|
+
map<U>(_fn: (value: never) => U): Option<U>;
|
|
311
|
+
/**
|
|
312
|
+
* Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.
|
|
313
|
+
* @template U The type of the value in the resulting `Option`.
|
|
314
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
315
|
+
* @returns The singleton `None` instance.
|
|
316
|
+
*/
|
|
317
|
+
flatMap<U>(_fn: (value: never) => Option<U>): Option<U>;
|
|
318
|
+
/**
|
|
319
|
+
* Returns the default value provided, since `None` has no value.
|
|
320
|
+
* @template T The type of the default value.
|
|
321
|
+
* @param defaultValue The value to return.
|
|
322
|
+
* @returns The default value provided.
|
|
323
|
+
*/
|
|
324
|
+
unwrapOr<T>(defaultValue: T): T;
|
|
325
|
+
/**
|
|
326
|
+
* Computes and returns the default value using the provided function, since `None` has no value.
|
|
327
|
+
* @template T The type of the default value.
|
|
328
|
+
* @param fn The function to compute the default value.
|
|
329
|
+
* @returns The result of calling the provided function.
|
|
330
|
+
*/
|
|
331
|
+
unwrapOrElse<T>(fn: () => T): T;
|
|
332
|
+
/**
|
|
333
|
+
* Converts this `Option` to a `Result`, using the provided error value since this is `None`.
|
|
334
|
+
* @template E The type of the error.
|
|
335
|
+
* @param error The error value to use.
|
|
336
|
+
* @returns An `Err` result containing the provided error.
|
|
337
|
+
*/
|
|
338
|
+
okOr<E>(error: E): Err<E>;
|
|
339
|
+
/**
|
|
340
|
+
* Filters this `Option` based on a predicate function.
|
|
341
|
+
* @param _predicate The function to test the value (ignored since this is `None`).
|
|
342
|
+
* @returns `None` since there is no value to filter.
|
|
343
|
+
*/
|
|
344
|
+
filter<T>(_predicate: (value: never) => boolean): Option<T>;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Represents an optional value that can either be `Some` (with a value) or `None` (no value).
|
|
349
|
+
*/
|
|
350
|
+
type Option<T> = Some<T> | None;
|
|
351
|
+
|
|
352
|
+
export { Err as E, None as N, type Option as O, type Result as R, Some as S, Ok as a };
|
package/dist/result/index.cjs
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// src/result/__internal__/return-types/err.ts
|
|
4
|
-
var Err = class _Err
|
|
4
|
+
var Err = class _Err {
|
|
5
5
|
error;
|
|
6
6
|
/**
|
|
7
7
|
* Creates a new `Err` instance with the given error value.
|
|
8
|
-
* @param error The error to wrap in the `Err` instance.
|
|
8
|
+
* @param error The error to wrap in the `Err` instance. Can be any type.
|
|
9
9
|
*/
|
|
10
10
|
constructor(error) {
|
|
11
|
-
|
|
12
|
-
this.error = typeof error === "string" ? new Error(error) : error;
|
|
13
|
-
Object.setPrototypeOf(this, _Err.prototype);
|
|
14
|
-
if (Error.captureStackTrace) {
|
|
15
|
-
Error.captureStackTrace(this, _Err);
|
|
16
|
-
}
|
|
11
|
+
this.error = error;
|
|
17
12
|
}
|
|
18
13
|
/**
|
|
19
14
|
* Checks if this result is an `Ok`.
|
|
@@ -47,7 +42,7 @@ var Err = class _Err extends Error {
|
|
|
47
42
|
}
|
|
48
43
|
/**
|
|
49
44
|
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
50
|
-
* @template U The type of the transformed error.
|
|
45
|
+
* @template U The type of the transformed error. Can be any type.
|
|
51
46
|
* @param fn The transformation function to apply to the error value.
|
|
52
47
|
* @returns A new `Err` containing the transformed error.
|
|
53
48
|
*/
|
|
@@ -57,8 +52,9 @@ var Err = class _Err extends Error {
|
|
|
57
52
|
/**
|
|
58
53
|
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
59
54
|
* @template U The type of the value in the resulting `Result`.
|
|
55
|
+
* @template F The type of the error in the resulting `Result`.
|
|
60
56
|
* @param _fn The transformation function (ignored in this implementation).
|
|
61
|
-
* @returns The original `Err` instance.
|
|
57
|
+
* @returns The original `Err` instance cast to the new error type.
|
|
62
58
|
*/
|
|
63
59
|
flatMap(_fn) {
|
|
64
60
|
return this;
|
|
@@ -70,6 +66,42 @@ var Err = class _Err extends Error {
|
|
|
70
66
|
unwrapErr() {
|
|
71
67
|
return this.error;
|
|
72
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.
|
|
71
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
72
|
+
* @returns The error value contained in this `Err`.
|
|
73
|
+
*/
|
|
74
|
+
value() {
|
|
75
|
+
return this.error;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
79
|
+
* @template U The type of the default value.
|
|
80
|
+
* @param defaultValue The value to return since this is an `Err`.
|
|
81
|
+
* @returns The provided default value.
|
|
82
|
+
*/
|
|
83
|
+
unwrapOr(defaultValue) {
|
|
84
|
+
return defaultValue;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
88
|
+
* @template U The type of the default value.
|
|
89
|
+
* @param fn The function to compute the default value.
|
|
90
|
+
* @returns The result of calling the provided function with the error.
|
|
91
|
+
*/
|
|
92
|
+
unwrapOrElse(fn) {
|
|
93
|
+
return fn(this.error);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.
|
|
97
|
+
* @template T The type of the alternative success value.
|
|
98
|
+
* @template F The type of the alternative error.
|
|
99
|
+
* @param fn The function to compute the alternative result.
|
|
100
|
+
* @returns The result of calling the provided function with the error.
|
|
101
|
+
*/
|
|
102
|
+
orElse(fn) {
|
|
103
|
+
return fn(this.error);
|
|
104
|
+
}
|
|
73
105
|
/**
|
|
74
106
|
* Converts `Result` type to `Option` type.
|
|
75
107
|
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
@@ -83,10 +115,10 @@ var Err = class _Err extends Error {
|
|
|
83
115
|
var Ok = class _Ok {
|
|
84
116
|
/**
|
|
85
117
|
* Creates a new `Ok` instance with the given value.
|
|
86
|
-
* @param
|
|
118
|
+
* @param _value The value to wrap in the `Ok` instance.
|
|
87
119
|
*/
|
|
88
|
-
constructor(
|
|
89
|
-
this.
|
|
120
|
+
constructor(_value) {
|
|
121
|
+
this._value = _value;
|
|
90
122
|
}
|
|
91
123
|
/**
|
|
92
124
|
* Checks if this result is an `Ok`.
|
|
@@ -107,7 +139,15 @@ var Ok = class _Ok {
|
|
|
107
139
|
* @returns The value contained in this `Ok`.
|
|
108
140
|
*/
|
|
109
141
|
unwrap() {
|
|
110
|
-
return this.
|
|
142
|
+
return this._value;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.
|
|
146
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
147
|
+
* @returns The value contained in this `Ok`.
|
|
148
|
+
*/
|
|
149
|
+
value() {
|
|
150
|
+
return this._value;
|
|
111
151
|
}
|
|
112
152
|
/**
|
|
113
153
|
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
@@ -116,25 +156,24 @@ var Ok = class _Ok {
|
|
|
116
156
|
* @returns A new `Ok` containing the transformed value.
|
|
117
157
|
*/
|
|
118
158
|
map(fn) {
|
|
119
|
-
return new _Ok(fn(this.
|
|
159
|
+
return new _Ok(fn(this._value));
|
|
120
160
|
}
|
|
121
161
|
/**
|
|
122
162
|
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
123
163
|
* @template U The type of the value in the resulting `Result`.
|
|
164
|
+
* @template E The type of the error in the resulting `Result`.
|
|
124
165
|
* @param fn The transformation function to apply to the value.
|
|
125
166
|
* @returns The result of applying the transformation function.
|
|
126
167
|
*/
|
|
127
168
|
flatMap(fn) {
|
|
128
|
-
return fn(this.
|
|
169
|
+
return fn(this._value);
|
|
129
170
|
}
|
|
130
171
|
/**
|
|
131
172
|
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
132
|
-
* @template U The type of the error (ignored for `Ok`).
|
|
173
|
+
* @template U The type of the error (ignored for `Ok`). Can be any type.
|
|
133
174
|
* @param _fn The mapping function for errors (not used).
|
|
134
175
|
* @returns The original `Ok` instance.
|
|
135
176
|
*/
|
|
136
|
-
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
|
|
137
|
-
// return this;
|
|
138
177
|
mapErr(_fn) {
|
|
139
178
|
return this;
|
|
140
179
|
}
|
|
@@ -145,15 +184,39 @@ var Ok = class _Ok {
|
|
|
145
184
|
unwrapErr() {
|
|
146
185
|
throw new Error("Called unwrapErr on an Ok value");
|
|
147
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
189
|
+
* @template U The type of the default value.
|
|
190
|
+
* @param defaultValue The value to return if this is an `Err`.
|
|
191
|
+
* @returns The value contained in this `Ok`.
|
|
192
|
+
*/
|
|
193
|
+
unwrapOr(defaultValue) {
|
|
194
|
+
return this._value;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
198
|
+
* @template U The type of the default value.
|
|
199
|
+
* @param fn The function to compute the default value.
|
|
200
|
+
* @returns The value contained in this `Ok`.
|
|
201
|
+
*/
|
|
202
|
+
unwrapOrElse(fn) {
|
|
203
|
+
return this._value;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.
|
|
207
|
+
* @template F The type of the alternative error.
|
|
208
|
+
* @param fn The function to compute the alternative result.
|
|
209
|
+
* @returns The original `Ok` instance.
|
|
210
|
+
*/
|
|
211
|
+
orElse(fn) {
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
148
214
|
/**
|
|
149
215
|
* Converts `Result` type to `Option` type.
|
|
150
216
|
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
151
217
|
*/
|
|
152
218
|
ok() {
|
|
153
|
-
return
|
|
154
|
-
Ok: (v) => Some(v),
|
|
155
|
-
Err: (_) => None()
|
|
156
|
-
});
|
|
219
|
+
return Some(this._value);
|
|
157
220
|
}
|
|
158
221
|
};
|
|
159
222
|
|
|
@@ -166,5 +229,8 @@ function Err2(error) {
|
|
|
166
229
|
}
|
|
167
230
|
global.Ok = Ok2;
|
|
168
231
|
global.Err = Err2;
|
|
232
|
+
|
|
233
|
+
exports.Err = Err2;
|
|
234
|
+
exports.Ok = Ok2;
|
|
169
235
|
//# sourceMappingURL=index.cjs.map
|
|
170
236
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/result/__internal__/return-types/err.ts","../../src/result/__internal__/return-types/ok.ts","../../src/result/result.ts"],"names":["Ok","Err"],"mappings":";;;AAOO,IAAM,GAAA,GAAN,MAAM,IAAA,SACH,KAEV,CAAA;AAAA,EACU,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY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AAKD,GAAAA,GAAAA;AAEpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["import type { Ok } from \"./ok\";\nimport type { ResultDefinition } from \"./result\";\n\n/**\n * Represents a failed result (`Err`) that contains an error value.\n * @template E The type of the error contained in this `Err`.\n */\nexport class Err<E extends Error>\n extends Error\n implements ResultDefinition<never, E>\n{\n private error: E;\n /**\n * Creates a new `Err` instance with the given error value.\n * @param error The error to wrap in the `Err` instance.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\n Object.setPrototypeOf(this, Err.prototype);\n\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, Err);\n }\n }\n\n /**\n * Checks if this result is an `Ok`.\n * @returns `false` because this is an `Err`.\n */\n isOk(): this is Ok<never> {\n return false;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `true` because this is an `Err`.\n */\n isErr(): this is Err<E extends Error ? E : Error> {\n return true;\n }\n\n /**\n * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n * @throws An error because `unwrap` is called on an `Err`.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on an Err value\");\n }\n\n /**\n * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n * @template U The type of the value (ignored for `Err`).\n * @param _fn The mapping function for values (not used).\n * @returns The original `Err` instance.\n */\n map<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n * @template U The type of the transformed error.\n * @param fn The transformation function to apply to the error value.\n * @returns A new `Err` containing the transformed error.\n */\n mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U> {\n return new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n * @template U The type of the value in the resulting `Result`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Retrieves the error value contained in this `Err`.\n * @returns The error value contained in this `Err`.\n */\n unwrapErr(): E {\n return this.error;\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return None();\n }\n}\n","import type { ResultDefinition } from \"./result\";\nimport type { Err } from \"./err\";\n\n/**\n * Represents a successful result (`Ok`) that contains a value.\n * @template T The type of the value contained in this `Ok`.\n */\nexport class Ok<T> implements ResultDefinition<T, never> {\n /**\n * Creates a new `Ok` instance with the given value.\n * @param value The value to wrap in the `Ok` instance.\n */\n constructor(private value: T) {}\n /**\n * Checks if this result is an `Ok`.\n * @returns `true` because this is an `Ok`.\n */\n isOk(): this is Ok<T> {\n return true;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `false` because this is an `Ok`.\n */\n isErr(): this is Err<never> {\n return false;\n }\n\n /**\n * Retrieves the value contained in this `Ok`.\n * @returns The value contained in this `Ok`.\n */\n unwrap(): T {\n return this.value;\n }\n\n /**\n * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Ok` containing the transformed value.\n */\n map<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n return new Ok(fn(this.value)) as ResultDefinition<U, never>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n * @template U The type of the value in the resulting `Result`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\n return fn(this.value);\n }\n\n /**\n * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n * @template U The type of the error (ignored for `Ok`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U> {\n return this as unknown as ResultDefinition<T, U>;\n }\n\n /**\n * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n * @throws An error because `unwrapErr` is called on an `Ok`.\n */\n unwrapErr(): never {\n throw new Error(\"Called unwrapErr on an Ok value\");\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return match(this, {\n Ok: (v) => Some(v),\n Err: (_) => None(),\n });\n }\n}\n","import {\n Err as ErrType,\n Ok as OkType,\n type Result,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Ok` instance, representing a successful result.\n * @template T The type of the value contained in the `Ok`.\n * @param value The value to wrap in the `Ok` instance.\n * @returns An `Ok` instance containing the given value.\n * @example\n * const result = Ok(42);\n * console.log(result.isOk()); // true\n * console.log(result.unwrap()); // 42\n */\nfunction Ok<T>(value: T): OkType<T> {\n return new OkType(value);\n}\n\n/**\n * Creates a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport type { Err, Ok, Result, ErrType, OkType };\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/result/__internal__/return-types/err.ts","../../src/result/__internal__/return-types/ok.ts","../../src/result/result.ts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AO,IAAK,CAAA,MAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,aAAgB,EAA4B,EAAA;AAC1C,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAU,EAAsE,EAAA;AAC9E,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,EAAK,GAAA;AACH,IAAO,OAAA,IAAA,CAAK,KAAK,MAAM,CAAA;AAAA;AAE3B,CAAA;;;AC5GA,SAASA,IAAM,KAAqB,EAAA;AAClC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACzB;AAKA,SAASC,KAAO,KAAsB,EAAA;AACpC,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AAC1B;AAGC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AAEpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["import type { Ok } from \"./ok\";\nimport type { ResultDefinition } from \"./result\";\n\n/**\n * Represents a failed result (`Err`) that contains an error value.\n * @template E The type of the error contained in this `Err`. Can be any type (Error, string, enum, etc).\n */\nexport class Err<E> implements ResultDefinition<never, E> {\n private error: E;\n /**\n * Creates a new `Err` instance with the given error value.\n * @param error The error to wrap in the `Err` instance. Can be any type.\n */\n constructor(error: E) {\n // Store error as-is - conversion is handled by factory function\n this.error = error;\n // Object.setPrototypeOf(this, Err.prototype);\n //\n // if (Error.captureStackTrace) {\n // Error.captureStackTrace(this, Err);\n // }\n }\n\n /**\n * Checks if this result is an `Ok`.\n * @returns `false` because this is an `Err`.\n */\n isOk(): this is Ok<never> {\n return false;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `true` because this is an `Err`.\n */\n isErr(): this is Err<E> {\n return true;\n }\n\n /**\n * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n * @throws An error because `unwrap` is called on an `Err`.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on an Err value\");\n }\n\n /**\n * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n * @template U The type of the value (ignored for `Err`).\n * @param _fn The mapping function for values (not used).\n * @returns The original `Err` instance.\n */\n map<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n * @template U The type of the transformed error. Can be any type.\n * @param fn The transformation function to apply to the error value.\n * @returns A new `Err` containing the transformed error.\n */\n mapErr<U>(fn: (err: E) => U): ResultDefinition<never, U> {\n return new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n * @template U The type of the value in the resulting `Result`.\n * @template F The type of the error in the resulting `Result`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance cast to the new error type.\n */\n flatMap<U, F = E>(\n _fn: (value: never) => ResultDefinition<U, F>,\n ): ResultDefinition<U, E> {\n return this as unknown as ResultDefinition<U, E>;\n }\n\n /**\n * Retrieves the error value contained in this `Err`.\n * @returns The error value contained in this `Err`.\n */\n unwrapErr(): E {\n return this.error;\n }\n\n /**\n * Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The error value contained in this `Err`.\n */\n value(): E {\n return this.error;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return since this is an `Err`.\n * @returns The provided default value.\n */\n unwrapOr<U>(defaultValue: U): U {\n return defaultValue;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function with the error.\n */\n unwrapOrElse<U>(fn: (error: E) => U): U {\n return fn(this.error);\n }\n\n /**\n * Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.\n * @template T The type of the alternative success value.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The result of calling the provided function with the error.\n */\n orElse<T, F>(\n fn: (error: E) => ResultDefinition<T, F>,\n ): ResultDefinition<T, F> {\n return fn(this.error);\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return None();\n }\n}\n","import type { ResultDefinition } from \"./result\";\nimport type { Err } from \"./err\";\n\n/**\n * Represents a successful result (`Ok`) that contains a value.\n * @template T The type of the value contained in this `Ok`.\n */\nexport class Ok<T> implements ResultDefinition<T, never> {\n /**\n * Creates a new `Ok` instance with the given value.\n * @param _value The value to wrap in the `Ok` instance.\n */\n constructor(private _value: T) {}\n /**\n * Checks if this result is an `Ok`.\n * @returns `true` because this is an `Ok`.\n */\n isOk(): this is Ok<T> {\n return true;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `false` because this is an `Ok`.\n */\n isErr(): this is Err<never> {\n return false;\n }\n\n /**\n * Retrieves the value contained in this `Ok`.\n * @returns The value contained in this `Ok`.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The value contained in this `Ok`.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Ok` containing the transformed value.\n */\n map<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n return new Ok(fn(this._value)) as unknown as ResultDefinition<U, never>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n * @template U The type of the value in the resulting `Result`.\n * @template E The type of the error in the resulting `Result`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U, E = never>(\n fn: (value: T) => ResultDefinition<U, E>,\n ): ResultDefinition<U, E> {\n return fn(this._value);\n }\n\n /**\n * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n * @template U The type of the error (ignored for `Ok`). Can be any type.\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n mapErr<U>(_fn: (err: never) => U): ResultDefinition<T, U> {\n return this as unknown as ResultDefinition<T, U>;\n }\n\n /**\n * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n * @throws An error because `unwrapErr` is called on an `Ok`.\n */\n unwrapErr(): never {\n throw new Error(\"Called unwrapErr on an Ok value\");\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return if this is an `Err`.\n * @returns The value contained in this `Ok`.\n */\n unwrapOr<U>(defaultValue: U): T {\n return this._value;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The value contained in this `Ok`.\n */\n unwrapOrElse<U>(fn: (error: never) => U): T {\n return this._value;\n }\n\n /**\n * Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The original `Ok` instance.\n */\n orElse<F>(fn: (error: never) => ResultDefinition<T, F>): ResultDefinition<T, F> {\n return this as unknown as ResultDefinition<T, F>;\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return Some(this._value);\n }\n}\n","import {\n Err as ErrType,\n Ok as OkType,\n type Result,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Ok` instance, representing a successful result.\n * @template T The type of the value contained in the `Ok`.\n * @param value The value to wrap in the `Ok` instance.\n * @returns An `Ok` instance containing the given value.\n * @example\n * const result = Ok(42);\n * console.log(result.isOk()); // true\n * console.log(result.unwrap()); // 42\n */\nfunction Ok<T>(value: T): OkType<T> {\n return new OkType(value);\n}\n\n/**\n * Creates an Err - strings are converted to Error, everything else preserved\n */\nfunction Err<E>(error: E): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n"]}
|
package/dist/result/index.d.cts
CHANGED
package/dist/result/index.d.ts
CHANGED