@consolidados/results 0.1.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/.release-it.json +20 -0
- package/README.md +361 -0
- package/dist/err-B7LUEZ0f.d.cts +128 -0
- package/dist/err-B7LUEZ0f.d.ts +128 -0
- package/dist/helpers/match.cjs +27 -0
- package/dist/helpers/match.cjs.map +1 -0
- package/dist/helpers/match.d.cts +13 -0
- package/dist/helpers/match.d.ts +13 -0
- package/dist/helpers/match.js +25 -0
- package/dist/helpers/match.js.map +1 -0
- package/dist/index.cjs +301 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +295 -0
- package/dist/index.js.map +1 -0
- package/dist/option/index.cjs +126 -0
- package/dist/option/index.cjs.map +1 -0
- package/dist/option/index.d.cts +2 -0
- package/dist/option/index.d.ts +2 -0
- package/dist/option/index.js +123 -0
- package/dist/option/index.js.map +1 -0
- package/dist/option/option.cjs +126 -0
- package/dist/option/option.cjs.map +1 -0
- package/dist/option/option.d.cts +25 -0
- package/dist/option/option.d.ts +25 -0
- package/dist/option/option.js +123 -0
- package/dist/option/option.js.map +1 -0
- package/dist/option-DpT8KCGE.d.cts +96 -0
- package/dist/option-DpT8KCGE.d.ts +96 -0
- package/dist/result/index.cjs +156 -0
- package/dist/result/index.cjs.map +1 -0
- package/dist/result/index.d.cts +2 -0
- package/dist/result/index.d.ts +2 -0
- package/dist/result/index.js +153 -0
- package/dist/result/index.js.map +1 -0
- package/dist/result/result.cjs +158 -0
- package/dist/result/result.cjs.map +1 -0
- package/dist/result/result.d.cts +27 -0
- package/dist/result/result.d.ts +27 -0
- package/dist/result/result.js +153 -0
- package/dist/result/result.js.map +1 -0
- package/dist/types/globals.cjs +4 -0
- package/dist/types/globals.cjs.map +1 -0
- package/dist/types/globals.d.cts +64 -0
- package/dist/types/globals.d.ts +64 -0
- package/dist/types/globals.js +3 -0
- package/dist/types/globals.js.map +1 -0
- package/index.ts +3 -0
- package/package.json +66 -0
- package/vite.config.ts +16 -0
package/.release-it.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://unpkg.com/release-it/schema/release-it.json",
|
|
3
|
+
"git": {
|
|
4
|
+
"requireBranch": "main",
|
|
5
|
+
"commitMessage": "chore: release v${version}"
|
|
6
|
+
},
|
|
7
|
+
"hooks": {
|
|
8
|
+
"before:init": ["git pull", "pnpm run test"],
|
|
9
|
+
"afeter:bump": ["pnpm run build", "npx auto-changelog -p"]
|
|
10
|
+
},
|
|
11
|
+
"github": {
|
|
12
|
+
"release": true
|
|
13
|
+
},
|
|
14
|
+
"npm": {
|
|
15
|
+
"publish": true
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/README.md
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
# ResulTS
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@consolidados/results)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
This package provides robust implementations of the `Result` and `Option` types, inspired by functional programming principles, to handle success/failure scenarios and optional values in your TypeScript backend applications. It also includes helper functions and a `match` function for convenient usage.
|
|
7
|
+
|
|
8
|
+
The `Result<T, E>` type is heavily inspired by Rust's `Result` and aims to provide a
|
|
9
|
+
robust way to handle success and failure scenarios in TypeScript.
|
|
10
|
+
|
|
11
|
+
The `Option<T>` type is heavily inspired by Rust's `Option` and aims to provide a robust way to handle optional values in TypeScript,
|
|
12
|
+
avoiding `null` and `undefined` issues.
|
|
13
|
+
|
|
14
|
+
## Installation (Global Availability Recommended)
|
|
15
|
+
|
|
16
|
+
For ease of use, the `Ok`, `Err`, `Some`, `None`, and `match` functions can be made globally available in your TypeScript project.
|
|
17
|
+
|
|
18
|
+
1. **Configure `tsconfig.json`:**
|
|
19
|
+
|
|
20
|
+
Within the `compilerOptions`, add the following to the `types` array:
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
"types": ["vitest/globals", "@consolidados/results/globals"]
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
2. **Import in Entry Point (e.g., `main.ts` or `index.ts`):**
|
|
27
|
+
|
|
28
|
+
Import the entire package in your main application file:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
// main.ts
|
|
32
|
+
import "@consolidados/results";
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
After this setup, you can use `Ok`, `Err`, `Some`, `None`, and `match` directly in your code without explicit imports.
|
|
36
|
+
|
|
37
|
+
## Core Concepts
|
|
38
|
+
|
|
39
|
+
### `Result<T, E>`
|
|
40
|
+
|
|
41
|
+
The `Result<T, E>` type represents the outcome of an operation that can either succeed with a value of type `T` or fail with an error of type `E` (typically extending `Error`).
|
|
42
|
+
|
|
43
|
+
**Generic Types:**
|
|
44
|
+
|
|
45
|
+
- `T`: The type of the successful value.
|
|
46
|
+
- `E`: The type of the error value (should extend `Error`).
|
|
47
|
+
|
|
48
|
+
**Usage Example:**
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
function divide(a: number, b: number): Result<number, Error> {
|
|
52
|
+
if (b === 0) {
|
|
53
|
+
return Err(new Error("Cannot divide by zero"));
|
|
54
|
+
}
|
|
55
|
+
return Ok(a / b);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const result1 = divide(10, 2);
|
|
59
|
+
const result2 = divide(10, 0);
|
|
60
|
+
|
|
61
|
+
if (result1.isOk()) {
|
|
62
|
+
console.log("Result:", result1.unwrap()); // Output: Result: 5
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (result2.isErr()) {
|
|
66
|
+
console.error("Error:", result2.unwrapErr().message); // Output: Error: Cannot divide by zero
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### `Ok<T>`
|
|
71
|
+
|
|
72
|
+
Represents a successful result containing a value of type `T`.
|
|
73
|
+
|
|
74
|
+
**Constructor:**
|
|
75
|
+
|
|
76
|
+
```TypeScript
|
|
77
|
+
|
|
78
|
+
new Ok<T>(value: T)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Example:**
|
|
82
|
+
|
|
83
|
+
```TypeScript
|
|
84
|
+
|
|
85
|
+
const successResult: Result<string, Error> = Ok("Operation successful");
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### `Err<E extends Error>`
|
|
89
|
+
|
|
90
|
+
Represents a failed result containing an error of type `E`.
|
|
91
|
+
|
|
92
|
+
**Constructor:**
|
|
93
|
+
|
|
94
|
+
```TypeScript
|
|
95
|
+
|
|
96
|
+
new Err<E>(error: E | string)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Example:**
|
|
100
|
+
|
|
101
|
+
```TypeScript
|
|
102
|
+
|
|
103
|
+
const failureResult: Result<number, Error> = Err(new Error("Operation failed"));
|
|
104
|
+
const failureResultWithMessage: Result<number, Error> = Err("Something went wrong");
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### Result Methods
|
|
108
|
+
|
|
109
|
+
- **`isOk(): this is Ok<T>`**: Checks if the result is a successful `Ok` value.
|
|
110
|
+
- **`isErr(): this is Err<E>`**: Checks if the result is a failed `Err` value.
|
|
111
|
+
- **`unwrap(): T`**: Extracts the successful value or throws an error if it's an `Err`.
|
|
112
|
+
- **`unwrapErr(): E`**: Extracts the error value or throws an error if it's an `Ok`.
|
|
113
|
+
- **`map<U>(fn: (value: T) => U): Result<U, E>`**: Applies a transformation function to the value of an `Ok`.
|
|
114
|
+
- **`flatMap<U>(fn: (value: T) => Result<U, E>): Result<U, E>`**: Applies a function that returns a `Result` to the value of an `Ok`.
|
|
115
|
+
- **`mapErr<U extends Error>(fn: (err: E) => U): Result<T, U>`**: Applies a transformation function to the error value of an `Err`.
|
|
116
|
+
|
|
117
|
+
### `Option<T>`
|
|
118
|
+
|
|
119
|
+
The `Option<T>` type represents an optional value that may or may not exist.
|
|
120
|
+
|
|
121
|
+
**Generic Type:**
|
|
122
|
+
|
|
123
|
+
- `T`: The type of the optional value.
|
|
124
|
+
|
|
125
|
+
**Usage Example:**
|
|
126
|
+
|
|
127
|
+
```TypeScript
|
|
128
|
+
|
|
129
|
+
function findUser(id: number): Option<string> {
|
|
130
|
+
if (id === 123) {
|
|
131
|
+
return Some("John Doe");
|
|
132
|
+
}
|
|
133
|
+
return None();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const user1 = findUser(123);
|
|
137
|
+
const user2 = findUser(456);
|
|
138
|
+
|
|
139
|
+
if (user1.isSome()) {
|
|
140
|
+
console.log("User:", user1.unwrap()); // Output: User: John Doe
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (user2.isNone()) {
|
|
144
|
+
console.log("User not found"); // Output: User not found
|
|
145
|
+
}```
|
|
146
|
+
|
|
147
|
+
#### `Some<T>`
|
|
148
|
+
|
|
149
|
+
Represents an `Option` that contains a value of type `T`.
|
|
150
|
+
|
|
151
|
+
**Constructor:**
|
|
152
|
+
|
|
153
|
+
```TypeScript
|
|
154
|
+
|
|
155
|
+
new Some<T>(value: T)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**Example:**
|
|
159
|
+
|
|
160
|
+
```TypeScript
|
|
161
|
+
|
|
162
|
+
const presentValue: Option<number> = Some(42);
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### `None`
|
|
166
|
+
|
|
167
|
+
Represents an `Option` that does not contain a value.
|
|
168
|
+
|
|
169
|
+
**Example:**
|
|
170
|
+
|
|
171
|
+
```TypeScript
|
|
172
|
+
|
|
173
|
+
const absentValue: Option<string> = None();
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### Option Methods
|
|
177
|
+
|
|
178
|
+
- **`isSome(): this is Some<T>`**: Checks if the option contains a value (`Some`).
|
|
179
|
+
- **`isNone(): this is None`**: Checks if the option does not contain a value (`None`).
|
|
180
|
+
- **`unwrap(): T`**: Extracts the value from a `Some` or throws an error if it's `None`.
|
|
181
|
+
- **`map<U>(fn: (value: T) => U): Option<U>`**: Applies a transformation function to the value of a `Some`.
|
|
182
|
+
- **`flatMap<U>(fn: (value: T) => Option<U>): Option<U>`**: Applies a function that returns an `Option` to the value of a `Some`.
|
|
183
|
+
- **`unwrapOr(defaultValue: T): T`**: Extracts the value from a `Some` or returns a default value if it's `None`.
|
|
184
|
+
|
|
185
|
+
### `match` Function
|
|
186
|
+
|
|
187
|
+
The `match` function provides a concise way to handle different cases for both `Result` and `Option` types.
|
|
188
|
+
|
|
189
|
+
**Usage with `Result`:**
|
|
190
|
+
|
|
191
|
+
```TypeScript
|
|
192
|
+
|
|
193
|
+
const result: Result<string, Error> = Ok("Success");
|
|
194
|
+
|
|
195
|
+
const optionResult: Option<string> = match(result, {
|
|
196
|
+
Ok: (value) => Some(value),
|
|
197
|
+
Err: (error) => None(),
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
match(result, {
|
|
201
|
+
Ok: (value) => console.log("Success Value: " + value),
|
|
202
|
+
Err: (err) => console.log("Err Value: " + err),
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**Usage with `Option`:**
|
|
207
|
+
|
|
208
|
+
```TypeScript
|
|
209
|
+
|
|
210
|
+
`const someValue: Option<number> = Some(10);
|
|
211
|
+
|
|
212
|
+
match(someValue, {
|
|
213
|
+
Some: (value) => console.log("Option has value: " + value),
|
|
214
|
+
None: () => console.log("Option is None"),
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
match(someValue, {
|
|
218
|
+
Some: (value) => Ok(value),
|
|
219
|
+
None: () => Err("Option is None"),
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## API Reference
|
|
224
|
+
|
|
225
|
+
### `Result<T, E>`
|
|
226
|
+
|
|
227
|
+
#### Methods
|
|
228
|
+
|
|
229
|
+
- **`isOk(): this is Ok<T>`**
|
|
230
|
+
- Checks if the result is an `Ok` instance.
|
|
231
|
+
- Returns: `true` if it's an `Ok`, `false` otherwise.
|
|
232
|
+
- **`isErr(): this is Err<E>`**
|
|
233
|
+
- Checks if the result is an `Err` instance.
|
|
234
|
+
- Returns: `true` if it's an `Err`, `false` otherwise.
|
|
235
|
+
- **`unwrap(): T`**
|
|
236
|
+
- Retrieves the value contained in an `Ok` instance.
|
|
237
|
+
- Throws an `Error` if called on an `Err` instance.
|
|
238
|
+
- **`unwrapErr(): E`**
|
|
239
|
+
- Retrieves the error contained in an `Err` instance.
|
|
240
|
+
- Throws an `Error` if called on an `Ok` instance.
|
|
241
|
+
- **`map<U>(fn: (value: T) => U): Result<U, E>`**
|
|
242
|
+
- Applies a function `fn` to the value of an `Ok` instance and returns a new `Result` with the transformed value.
|
|
243
|
+
- If the `Result` is an `Err`, it returns the original `Err` without applying the function.
|
|
244
|
+
- Parameters:
|
|
245
|
+
- `fn: (value: T) => U`: The function to apply to the value.
|
|
246
|
+
- Returns: A new `Result` with the transformed value or the original `Err`.
|
|
247
|
+
- **`flatMap<U>(fn: (value: T) => Result<U, E>): Result<U, E>`**
|
|
248
|
+
- Applies a function `fn` that returns a `Result` to the value of an `Ok` instance.
|
|
249
|
+
- If the `Result` is an `Err`, it returns the original `Err`.
|
|
250
|
+
- Parameters:
|
|
251
|
+
- `fn: (value: T) => Result<U, E>`: The function to apply to the value.
|
|
252
|
+
- Returns: The result of applying the function or the original `Err`.
|
|
253
|
+
- **`mapErr<U extends Error>(fn: (err: E) => U): Result<T, U>`**
|
|
254
|
+
- Applies a function `fn` to the error of an `Err` instance and returns a new `Result` with the transformed error.
|
|
255
|
+
- If the `Result` is an `Ok`, it returns the original `Ok` without applying the function.
|
|
256
|
+
- Parameters:
|
|
257
|
+
- `fn: (err: E) => U`: The function to apply to the error.
|
|
258
|
+
- Returns: A new `Result` with the transformed error or the original `Ok`.
|
|
259
|
+
|
|
260
|
+
### `Option<T>`
|
|
261
|
+
|
|
262
|
+
#### Methods
|
|
263
|
+
|
|
264
|
+
- **`isSome(): this is Some<T>`**
|
|
265
|
+
- Checks if the option is a `Some` instance.
|
|
266
|
+
- Returns: `true` if it's a `Some`, `false` otherwise.
|
|
267
|
+
- **`isNone(): this is None`**
|
|
268
|
+
- Checks if the option is a `None` instance.
|
|
269
|
+
- Returns: `true` if it's a `None`, `false` otherwise.
|
|
270
|
+
- **`unwrap(): T`**
|
|
271
|
+
- Retrieves the value contained in a `Some` instance.
|
|
272
|
+
- Throws an `Error` if called on a `None` instance.
|
|
273
|
+
- **`map<U>(fn: (value: T) => U): Option<U>`**
|
|
274
|
+
- Applies a function `fn` to the value of a `Some` instance and returns a new `Option` with the transformed value.
|
|
275
|
+
- If the `Option` is `None`, it returns `None`.
|
|
276
|
+
- Parameters:
|
|
277
|
+
- `fn: (value: T) => U`: The function to apply to the value.
|
|
278
|
+
- Returns: A new `Option` with the transformed value or `None`.
|
|
279
|
+
- **`flatMap<U>(fn: (value: T) => Option<U>): Option<U>`**
|
|
280
|
+
- Applies a function `fn` that returns an `Option` to the value of a `Some` instance.
|
|
281
|
+
- If the `Option` is `None`, it returns `None`.
|
|
282
|
+
- Parameters:
|
|
283
|
+
- `fn: (value: T) => Option<U>`: The function to apply to the value.
|
|
284
|
+
- Returns: The result of applying the function or `None`.
|
|
285
|
+
- **`unwrapOr(defaultValue: T): T`**
|
|
286
|
+
- Retrieves the value contained in a `Some` instance.
|
|
287
|
+
- If the `Option` is `None`, it returns the provided `defaultValue`.
|
|
288
|
+
- Parameters:
|
|
289
|
+
- `defaultValue: T`: The value to return if the `Option` is `None`.
|
|
290
|
+
- Returns: The value of the `Some` instance or the `defaultValue`.
|
|
291
|
+
|
|
292
|
+
### `match` Function
|
|
293
|
+
|
|
294
|
+
- Provides pattern matching for `Result` and `Option` types.
|
|
295
|
+
- Requires handlers for all possible cases (`Ok`, `Err` for `Result`; `Some`, `None` for `Option`).
|
|
296
|
+
|
|
297
|
+
## Work in Progress (WIP)
|
|
298
|
+
|
|
299
|
+
### `Result`
|
|
300
|
+
|
|
301
|
+
#### Current Implementation
|
|
302
|
+
|
|
303
|
+
The `Result` type currently implements the following methods:
|
|
304
|
+
|
|
305
|
+
- [x] `isOk()`: Checks if the result is `Ok`.
|
|
306
|
+
- [x] `isErr()`: Checks if the result is `Err`.
|
|
307
|
+
- [x] `unwrap()`: Extracts the successful value or throws an error.
|
|
308
|
+
- [x] `unwrapErr()`: Extracts the error value.
|
|
309
|
+
- [x] `map(fn)`: Maps a successful value using a function.
|
|
310
|
+
- [x] `flatMap(fn)`: Applies a function that returns a `Result`.
|
|
311
|
+
- [x] `mapErr(fn)`: Maps an error value using a function.
|
|
312
|
+
|
|
313
|
+
#### Methods to be Developed
|
|
314
|
+
|
|
315
|
+
The following methods are planned for future development:
|
|
316
|
+
|
|
317
|
+
- [ ] `expect(message)`: Extracts the successful value or throws an error with a custom message.
|
|
318
|
+
- [ ] `ok()`: Converts `Result<T, E>` into `Option<T>`.
|
|
319
|
+
- [ ] `err()`: Converts `Result<T, E>` into `Option<E>`.
|
|
320
|
+
- [ ] `and(res)`: Returns `Err` if `self` is `Err`, otherwise returns `res`.
|
|
321
|
+
- [ ] `andThen(fn)`: Calls `fn` if the result is `Ok`, otherwise returns `Err`.
|
|
322
|
+
- [ ] `or(res)`: Returns `Ok` if `self` is `Ok`, otherwise returns `res`.
|
|
323
|
+
- [ ] `orElse(fn)`: Calls `fn` if the result is `Err`, otherwise returns `Ok`.
|
|
324
|
+
- [ ] `unwrapOr(defaultValue)`: Extracts the successful value or returns a default value.
|
|
325
|
+
- [ ] `unwrapOrElse(fn)`: Extracts the successful value or calls a function to get a default value.
|
|
326
|
+
- [ ] `transpose()`: Transposes a `Result<Option<T>, E>` into an `Option<Result<T, E>>`.
|
|
327
|
+
- [ ] `flatten()`: Flattens a nested `Result<Result<T, E>, E>` into a `Result<T, E>`.
|
|
328
|
+
|
|
329
|
+
### `Option`
|
|
330
|
+
|
|
331
|
+
#### Current Implementation
|
|
332
|
+
|
|
333
|
+
The `Option` type currently implements the following methods:
|
|
334
|
+
|
|
335
|
+
- [x] `isSome()`: Checks if the option is `Some`.
|
|
336
|
+
- [x] `isNone()`: Checks if the option is `None`.
|
|
337
|
+
- [x] `unwrap()`: Extracts the value or throws an error if `None`.
|
|
338
|
+
- [x] `map(fn)`: Maps a `Some` value using a function.
|
|
339
|
+
- [x] `flatMap(fn)`: Applies a function that returns an `Option`.
|
|
340
|
+
- [x] `unwrapOr(defaultValue)`: Extracts the value or returns a default value.
|
|
341
|
+
|
|
342
|
+
#### Methods to be Developed
|
|
343
|
+
|
|
344
|
+
The following methods are planned for future development:
|
|
345
|
+
|
|
346
|
+
- [ ] `expect(message)`: Extracts the value or throws an error with a custom message if `None`.
|
|
347
|
+
- [ ] `okOr(err)`: Converts `Option<T>` into `Result<T, E>`.
|
|
348
|
+
- [ ] `okOrElse(errFn)`: Converts `Option<T>` into `Result<T, E>` using a function to create the error.
|
|
349
|
+
- [ ] `and(optb)`: Returns `None` if `self` is `None`, otherwise returns `optb`.
|
|
350
|
+
- [ ] `andThen(fn)`: Calls `fn` if the option is `Some`, otherwise returns `None`.
|
|
351
|
+
- [ ] `or(optb)`: Returns `self` if `Some`, otherwise returns `optb`.
|
|
352
|
+
- [ ] `orElse(fn)`: Returns `self` if `Some`, otherwise calls `fn` to get an `Option`.
|
|
353
|
+
- [ ] `unwrapOrElse(fn)`: Extracts the value or calls a function to get a default value.
|
|
354
|
+
- [ ] `filter(predicate)`: Returns `Some` if the value matches the predicate, otherwise `None`.
|
|
355
|
+
- [ ] `zip(other)`: Zips two `Option` values into a tuple if both are `Some`.
|
|
356
|
+
- [ ] `zipWith(other, fn)`: Zips two `Option` values using a function if both are `Some`.
|
|
357
|
+
- [ ] `transpose()`: Transposes an `Option<Result<T, E>>` into a `Result<Option<T>, E>`.
|
|
358
|
+
|
|
359
|
+
## Contributing
|
|
360
|
+
|
|
361
|
+
Contributions to this package are welcome. Feel free to open issues and submit pull requests.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful result (`Ok`) that contains a value.
|
|
3
|
+
* @template T The type of the value contained in this `Ok`.
|
|
4
|
+
*/
|
|
5
|
+
declare class Ok<T> implements ResultDefinition<T, never> {
|
|
6
|
+
private value;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new `Ok` instance with the given value.
|
|
9
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
10
|
+
*/
|
|
11
|
+
constructor(value: T);
|
|
12
|
+
/**
|
|
13
|
+
* Checks if this result is an `Ok`.
|
|
14
|
+
* @returns `true` because this is an `Ok`.
|
|
15
|
+
*/
|
|
16
|
+
isOk(): this is Ok<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if this result is an `Err`.
|
|
19
|
+
* @returns `false` because this is an `Ok`.
|
|
20
|
+
*/
|
|
21
|
+
isErr(): this is Err<never>;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the value contained in this `Ok`.
|
|
24
|
+
* @returns The value contained in this `Ok`.
|
|
25
|
+
*/
|
|
26
|
+
unwrap(): T;
|
|
27
|
+
/**
|
|
28
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
29
|
+
* @template U The type of the transformed value.
|
|
30
|
+
* @param fn The transformation function to apply to the value.
|
|
31
|
+
* @returns A new `Ok` containing the transformed value.
|
|
32
|
+
*/
|
|
33
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
|
|
34
|
+
/**
|
|
35
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
36
|
+
* @template U The type of the value in the resulting `Result`.
|
|
37
|
+
* @param fn The transformation function to apply to the value.
|
|
38
|
+
* @returns The result of applying the transformation function.
|
|
39
|
+
*/
|
|
40
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
|
|
41
|
+
/**
|
|
42
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
43
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
44
|
+
* @param _fn The mapping function for errors (not used).
|
|
45
|
+
* @returns The original `Ok` instance.
|
|
46
|
+
*/
|
|
47
|
+
mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
50
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
51
|
+
*/
|
|
52
|
+
unwrapErr(): never;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type OkType<T> = [T] extends [never] ? never : T;
|
|
56
|
+
type ErrType<E> = [E] extends [never] ? never : E;
|
|
57
|
+
/**
|
|
58
|
+
* Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
|
|
59
|
+
*/
|
|
60
|
+
type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
|
|
61
|
+
/**
|
|
62
|
+
* Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
|
|
63
|
+
*/
|
|
64
|
+
interface ResultDefinition<T = never, E = never> {
|
|
65
|
+
isOk(): this is Ok<T>;
|
|
66
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
67
|
+
unwrap(): T;
|
|
68
|
+
unwrapErr(): E;
|
|
69
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
|
|
70
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
71
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Represents a failed result (`Err`) that contains an error value.
|
|
76
|
+
* @template E The type of the error contained in this `Err`.
|
|
77
|
+
*/
|
|
78
|
+
declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
|
|
79
|
+
private error;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new `Err` instance with the given error value.
|
|
82
|
+
* @param error The error to wrap in the `Err` instance.
|
|
83
|
+
*/
|
|
84
|
+
constructor(error: E | string);
|
|
85
|
+
/**
|
|
86
|
+
* Checks if this result is an `Ok`.
|
|
87
|
+
* @returns `false` because this is an `Err`.
|
|
88
|
+
*/
|
|
89
|
+
isOk(): this is Ok<never>;
|
|
90
|
+
/**
|
|
91
|
+
* Checks if this result is an `Err`.
|
|
92
|
+
* @returns `true` because this is an `Err`.
|
|
93
|
+
*/
|
|
94
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
95
|
+
/**
|
|
96
|
+
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
97
|
+
* @throws An error because `unwrap` is called on an `Err`.
|
|
98
|
+
*/
|
|
99
|
+
unwrap(): never;
|
|
100
|
+
/**
|
|
101
|
+
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
102
|
+
* @template U The type of the value (ignored for `Err`).
|
|
103
|
+
* @param _fn The mapping function for values (not used).
|
|
104
|
+
* @returns The original `Err` instance.
|
|
105
|
+
*/
|
|
106
|
+
map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
|
|
107
|
+
/**
|
|
108
|
+
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
109
|
+
* @template U The type of the transformed error.
|
|
110
|
+
* @param fn The transformation function to apply to the error value.
|
|
111
|
+
* @returns A new `Err` containing the transformed error.
|
|
112
|
+
*/
|
|
113
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
|
|
114
|
+
/**
|
|
115
|
+
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
116
|
+
* @template U The type of the value in the resulting `Result`.
|
|
117
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
118
|
+
* @returns The original `Err` instance.
|
|
119
|
+
*/
|
|
120
|
+
flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
|
|
121
|
+
/**
|
|
122
|
+
* Retrieves the error value contained in this `Err`.
|
|
123
|
+
* @returns The error value contained in this `Err`.
|
|
124
|
+
*/
|
|
125
|
+
unwrapErr(): E;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { Err as E, Ok as O, type Result as R };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful result (`Ok`) that contains a value.
|
|
3
|
+
* @template T The type of the value contained in this `Ok`.
|
|
4
|
+
*/
|
|
5
|
+
declare class Ok<T> implements ResultDefinition<T, never> {
|
|
6
|
+
private value;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new `Ok` instance with the given value.
|
|
9
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
10
|
+
*/
|
|
11
|
+
constructor(value: T);
|
|
12
|
+
/**
|
|
13
|
+
* Checks if this result is an `Ok`.
|
|
14
|
+
* @returns `true` because this is an `Ok`.
|
|
15
|
+
*/
|
|
16
|
+
isOk(): this is Ok<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if this result is an `Err`.
|
|
19
|
+
* @returns `false` because this is an `Ok`.
|
|
20
|
+
*/
|
|
21
|
+
isErr(): this is Err<never>;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the value contained in this `Ok`.
|
|
24
|
+
* @returns The value contained in this `Ok`.
|
|
25
|
+
*/
|
|
26
|
+
unwrap(): T;
|
|
27
|
+
/**
|
|
28
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
29
|
+
* @template U The type of the transformed value.
|
|
30
|
+
* @param fn The transformation function to apply to the value.
|
|
31
|
+
* @returns A new `Ok` containing the transformed value.
|
|
32
|
+
*/
|
|
33
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
|
|
34
|
+
/**
|
|
35
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
36
|
+
* @template U The type of the value in the resulting `Result`.
|
|
37
|
+
* @param fn The transformation function to apply to the value.
|
|
38
|
+
* @returns The result of applying the transformation function.
|
|
39
|
+
*/
|
|
40
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
|
|
41
|
+
/**
|
|
42
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
43
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
44
|
+
* @param _fn The mapping function for errors (not used).
|
|
45
|
+
* @returns The original `Ok` instance.
|
|
46
|
+
*/
|
|
47
|
+
mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
50
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
51
|
+
*/
|
|
52
|
+
unwrapErr(): never;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type OkType<T> = [T] extends [never] ? never : T;
|
|
56
|
+
type ErrType<E> = [E] extends [never] ? never : E;
|
|
57
|
+
/**
|
|
58
|
+
* Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
|
|
59
|
+
*/
|
|
60
|
+
type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
|
|
61
|
+
/**
|
|
62
|
+
* Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
|
|
63
|
+
*/
|
|
64
|
+
interface ResultDefinition<T = never, E = never> {
|
|
65
|
+
isOk(): this is Ok<T>;
|
|
66
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
67
|
+
unwrap(): T;
|
|
68
|
+
unwrapErr(): E;
|
|
69
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
|
|
70
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
71
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Represents a failed result (`Err`) that contains an error value.
|
|
76
|
+
* @template E The type of the error contained in this `Err`.
|
|
77
|
+
*/
|
|
78
|
+
declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
|
|
79
|
+
private error;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new `Err` instance with the given error value.
|
|
82
|
+
* @param error The error to wrap in the `Err` instance.
|
|
83
|
+
*/
|
|
84
|
+
constructor(error: E | string);
|
|
85
|
+
/**
|
|
86
|
+
* Checks if this result is an `Ok`.
|
|
87
|
+
* @returns `false` because this is an `Err`.
|
|
88
|
+
*/
|
|
89
|
+
isOk(): this is Ok<never>;
|
|
90
|
+
/**
|
|
91
|
+
* Checks if this result is an `Err`.
|
|
92
|
+
* @returns `true` because this is an `Err`.
|
|
93
|
+
*/
|
|
94
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
95
|
+
/**
|
|
96
|
+
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
97
|
+
* @throws An error because `unwrap` is called on an `Err`.
|
|
98
|
+
*/
|
|
99
|
+
unwrap(): never;
|
|
100
|
+
/**
|
|
101
|
+
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
102
|
+
* @template U The type of the value (ignored for `Err`).
|
|
103
|
+
* @param _fn The mapping function for values (not used).
|
|
104
|
+
* @returns The original `Err` instance.
|
|
105
|
+
*/
|
|
106
|
+
map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
|
|
107
|
+
/**
|
|
108
|
+
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
109
|
+
* @template U The type of the transformed error.
|
|
110
|
+
* @param fn The transformation function to apply to the error value.
|
|
111
|
+
* @returns A new `Err` containing the transformed error.
|
|
112
|
+
*/
|
|
113
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
|
|
114
|
+
/**
|
|
115
|
+
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
116
|
+
* @template U The type of the value in the resulting `Result`.
|
|
117
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
118
|
+
* @returns The original `Err` instance.
|
|
119
|
+
*/
|
|
120
|
+
flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
|
|
121
|
+
/**
|
|
122
|
+
* Retrieves the error value contained in this `Err`.
|
|
123
|
+
* @returns The error value contained in this `Err`.
|
|
124
|
+
*/
|
|
125
|
+
unwrapErr(): E;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { Err as E, Ok as O, type Result as R };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/helpers/match.ts
|
|
4
|
+
function match(matcher, cases) {
|
|
5
|
+
if ("isOk" in matcher && matcher.isOk()) {
|
|
6
|
+
if (!cases.Ok) throw new Error("Missing case for Ok");
|
|
7
|
+
return cases.Ok(matcher.unwrap());
|
|
8
|
+
}
|
|
9
|
+
if ("isErr" in matcher && matcher.isErr()) {
|
|
10
|
+
if (!cases.Err) throw new Error("Missing case for Err");
|
|
11
|
+
return cases.Err(matcher.unwrapErr());
|
|
12
|
+
}
|
|
13
|
+
if ("isSome" in matcher && matcher.isSome()) {
|
|
14
|
+
if (!cases.Some) throw new Error("Missing case for Some");
|
|
15
|
+
return cases.Some(matcher.unwrap());
|
|
16
|
+
}
|
|
17
|
+
if ("isNone" in matcher && matcher.isNone()) {
|
|
18
|
+
if (!cases.None) throw new Error("Missing case for None");
|
|
19
|
+
return cases.None();
|
|
20
|
+
}
|
|
21
|
+
throw new Error("Invalid matcher or missing case");
|
|
22
|
+
}
|
|
23
|
+
global.match = match;
|
|
24
|
+
|
|
25
|
+
exports.match = match;
|
|
26
|
+
//# sourceMappingURL=match.cjs.map
|
|
27
|
+
//# sourceMappingURL=match.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/helpers/match.ts"],"names":[],"mappings":";;;AAiBO,SAAS,KAAA,CACf,SACA,KAMI,EAAA;AACJ,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACxC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGjC,EAAA,IAAI,OAAW,IAAA,OAAA,IAAW,OAAQ,CAAA,KAAA,EAAS,EAAA;AAC1C,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAG1C,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC5C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGnC,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC5C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,MAAM,IAAK,EAAA;AAAA;AAGnB,EAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAClD;AAEC,MAAA,CAAe,KAAQ,GAAA,KAAA","file":"match.cjs","sourcesContent":["import { Result } from \"../result\";\nimport { Option } from \"../option\";\n\nexport function match<T, E extends Error, R>(\n\tmatcher: Result<T, E>,\n\tcases: {\n\t\tOk: (value: T) => R;\n\t\tErr: (error: E) => R;\n\t},\n): R;\nexport function match<T, R>(\n\tmatcher: Option<T>,\n\tcases: {\n\t\tSome: (value: T) => R;\n\t\tNone: () => R;\n\t},\n): R;\nexport function match<T, E extends Error, R>(\n\tmatcher: Result<T, E> | Option<T>,\n\tcases: {\n\t\tOk?: (value: T) => R;\n\t\tErr?: (error: E) => R;\n\t\tSome?: (value: T) => R;\n\t\tNone?: () => R;\n\t},\n): R {\n\tif (\"isOk\" in matcher && matcher.isOk()) {\n\t\tif (!cases.Ok) throw new Error(\"Missing case for Ok\");\n\t\treturn cases.Ok(matcher.unwrap());\n\t}\n\n\tif (\"isErr\" in matcher && matcher.isErr()) {\n\t\tif (!cases.Err) throw new Error(\"Missing case for Err\");\n\t\treturn cases.Err(matcher.unwrapErr() as E);\n\t}\n\n\tif (\"isSome\" in matcher && matcher.isSome()) {\n\t\tif (!cases.Some) throw new Error(\"Missing case for Some\");\n\t\treturn cases.Some(matcher.unwrap());\n\t}\n\n\tif (\"isNone\" in matcher && matcher.isNone()) {\n\t\tif (!cases.None) throw new Error(\"Missing case for None\");\n\t\treturn cases.None();\n\t}\n\n\tthrow new Error(\"Invalid matcher or missing case\");\n}\n\n(global as any).match = match;\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { R as Result } from '../err-B7LUEZ0f.cjs';
|
|
2
|
+
import { O as Option } from '../option-DpT8KCGE.cjs';
|
|
3
|
+
|
|
4
|
+
declare function match<T, E extends Error, R>(matcher: Result<T, E>, cases: {
|
|
5
|
+
Ok: (value: T) => R;
|
|
6
|
+
Err: (error: E) => R;
|
|
7
|
+
}): R;
|
|
8
|
+
declare function match<T, R>(matcher: Option<T>, cases: {
|
|
9
|
+
Some: (value: T) => R;
|
|
10
|
+
None: () => R;
|
|
11
|
+
}): R;
|
|
12
|
+
|
|
13
|
+
export { match };
|