@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
package/dist/result/result.js
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
// src/result/__internal__/return-types/err.ts
|
|
2
|
-
var Err = class _Err
|
|
2
|
+
var Err = class _Err {
|
|
3
3
|
error;
|
|
4
4
|
/**
|
|
5
5
|
* Creates a new `Err` instance with the given error value.
|
|
6
|
-
* @param error The error to wrap in the `Err` instance.
|
|
6
|
+
* @param error The error to wrap in the `Err` instance. Can be any type.
|
|
7
7
|
*/
|
|
8
8
|
constructor(error) {
|
|
9
|
-
|
|
10
|
-
this.error = typeof error === "string" ? new Error(error) : error;
|
|
11
|
-
Object.setPrototypeOf(this, _Err.prototype);
|
|
12
|
-
if (Error.captureStackTrace) {
|
|
13
|
-
Error.captureStackTrace(this, _Err);
|
|
14
|
-
}
|
|
9
|
+
this.error = error;
|
|
15
10
|
}
|
|
16
11
|
/**
|
|
17
12
|
* Checks if this result is an `Ok`.
|
|
@@ -45,7 +40,7 @@ var Err = class _Err extends Error {
|
|
|
45
40
|
}
|
|
46
41
|
/**
|
|
47
42
|
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
48
|
-
* @template U The type of the transformed error.
|
|
43
|
+
* @template U The type of the transformed error. Can be any type.
|
|
49
44
|
* @param fn The transformation function to apply to the error value.
|
|
50
45
|
* @returns A new `Err` containing the transformed error.
|
|
51
46
|
*/
|
|
@@ -55,8 +50,9 @@ var Err = class _Err extends Error {
|
|
|
55
50
|
/**
|
|
56
51
|
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
57
52
|
* @template U The type of the value in the resulting `Result`.
|
|
53
|
+
* @template F The type of the error in the resulting `Result`.
|
|
58
54
|
* @param _fn The transformation function (ignored in this implementation).
|
|
59
|
-
* @returns The original `Err` instance.
|
|
55
|
+
* @returns The original `Err` instance cast to the new error type.
|
|
60
56
|
*/
|
|
61
57
|
flatMap(_fn) {
|
|
62
58
|
return this;
|
|
@@ -68,6 +64,42 @@ var Err = class _Err extends Error {
|
|
|
68
64
|
unwrapErr() {
|
|
69
65
|
return this.error;
|
|
70
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.
|
|
69
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
70
|
+
* @returns The error value contained in this `Err`.
|
|
71
|
+
*/
|
|
72
|
+
value() {
|
|
73
|
+
return this.error;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
77
|
+
* @template U The type of the default value.
|
|
78
|
+
* @param defaultValue The value to return since this is an `Err`.
|
|
79
|
+
* @returns The provided default value.
|
|
80
|
+
*/
|
|
81
|
+
unwrapOr(defaultValue) {
|
|
82
|
+
return defaultValue;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
86
|
+
* @template U The type of the default value.
|
|
87
|
+
* @param fn The function to compute the default value.
|
|
88
|
+
* @returns The result of calling the provided function with the error.
|
|
89
|
+
*/
|
|
90
|
+
unwrapOrElse(fn) {
|
|
91
|
+
return fn(this.error);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.
|
|
95
|
+
* @template T The type of the alternative success value.
|
|
96
|
+
* @template F The type of the alternative error.
|
|
97
|
+
* @param fn The function to compute the alternative result.
|
|
98
|
+
* @returns The result of calling the provided function with the error.
|
|
99
|
+
*/
|
|
100
|
+
orElse(fn) {
|
|
101
|
+
return fn(this.error);
|
|
102
|
+
}
|
|
71
103
|
/**
|
|
72
104
|
* Converts `Result` type to `Option` type.
|
|
73
105
|
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
@@ -81,10 +113,10 @@ var Err = class _Err extends Error {
|
|
|
81
113
|
var Ok = class _Ok {
|
|
82
114
|
/**
|
|
83
115
|
* Creates a new `Ok` instance with the given value.
|
|
84
|
-
* @param
|
|
116
|
+
* @param _value The value to wrap in the `Ok` instance.
|
|
85
117
|
*/
|
|
86
|
-
constructor(
|
|
87
|
-
this.
|
|
118
|
+
constructor(_value) {
|
|
119
|
+
this._value = _value;
|
|
88
120
|
}
|
|
89
121
|
/**
|
|
90
122
|
* Checks if this result is an `Ok`.
|
|
@@ -105,7 +137,15 @@ var Ok = class _Ok {
|
|
|
105
137
|
* @returns The value contained in this `Ok`.
|
|
106
138
|
*/
|
|
107
139
|
unwrap() {
|
|
108
|
-
return this.
|
|
140
|
+
return this._value;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.
|
|
144
|
+
* Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).
|
|
145
|
+
* @returns The value contained in this `Ok`.
|
|
146
|
+
*/
|
|
147
|
+
value() {
|
|
148
|
+
return this._value;
|
|
109
149
|
}
|
|
110
150
|
/**
|
|
111
151
|
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
@@ -114,25 +154,24 @@ var Ok = class _Ok {
|
|
|
114
154
|
* @returns A new `Ok` containing the transformed value.
|
|
115
155
|
*/
|
|
116
156
|
map(fn) {
|
|
117
|
-
return new _Ok(fn(this.
|
|
157
|
+
return new _Ok(fn(this._value));
|
|
118
158
|
}
|
|
119
159
|
/**
|
|
120
160
|
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
121
161
|
* @template U The type of the value in the resulting `Result`.
|
|
162
|
+
* @template E The type of the error in the resulting `Result`.
|
|
122
163
|
* @param fn The transformation function to apply to the value.
|
|
123
164
|
* @returns The result of applying the transformation function.
|
|
124
165
|
*/
|
|
125
166
|
flatMap(fn) {
|
|
126
|
-
return fn(this.
|
|
167
|
+
return fn(this._value);
|
|
127
168
|
}
|
|
128
169
|
/**
|
|
129
170
|
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
130
|
-
* @template U The type of the error (ignored for `Ok`).
|
|
171
|
+
* @template U The type of the error (ignored for `Ok`). Can be any type.
|
|
131
172
|
* @param _fn The mapping function for errors (not used).
|
|
132
173
|
* @returns The original `Ok` instance.
|
|
133
174
|
*/
|
|
134
|
-
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
|
|
135
|
-
// return this;
|
|
136
175
|
mapErr(_fn) {
|
|
137
176
|
return this;
|
|
138
177
|
}
|
|
@@ -143,15 +182,39 @@ var Ok = class _Ok {
|
|
|
143
182
|
unwrapErr() {
|
|
144
183
|
throw new Error("Called unwrapErr on an Ok value");
|
|
145
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Returns the contained value if `Ok`, otherwise returns the provided default value.
|
|
187
|
+
* @template U The type of the default value.
|
|
188
|
+
* @param defaultValue The value to return if this is an `Err`.
|
|
189
|
+
* @returns The value contained in this `Ok`.
|
|
190
|
+
*/
|
|
191
|
+
unwrapOr(defaultValue) {
|
|
192
|
+
return this._value;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.
|
|
196
|
+
* @template U The type of the default value.
|
|
197
|
+
* @param fn The function to compute the default value.
|
|
198
|
+
* @returns The value contained in this `Ok`.
|
|
199
|
+
*/
|
|
200
|
+
unwrapOrElse(fn) {
|
|
201
|
+
return this._value;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.
|
|
205
|
+
* @template F The type of the alternative error.
|
|
206
|
+
* @param fn The function to compute the alternative result.
|
|
207
|
+
* @returns The original `Ok` instance.
|
|
208
|
+
*/
|
|
209
|
+
orElse(fn) {
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
146
212
|
/**
|
|
147
213
|
* Converts `Result` type to `Option` type.
|
|
148
214
|
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
149
215
|
*/
|
|
150
216
|
ok() {
|
|
151
|
-
return
|
|
152
|
-
Ok: (v) => Some(v),
|
|
153
|
-
Err: (_) => None()
|
|
154
|
-
});
|
|
217
|
+
return Some(this._value);
|
|
155
218
|
}
|
|
156
219
|
};
|
|
157
220
|
|
|
@@ -164,5 +227,7 @@ function Err2(error) {
|
|
|
164
227
|
}
|
|
165
228
|
global.Ok = Ok2;
|
|
166
229
|
global.Err = Err2;
|
|
230
|
+
|
|
231
|
+
export { Err2 as Err, Err as ErrType, Ok2 as Ok, Ok as OkType };
|
|
167
232
|
//# sourceMappingURL=result.js.map
|
|
168
233
|
//# sourceMappingURL=result.js.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":"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","file":"result.js","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"],"names":["Ok","Err"],"mappings":"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","file":"result.js","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/types/globals.d.cts
CHANGED
|
@@ -1,157 +1,39 @@
|
|
|
1
|
-
import { O as Option,
|
|
2
|
-
|
|
3
|
-
type OkType<T> = [T] extends [never] ? never : T;
|
|
4
|
-
type ErrType<E> = [E] extends [never] ? never : E;
|
|
5
|
-
/**
|
|
6
|
-
* Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
|
|
7
|
-
*/
|
|
8
|
-
type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
|
|
9
|
-
/**
|
|
10
|
-
* Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
|
|
11
|
-
*/
|
|
12
|
-
interface ResultDefinition<T = never, E = never> {
|
|
13
|
-
isOk(): this is Ok<T>;
|
|
14
|
-
isErr(): this is Err<E extends Error ? E : Error>;
|
|
15
|
-
unwrap(): T;
|
|
16
|
-
unwrapErr(): E;
|
|
17
|
-
map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
|
|
18
|
-
flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
19
|
-
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
|
|
20
|
-
ok(): Option<T>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Represents a successful result (`Ok`) that contains a value.
|
|
25
|
-
* @template T The type of the value contained in this `Ok`.
|
|
26
|
-
*/
|
|
27
|
-
declare class Ok<T> implements ResultDefinition<T, never> {
|
|
28
|
-
private value;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new `Ok` instance with the given value.
|
|
31
|
-
* @param value The value to wrap in the `Ok` instance.
|
|
32
|
-
*/
|
|
33
|
-
constructor(value: T);
|
|
34
|
-
/**
|
|
35
|
-
* Checks if this result is an `Ok`.
|
|
36
|
-
* @returns `true` because this is an `Ok`.
|
|
37
|
-
*/
|
|
38
|
-
isOk(): this is Ok<T>;
|
|
39
|
-
/**
|
|
40
|
-
* Checks if this result is an `Err`.
|
|
41
|
-
* @returns `false` because this is an `Ok`.
|
|
42
|
-
*/
|
|
43
|
-
isErr(): this is Err<never>;
|
|
44
|
-
/**
|
|
45
|
-
* Retrieves the value contained in this `Ok`.
|
|
46
|
-
* @returns The value contained in this `Ok`.
|
|
47
|
-
*/
|
|
48
|
-
unwrap(): T;
|
|
49
|
-
/**
|
|
50
|
-
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
51
|
-
* @template U The type of the transformed value.
|
|
52
|
-
* @param fn The transformation function to apply to the value.
|
|
53
|
-
* @returns A new `Ok` containing the transformed value.
|
|
54
|
-
*/
|
|
55
|
-
map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
|
|
56
|
-
/**
|
|
57
|
-
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
58
|
-
* @template U The type of the value in the resulting `Result`.
|
|
59
|
-
* @param fn The transformation function to apply to the value.
|
|
60
|
-
* @returns The result of applying the transformation function.
|
|
61
|
-
*/
|
|
62
|
-
flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
|
|
63
|
-
/**
|
|
64
|
-
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
65
|
-
* @template U The type of the error (ignored for `Ok`).
|
|
66
|
-
* @param _fn The mapping function for errors (not used).
|
|
67
|
-
* @returns The original `Ok` instance.
|
|
68
|
-
*/
|
|
69
|
-
mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
|
|
70
|
-
/**
|
|
71
|
-
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
72
|
-
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
73
|
-
*/
|
|
74
|
-
unwrapErr(): never;
|
|
75
|
-
/**
|
|
76
|
-
* Converts `Result` type to `Option` type.
|
|
77
|
-
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
78
|
-
*/
|
|
79
|
-
ok(): Option<T>;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Represents a failed result (`Err`) that contains an error value.
|
|
84
|
-
* @template E The type of the error contained in this `Err`.
|
|
85
|
-
*/
|
|
86
|
-
declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
|
|
87
|
-
private error;
|
|
88
|
-
/**
|
|
89
|
-
* Creates a new `Err` instance with the given error value.
|
|
90
|
-
* @param error The error to wrap in the `Err` instance.
|
|
91
|
-
*/
|
|
92
|
-
constructor(error: E | string);
|
|
93
|
-
/**
|
|
94
|
-
* Checks if this result is an `Ok`.
|
|
95
|
-
* @returns `false` because this is an `Err`.
|
|
96
|
-
*/
|
|
97
|
-
isOk(): this is Ok<never>;
|
|
98
|
-
/**
|
|
99
|
-
* Checks if this result is an `Err`.
|
|
100
|
-
* @returns `true` because this is an `Err`.
|
|
101
|
-
*/
|
|
102
|
-
isErr(): this is Err<E extends Error ? E : Error>;
|
|
103
|
-
/**
|
|
104
|
-
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
105
|
-
* @throws An error because `unwrap` is called on an `Err`.
|
|
106
|
-
*/
|
|
107
|
-
unwrap(): never;
|
|
108
|
-
/**
|
|
109
|
-
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
110
|
-
* @template U The type of the value (ignored for `Err`).
|
|
111
|
-
* @param _fn The mapping function for values (not used).
|
|
112
|
-
* @returns The original `Err` instance.
|
|
113
|
-
*/
|
|
114
|
-
map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
|
|
115
|
-
/**
|
|
116
|
-
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
117
|
-
* @template U The type of the transformed error.
|
|
118
|
-
* @param fn The transformation function to apply to the error value.
|
|
119
|
-
* @returns A new `Err` containing the transformed error.
|
|
120
|
-
*/
|
|
121
|
-
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
|
|
122
|
-
/**
|
|
123
|
-
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
124
|
-
* @template U The type of the value in the resulting `Result`.
|
|
125
|
-
* @param _fn The transformation function (ignored in this implementation).
|
|
126
|
-
* @returns The original `Err` instance.
|
|
127
|
-
*/
|
|
128
|
-
flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
|
|
129
|
-
/**
|
|
130
|
-
* Retrieves the error value contained in this `Err`.
|
|
131
|
-
* @returns The error value contained in this `Err`.
|
|
132
|
-
*/
|
|
133
|
-
unwrapErr(): E;
|
|
134
|
-
/**
|
|
135
|
-
* Converts `Result` type to `Option` type.
|
|
136
|
-
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
137
|
-
*/
|
|
138
|
-
ok(): None;
|
|
139
|
-
}
|
|
1
|
+
import { R as Result, O as Option, a as Ok, E as Err, S as Some, N as None } from '../option-B_KKIecf.cjs';
|
|
140
2
|
|
|
141
3
|
declare global {
|
|
142
|
-
function match<T, E
|
|
4
|
+
export function match<T, E, R>(matcher: Result<T, E>, cases: {
|
|
143
5
|
Ok: (value: T) => R;
|
|
144
6
|
Err: (error: E) => R;
|
|
145
7
|
}): R;
|
|
146
|
-
function match<T, R>(matcher: Option<T>, cases: {
|
|
8
|
+
export function match<T, R>(matcher: Option<T>, cases: {
|
|
147
9
|
Some: (value: T) => R;
|
|
148
10
|
None: () => R;
|
|
149
11
|
}): R;
|
|
150
|
-
function match<T
|
|
12
|
+
export function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
|
|
13
|
+
default: () => R;
|
|
14
|
+
}): R;
|
|
15
|
+
export function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
|
|
16
|
+
export function match<T extends {
|
|
17
|
+
[K in D]: string | number | symbol;
|
|
18
|
+
}, D extends keyof T, R>(matcher: T, cases: {
|
|
19
|
+
[K in T[D]]?: (value: Extract<T, {
|
|
20
|
+
[P in D]: K;
|
|
21
|
+
}>) => R;
|
|
22
|
+
} & {
|
|
23
|
+
default: (value: T) => R;
|
|
24
|
+
}, discriminant: D): R;
|
|
25
|
+
export function match<T extends {
|
|
26
|
+
[K in D]: string | number | symbol;
|
|
27
|
+
}, D extends keyof T, R>(matcher: T, cases: {
|
|
28
|
+
[K in T[D]]: (value: Extract<T, {
|
|
29
|
+
[P in D]: K;
|
|
30
|
+
}>) => R;
|
|
31
|
+
}, discriminant: D): R;
|
|
32
|
+
export function match<T, E>(matcher: Result<T, E>, cases: {
|
|
151
33
|
Ok: (value: T) => Option<T>;
|
|
152
34
|
Err: (error: E) => Option<T>;
|
|
153
35
|
}): Option<T>;
|
|
154
|
-
function match<T, E
|
|
36
|
+
export function match<T, E>(matcher: Option<T>, cases: {
|
|
155
37
|
Some: (value: T) => Result<T, E>;
|
|
156
38
|
None: () => Result<T, E>;
|
|
157
39
|
}): Result<T, E>;
|
|
@@ -176,7 +58,7 @@ declare global {
|
|
|
176
58
|
* console.log(result.isErr()); // true
|
|
177
59
|
* console.log(result.unwrapErr()); // "Something went wrong"
|
|
178
60
|
*/
|
|
179
|
-
function Err<E
|
|
61
|
+
function Err<E>(error: E): Err<E>;
|
|
180
62
|
/**
|
|
181
63
|
* creates a new `Some` instance, representing some value.
|
|
182
64
|
* @template T the type of the value contained in the `Some`.
|
|
@@ -199,5 +81,3 @@ declare global {
|
|
|
199
81
|
*/
|
|
200
82
|
function None(): None;
|
|
201
83
|
}
|
|
202
|
-
|
|
203
|
-
export { Err as E, Ok as O, type Result as R };
|
package/dist/types/globals.d.ts
CHANGED
|
@@ -1,157 +1,39 @@
|
|
|
1
|
-
import { O as Option,
|
|
2
|
-
|
|
3
|
-
type OkType<T> = [T] extends [never] ? never : T;
|
|
4
|
-
type ErrType<E> = [E] extends [never] ? never : E;
|
|
5
|
-
/**
|
|
6
|
-
* Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
|
|
7
|
-
*/
|
|
8
|
-
type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
|
|
9
|
-
/**
|
|
10
|
-
* Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
|
|
11
|
-
*/
|
|
12
|
-
interface ResultDefinition<T = never, E = never> {
|
|
13
|
-
isOk(): this is Ok<T>;
|
|
14
|
-
isErr(): this is Err<E extends Error ? E : Error>;
|
|
15
|
-
unwrap(): T;
|
|
16
|
-
unwrapErr(): E;
|
|
17
|
-
map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
|
|
18
|
-
flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
19
|
-
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
|
|
20
|
-
ok(): Option<T>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Represents a successful result (`Ok`) that contains a value.
|
|
25
|
-
* @template T The type of the value contained in this `Ok`.
|
|
26
|
-
*/
|
|
27
|
-
declare class Ok<T> implements ResultDefinition<T, never> {
|
|
28
|
-
private value;
|
|
29
|
-
/**
|
|
30
|
-
* Creates a new `Ok` instance with the given value.
|
|
31
|
-
* @param value The value to wrap in the `Ok` instance.
|
|
32
|
-
*/
|
|
33
|
-
constructor(value: T);
|
|
34
|
-
/**
|
|
35
|
-
* Checks if this result is an `Ok`.
|
|
36
|
-
* @returns `true` because this is an `Ok`.
|
|
37
|
-
*/
|
|
38
|
-
isOk(): this is Ok<T>;
|
|
39
|
-
/**
|
|
40
|
-
* Checks if this result is an `Err`.
|
|
41
|
-
* @returns `false` because this is an `Ok`.
|
|
42
|
-
*/
|
|
43
|
-
isErr(): this is Err<never>;
|
|
44
|
-
/**
|
|
45
|
-
* Retrieves the value contained in this `Ok`.
|
|
46
|
-
* @returns The value contained in this `Ok`.
|
|
47
|
-
*/
|
|
48
|
-
unwrap(): T;
|
|
49
|
-
/**
|
|
50
|
-
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
51
|
-
* @template U The type of the transformed value.
|
|
52
|
-
* @param fn The transformation function to apply to the value.
|
|
53
|
-
* @returns A new `Ok` containing the transformed value.
|
|
54
|
-
*/
|
|
55
|
-
map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
|
|
56
|
-
/**
|
|
57
|
-
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
58
|
-
* @template U The type of the value in the resulting `Result`.
|
|
59
|
-
* @param fn The transformation function to apply to the value.
|
|
60
|
-
* @returns The result of applying the transformation function.
|
|
61
|
-
*/
|
|
62
|
-
flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
|
|
63
|
-
/**
|
|
64
|
-
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
65
|
-
* @template U The type of the error (ignored for `Ok`).
|
|
66
|
-
* @param _fn The mapping function for errors (not used).
|
|
67
|
-
* @returns The original `Ok` instance.
|
|
68
|
-
*/
|
|
69
|
-
mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
|
|
70
|
-
/**
|
|
71
|
-
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
72
|
-
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
73
|
-
*/
|
|
74
|
-
unwrapErr(): never;
|
|
75
|
-
/**
|
|
76
|
-
* Converts `Result` type to `Option` type.
|
|
77
|
-
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
78
|
-
*/
|
|
79
|
-
ok(): Option<T>;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Represents a failed result (`Err`) that contains an error value.
|
|
84
|
-
* @template E The type of the error contained in this `Err`.
|
|
85
|
-
*/
|
|
86
|
-
declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
|
|
87
|
-
private error;
|
|
88
|
-
/**
|
|
89
|
-
* Creates a new `Err` instance with the given error value.
|
|
90
|
-
* @param error The error to wrap in the `Err` instance.
|
|
91
|
-
*/
|
|
92
|
-
constructor(error: E | string);
|
|
93
|
-
/**
|
|
94
|
-
* Checks if this result is an `Ok`.
|
|
95
|
-
* @returns `false` because this is an `Err`.
|
|
96
|
-
*/
|
|
97
|
-
isOk(): this is Ok<never>;
|
|
98
|
-
/**
|
|
99
|
-
* Checks if this result is an `Err`.
|
|
100
|
-
* @returns `true` because this is an `Err`.
|
|
101
|
-
*/
|
|
102
|
-
isErr(): this is Err<E extends Error ? E : Error>;
|
|
103
|
-
/**
|
|
104
|
-
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
105
|
-
* @throws An error because `unwrap` is called on an `Err`.
|
|
106
|
-
*/
|
|
107
|
-
unwrap(): never;
|
|
108
|
-
/**
|
|
109
|
-
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
110
|
-
* @template U The type of the value (ignored for `Err`).
|
|
111
|
-
* @param _fn The mapping function for values (not used).
|
|
112
|
-
* @returns The original `Err` instance.
|
|
113
|
-
*/
|
|
114
|
-
map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
|
|
115
|
-
/**
|
|
116
|
-
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
117
|
-
* @template U The type of the transformed error.
|
|
118
|
-
* @param fn The transformation function to apply to the error value.
|
|
119
|
-
* @returns A new `Err` containing the transformed error.
|
|
120
|
-
*/
|
|
121
|
-
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
|
|
122
|
-
/**
|
|
123
|
-
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
124
|
-
* @template U The type of the value in the resulting `Result`.
|
|
125
|
-
* @param _fn The transformation function (ignored in this implementation).
|
|
126
|
-
* @returns The original `Err` instance.
|
|
127
|
-
*/
|
|
128
|
-
flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
|
|
129
|
-
/**
|
|
130
|
-
* Retrieves the error value contained in this `Err`.
|
|
131
|
-
* @returns The error value contained in this `Err`.
|
|
132
|
-
*/
|
|
133
|
-
unwrapErr(): E;
|
|
134
|
-
/**
|
|
135
|
-
* Converts `Result` type to `Option` type.
|
|
136
|
-
* @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
|
|
137
|
-
*/
|
|
138
|
-
ok(): None;
|
|
139
|
-
}
|
|
1
|
+
import { R as Result, O as Option, a as Ok, E as Err, S as Some, N as None } from '../option-B_KKIecf.js';
|
|
140
2
|
|
|
141
3
|
declare global {
|
|
142
|
-
function match<T, E
|
|
4
|
+
export function match<T, E, R>(matcher: Result<T, E>, cases: {
|
|
143
5
|
Ok: (value: T) => R;
|
|
144
6
|
Err: (error: E) => R;
|
|
145
7
|
}): R;
|
|
146
|
-
function match<T, R>(matcher: Option<T>, cases: {
|
|
8
|
+
export function match<T, R>(matcher: Option<T>, cases: {
|
|
147
9
|
Some: (value: T) => R;
|
|
148
10
|
None: () => R;
|
|
149
11
|
}): R;
|
|
150
|
-
function match<T
|
|
12
|
+
export function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
|
|
13
|
+
default: () => R;
|
|
14
|
+
}): R;
|
|
15
|
+
export function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
|
|
16
|
+
export function match<T extends {
|
|
17
|
+
[K in D]: string | number | symbol;
|
|
18
|
+
}, D extends keyof T, R>(matcher: T, cases: {
|
|
19
|
+
[K in T[D]]?: (value: Extract<T, {
|
|
20
|
+
[P in D]: K;
|
|
21
|
+
}>) => R;
|
|
22
|
+
} & {
|
|
23
|
+
default: (value: T) => R;
|
|
24
|
+
}, discriminant: D): R;
|
|
25
|
+
export function match<T extends {
|
|
26
|
+
[K in D]: string | number | symbol;
|
|
27
|
+
}, D extends keyof T, R>(matcher: T, cases: {
|
|
28
|
+
[K in T[D]]: (value: Extract<T, {
|
|
29
|
+
[P in D]: K;
|
|
30
|
+
}>) => R;
|
|
31
|
+
}, discriminant: D): R;
|
|
32
|
+
export function match<T, E>(matcher: Result<T, E>, cases: {
|
|
151
33
|
Ok: (value: T) => Option<T>;
|
|
152
34
|
Err: (error: E) => Option<T>;
|
|
153
35
|
}): Option<T>;
|
|
154
|
-
function match<T, E
|
|
36
|
+
export function match<T, E>(matcher: Option<T>, cases: {
|
|
155
37
|
Some: (value: T) => Result<T, E>;
|
|
156
38
|
None: () => Result<T, E>;
|
|
157
39
|
}): Result<T, E>;
|
|
@@ -176,7 +58,7 @@ declare global {
|
|
|
176
58
|
* console.log(result.isErr()); // true
|
|
177
59
|
* console.log(result.unwrapErr()); // "Something went wrong"
|
|
178
60
|
*/
|
|
179
|
-
function Err<E
|
|
61
|
+
function Err<E>(error: E): Err<E>;
|
|
180
62
|
/**
|
|
181
63
|
* creates a new `Some` instance, representing some value.
|
|
182
64
|
* @template T the type of the value contained in the `Some`.
|
|
@@ -199,5 +81,3 @@ declare global {
|
|
|
199
81
|
*/
|
|
200
82
|
function None(): None;
|
|
201
83
|
}
|
|
202
|
-
|
|
203
|
-
export { Err as E, Ok as O, type Result as R };
|