@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.
Files changed (44) hide show
  1. package/README.md +593 -253
  2. package/dist/helpers/match.cjs +28 -5
  3. package/dist/helpers/match.cjs.map +1 -1
  4. package/dist/helpers/match.d.cts +30 -3
  5. package/dist/helpers/match.d.ts +30 -3
  6. package/dist/helpers/match.js +28 -5
  7. package/dist/helpers/match.js.map +1 -1
  8. package/dist/index.cjs +312 -151
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +2 -2
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +309 -150
  13. package/dist/index.js.map +1 -1
  14. package/dist/option/index.cjs +321 -18
  15. package/dist/option/index.cjs.map +1 -1
  16. package/dist/option/index.d.cts +1 -1
  17. package/dist/option/index.d.ts +1 -1
  18. package/dist/option/index.js +321 -18
  19. package/dist/option/index.js.map +1 -1
  20. package/dist/option/option.cjs +321 -18
  21. package/dist/option/option.cjs.map +1 -1
  22. package/dist/option/option.d.cts +5 -4
  23. package/dist/option/option.d.ts +5 -4
  24. package/dist/option/option.js +321 -18
  25. package/dist/option/option.js.map +1 -1
  26. package/dist/option-B_KKIecf.d.cts +352 -0
  27. package/dist/option-B_KKIecf.d.ts +352 -0
  28. package/dist/result/index.cjs +89 -23
  29. package/dist/result/index.cjs.map +1 -1
  30. package/dist/result/index.d.cts +1 -2
  31. package/dist/result/index.d.ts +1 -2
  32. package/dist/result/index.js +88 -23
  33. package/dist/result/index.js.map +1 -1
  34. package/dist/result/result.cjs +91 -23
  35. package/dist/result/result.cjs.map +1 -1
  36. package/dist/result/result.d.cts +4 -12
  37. package/dist/result/result.d.ts +4 -12
  38. package/dist/result/result.js +88 -23
  39. package/dist/result/result.js.map +1 -1
  40. package/dist/types/globals.d.cts +26 -146
  41. package/dist/types/globals.d.ts +26 -146
  42. package/package.json +72 -71
  43. package/dist/option-DpT8KCGE.d.cts +0 -96
  44. 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 };