@theateros/result 0.0.1
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 +201 -0
- package/dist/result.d.ts +146 -0
- package/dist/result.d.ts.map +1 -0
- package/dist/result.js +128 -0
- package/dist/result.js.map +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Theater OS - Result
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="../../.etc/assets/result-logo.webp" alt="Theater OS - Foundations - Result">
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
A type-safe Result type implementation for TypeScript that provides a functional approach to error handling without throwing exceptions and allows codebases to take care of their wrong path.
|
|
8
|
+
|
|
9
|
+
## Why Result?
|
|
10
|
+
|
|
11
|
+
Traditional error handling in JavaScript/TypeScript relies on exceptions, which have several limitations:
|
|
12
|
+
|
|
13
|
+
- **No type safety**: You can't know at compile time if a function might throw
|
|
14
|
+
- **Unpredictable control flow**: Exceptions can be thrown anywhere, making code flow hard to follow
|
|
15
|
+
- **No explicit error types**: You don't know what errors a function might produce
|
|
16
|
+
- **Hard to compose**: Chaining operations that might fail requires try-catch blocks everywhere
|
|
17
|
+
|
|
18
|
+
`Result` addresses these issues by providing:
|
|
19
|
+
|
|
20
|
+
- **Type-safe error handling**: The type system enforces handling both success and error cases
|
|
21
|
+
- **Explicit error types**: Errors are part of the return type, making them visible and type-checked
|
|
22
|
+
- **Composable operations**: Chain operations without nested try-catch blocks
|
|
23
|
+
- **No exceptions**: Errors are values, not thrown exceptions
|
|
24
|
+
- **Functional style**: Inspired by Rust's `Result<T, E>` type
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @theateros/result
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Getting Started
|
|
33
|
+
|
|
34
|
+
### Basic Usage
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { Result } from '@theateros/result'
|
|
38
|
+
|
|
39
|
+
// Create a successful result
|
|
40
|
+
const success = Result.ok('Operation completed')
|
|
41
|
+
|
|
42
|
+
// Create an error result
|
|
43
|
+
const failure = Result.err('Something went wrong')
|
|
44
|
+
|
|
45
|
+
// Check the result
|
|
46
|
+
if (Result.isOk(success)) {
|
|
47
|
+
console.log('Value:', success.value)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (Result.isErr(failure)) {
|
|
51
|
+
console.log('Error:', failure.error)
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Creating Results
|
|
56
|
+
|
|
57
|
+
Results can represent either success (`Ok`) or failure (`Err`):
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { Result } from '@theateros/result'
|
|
61
|
+
|
|
62
|
+
// Success case
|
|
63
|
+
const userResult = Result.ok({ id: 1, name: 'John' })
|
|
64
|
+
|
|
65
|
+
// Error case
|
|
66
|
+
const errorResult = Result.err('User not found')
|
|
67
|
+
|
|
68
|
+
// Use type guards to narrow the type
|
|
69
|
+
if (Result.isOk(userResult)) {
|
|
70
|
+
// TypeScript knows this is Ok<{ id: number, name: string }>
|
|
71
|
+
console.log(userResult.value.name)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (Result.isErr(errorResult)) {
|
|
75
|
+
// TypeScript knows this is Err<string>
|
|
76
|
+
console.log(errorResult.error)
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Type Guards
|
|
81
|
+
|
|
82
|
+
Use type guards to safely check and narrow result types:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { Result } from '@theateros/result'
|
|
86
|
+
|
|
87
|
+
function processResult<T, E>(result: Result<T, E>) {
|
|
88
|
+
if (Result.isOk(result)) {
|
|
89
|
+
// TypeScript knows result is Ok<T>
|
|
90
|
+
return result.value
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (Result.isErr(result)) {
|
|
94
|
+
// TypeScript knows result is Err<E>
|
|
95
|
+
return result.error
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Check if something is a Result
|
|
100
|
+
const unknown: unknown = Result.ok(42)
|
|
101
|
+
if (Result.is(unknown)) {
|
|
102
|
+
// Now we know it's a Result
|
|
103
|
+
if (Result.isOk(unknown)) {
|
|
104
|
+
console.log(unknown.value)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Unwrapping Results
|
|
110
|
+
|
|
111
|
+
Safely extract values from results with optional default values:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { Result } from '@theateros/result'
|
|
115
|
+
|
|
116
|
+
// Unwrap with default value
|
|
117
|
+
const result = Result.err('Failed')
|
|
118
|
+
const value = Result.unwrap(result, 'default value')
|
|
119
|
+
// value === 'default value'
|
|
120
|
+
|
|
121
|
+
// Unwrap without default (throws if Err)
|
|
122
|
+
const okResult = Result.ok('success')
|
|
123
|
+
const unwrapped = Result.unwrap(okResult)
|
|
124
|
+
// unwrapped === 'success'
|
|
125
|
+
|
|
126
|
+
// Throws if no default provided
|
|
127
|
+
// (carrefull when using this method because you
|
|
128
|
+
// can possibly break flows entierely).
|
|
129
|
+
try {
|
|
130
|
+
Result.unwrap(Result.err('error'))
|
|
131
|
+
} catch (error) {
|
|
132
|
+
// error === 'error'
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Safe Function Wrapping
|
|
137
|
+
|
|
138
|
+
Wrap functions that might throw to return Results instead:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { Result } from '@theateros/result'
|
|
142
|
+
|
|
143
|
+
// Wrap a function that might throw
|
|
144
|
+
const safeParse = Result.safe((json: string) => JSON.parse(json))
|
|
145
|
+
|
|
146
|
+
// Now it returns a Result instead of throwing
|
|
147
|
+
const result = safeParse('{"key": "value"}')
|
|
148
|
+
|
|
149
|
+
if (Result.isOk(result)) {
|
|
150
|
+
console.log(result.value) // Parsed object
|
|
151
|
+
} else {
|
|
152
|
+
console.log('Parse failed:', result.error)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// With error transformation
|
|
156
|
+
const safeParseWithError = Result.safe(
|
|
157
|
+
(json: string) => JSON.parse(json),
|
|
158
|
+
error => `Parse error: ${String(error)}`
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
const result2 = safeParseWithError('invalid')
|
|
162
|
+
if (Result.isErr(result2)) {
|
|
163
|
+
console.log(result2.error) // "Parse error: ..."
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## API Reference
|
|
168
|
+
|
|
169
|
+
### `Result` Namespace
|
|
170
|
+
|
|
171
|
+
The main namespace containing all Result utilities.
|
|
172
|
+
|
|
173
|
+
#### Types
|
|
174
|
+
|
|
175
|
+
- **`Ok<T>`**: Represents a successful result with value of type `T`
|
|
176
|
+
- **`Err<E>`**: Represents a failed result with error of type `E`
|
|
177
|
+
- **`Result<T, E>`**: Union type representing either `Ok<T>` or `Err<E>`
|
|
178
|
+
|
|
179
|
+
#### Static Methods
|
|
180
|
+
|
|
181
|
+
- **`Result.ok<T>(value: T): Ok<T>`**: Creates a new Ok result
|
|
182
|
+
- **`Result.err<E>(error: E): Err<E>`**: Creates a new Err result
|
|
183
|
+
- **`Result.is<T, E>(result: unknown): result is Result<T, E>`**: Type guard to check if a value is a Result
|
|
184
|
+
- **`Result.isOk<T>(result: unknown): result is Ok<T>`**: Type guard to check if a result is Ok
|
|
185
|
+
- **`Result.isErr<E>(result: unknown): result is Err<E>`**: Type guard to check if a result is Err
|
|
186
|
+
- **`Result.unwrap<T, E>(result: Result<T, E>, defaultValue?: T): T`**: Unwraps a result, returning the value or default, or throwing the error
|
|
187
|
+
- **`Result.safe<Fn, E>(fn: Fn, onError?: (error: unknown) => E)`**: Wraps a function to return Results instead of throwing
|
|
188
|
+
|
|
189
|
+
#### Properties
|
|
190
|
+
|
|
191
|
+
**Ok<T>**:
|
|
192
|
+
- **`_type: symbol`**: Brand symbol for type checking
|
|
193
|
+
- **`_kind: symbol`**: Brand symbol for Ok kind
|
|
194
|
+
- **`value: T`**: The success value
|
|
195
|
+
- **`error?: never`**: Always undefined for Ok results
|
|
196
|
+
|
|
197
|
+
**Err<E>**:
|
|
198
|
+
- **`_type: symbol`**: Brand symbol for type checking
|
|
199
|
+
- **`_kind: symbol`**: Brand symbol for Err kind
|
|
200
|
+
- **`error: E`**: The error value
|
|
201
|
+
- **`value?: never`**: Always undefined for Err results
|
package/dist/result.d.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
declare const resultSymbol: unique symbol;
|
|
2
|
+
declare const okSymbol: unique symbol;
|
|
3
|
+
declare const errSymbol: unique symbol;
|
|
4
|
+
declare const unsetDefaultValueSymbol: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* The Ok type represents a successful result.
|
|
7
|
+
*
|
|
8
|
+
* @typeparam T - The value type.
|
|
9
|
+
*/
|
|
10
|
+
export type Ok<T> = {
|
|
11
|
+
/**
|
|
12
|
+
* The type of the result. Allows us to brand the type of the result and ensure that
|
|
13
|
+
* that is comming from the @theateros/result package.
|
|
14
|
+
*/
|
|
15
|
+
_type: typeof resultSymbol;
|
|
16
|
+
/**
|
|
17
|
+
* The kind of the result. Allows us to brand the kind of the result and ensure that
|
|
18
|
+
* that is comming from the @theateros/result package.
|
|
19
|
+
*/
|
|
20
|
+
_kind: typeof okSymbol;
|
|
21
|
+
/**
|
|
22
|
+
* The value of the result.
|
|
23
|
+
*/
|
|
24
|
+
value: T;
|
|
25
|
+
/**
|
|
26
|
+
* The error of the result. Should be never because the result is OK.
|
|
27
|
+
*/
|
|
28
|
+
error?: never;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The Err type represents a failed result.
|
|
32
|
+
*
|
|
33
|
+
* @typeparam E - The error type.
|
|
34
|
+
*/
|
|
35
|
+
export type Err<E> = {
|
|
36
|
+
/**
|
|
37
|
+
* The type of the result. Allows us to brand the type of the result and ensure that
|
|
38
|
+
* that is comming from the @theateros/result package.
|
|
39
|
+
*/
|
|
40
|
+
_type: typeof resultSymbol;
|
|
41
|
+
/**
|
|
42
|
+
* The kind of the result. Allows us to brand the kind of the result and ensure that
|
|
43
|
+
* that is comming from the @theateros/result package.
|
|
44
|
+
*/
|
|
45
|
+
_kind: typeof errSymbol;
|
|
46
|
+
/**
|
|
47
|
+
* The error of the result.
|
|
48
|
+
*/
|
|
49
|
+
error: E;
|
|
50
|
+
/**
|
|
51
|
+
* The value of the result. Should be never because the result is an error.
|
|
52
|
+
*/
|
|
53
|
+
value?: never;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* The Result type represents a result of a computation.
|
|
57
|
+
*
|
|
58
|
+
* @typeparam T - The value type.
|
|
59
|
+
* @typeparam E - The error type.
|
|
60
|
+
*/
|
|
61
|
+
export type Result<T, E> = Ok<T> | Err<E>;
|
|
62
|
+
export declare namespace Result {
|
|
63
|
+
/**
|
|
64
|
+
* Create a new Ok result.
|
|
65
|
+
*
|
|
66
|
+
* @typeparam T - The value type.
|
|
67
|
+
* @param value - The value of the result.
|
|
68
|
+
* @returns A new Ok result.
|
|
69
|
+
*/
|
|
70
|
+
export function ok<T>(value: T): Ok<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Create a new Err result.
|
|
73
|
+
*
|
|
74
|
+
* @typeparam E - The error type.
|
|
75
|
+
* @param error - The error of the result.
|
|
76
|
+
* @returns A new Err result.
|
|
77
|
+
*/
|
|
78
|
+
export function err<E>(error: E): Err<E>;
|
|
79
|
+
/**
|
|
80
|
+
* Test if the given result is a result.
|
|
81
|
+
*
|
|
82
|
+
* @param result - The result to check.
|
|
83
|
+
*
|
|
84
|
+
* @typeparam T - The value type.
|
|
85
|
+
* @typeparam E - The error type.
|
|
86
|
+
*
|
|
87
|
+
* @returns True if the result is a result, false otherwise.
|
|
88
|
+
*/
|
|
89
|
+
export function is<T = unknown, E = unknown>(result: unknown): result is Result<T, E>;
|
|
90
|
+
/**
|
|
91
|
+
* Test if the given result is an Ok result.
|
|
92
|
+
*
|
|
93
|
+
* @param result - The result to check.
|
|
94
|
+
*
|
|
95
|
+
* @typeparam T - The value type.
|
|
96
|
+
*
|
|
97
|
+
* @returns True if the result is an Ok result, false otherwise.
|
|
98
|
+
*/
|
|
99
|
+
export function isOk<T = unknown>(result: unknown): result is Ok<T>;
|
|
100
|
+
/**
|
|
101
|
+
* Test if the given result is an Err result.
|
|
102
|
+
*
|
|
103
|
+
* @param result - The result to check.
|
|
104
|
+
*
|
|
105
|
+
* @typeparam E - The error type.
|
|
106
|
+
*
|
|
107
|
+
* @returns True if the result is an Err result, false otherwise.
|
|
108
|
+
*/
|
|
109
|
+
export function isErr<E = unknown>(result: unknown): result is Err<E>;
|
|
110
|
+
/**
|
|
111
|
+
* Unwrap the value of the given result.
|
|
112
|
+
*
|
|
113
|
+
* @param result - The result to unwrap.
|
|
114
|
+
*
|
|
115
|
+
* @typeparam T - The value type.
|
|
116
|
+
* @typeparam E - The error type.
|
|
117
|
+
*
|
|
118
|
+
* @throws If the result is an Err result and the default value is the unset default value (default value
|
|
119
|
+
* is not provided), the error of the result is thrown.
|
|
120
|
+
*
|
|
121
|
+
* @returns The value of the result.
|
|
122
|
+
*/
|
|
123
|
+
export function unwrap<T = unknown, E = unknown>(result: Result<T, E>, defaultValue?: T | typeof unsetDefaultValueSymbol): T;
|
|
124
|
+
/**
|
|
125
|
+
* A simple helper that represents any function.
|
|
126
|
+
*
|
|
127
|
+
* @typeparam Args - The arguments type.
|
|
128
|
+
* @typeparam Return - The return type.
|
|
129
|
+
*/
|
|
130
|
+
type AnyFn = (...args: any[]) => any;
|
|
131
|
+
/**
|
|
132
|
+
* Safe wrap a function in a Result.
|
|
133
|
+
*
|
|
134
|
+
* @param fn - The function to safe wrap.
|
|
135
|
+
* @param onError - The function to handle the error.
|
|
136
|
+
*
|
|
137
|
+
* @typeparam T - The value type.
|
|
138
|
+
* @typeparam E - The error type.
|
|
139
|
+
*
|
|
140
|
+
* @returns A new function that wraps the given function in a Result.
|
|
141
|
+
*/
|
|
142
|
+
export function safe<Fn extends AnyFn, E = unknown>(fn: Fn, onError?: (error: unknown) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
|
|
143
|
+
export {};
|
|
144
|
+
}
|
|
145
|
+
export {};
|
|
146
|
+
//# sourceMappingURL=result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,YAAY,EAAE,OAAO,MAAwC,CAAA;AACnE,QAAA,MAAM,QAAQ,EAAE,OAAO,MAA2C,CAAA;AAClE,QAAA,MAAM,SAAS,EAAE,OAAO,MAA4C,CAAA;AACpE,QAAA,MAAM,uBAAuB,EAAE,OAAO,MAA4D,CAAA;AAElG;;;;GAIG;AACH,MAAM,MAAM,EAAE,CAAC,CAAC,IAAI;IAClB;;;OAGG;IACH,KAAK,EAAE,OAAO,YAAY,CAAA;IAE1B;;;OAGG;IACH,KAAK,EAAE,OAAO,QAAQ,CAAA;IAEtB;;OAEG;IACH,KAAK,EAAE,CAAC,CAAA;IAER;;OAEG;IACH,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI;IACnB;;;OAGG;IACH,KAAK,EAAE,OAAO,YAAY,CAAA;IAE1B;;;OAGG;IACH,KAAK,EAAE,OAAO,SAAS,CAAA;IAEvB;;OAEG;IACH,KAAK,EAAE,CAAC,CAAA;IAER;;OAEG;IACH,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,CAAA;AAED;;;;;GAKG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,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AAC,IACjF,GAAG,MAAM,UAAU,CAAC,EAAE,CAAC,KAAG,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAa5D;;CACF"}
|
package/dist/result.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Result = void 0;
|
|
4
|
+
const resultSymbol = Symbol.for('@theateros/result');
|
|
5
|
+
const okSymbol = Symbol.for('@theateros/result/ok');
|
|
6
|
+
const errSymbol = Symbol.for('@theateros/result/err');
|
|
7
|
+
const unsetDefaultValueSymbol = Symbol.for('@theateros/result/unset-default-value');
|
|
8
|
+
var Result;
|
|
9
|
+
(function (Result) {
|
|
10
|
+
/**
|
|
11
|
+
* Create a new Ok result.
|
|
12
|
+
*
|
|
13
|
+
* @typeparam T - The value type.
|
|
14
|
+
* @param value - The value of the result.
|
|
15
|
+
* @returns A new Ok result.
|
|
16
|
+
*/
|
|
17
|
+
function ok(value) {
|
|
18
|
+
return { _type: resultSymbol, _kind: okSymbol, value };
|
|
19
|
+
}
|
|
20
|
+
Result.ok = ok;
|
|
21
|
+
/**
|
|
22
|
+
* Create a new Err result.
|
|
23
|
+
*
|
|
24
|
+
* @typeparam E - The error type.
|
|
25
|
+
* @param error - The error of the result.
|
|
26
|
+
* @returns A new Err result.
|
|
27
|
+
*/
|
|
28
|
+
function err(error) {
|
|
29
|
+
return { _type: resultSymbol, _kind: errSymbol, error };
|
|
30
|
+
}
|
|
31
|
+
Result.err = err;
|
|
32
|
+
/**
|
|
33
|
+
* Test if the given result is a result.
|
|
34
|
+
*
|
|
35
|
+
* @param result - The result to check.
|
|
36
|
+
*
|
|
37
|
+
* @typeparam T - The value type.
|
|
38
|
+
* @typeparam E - The error type.
|
|
39
|
+
*
|
|
40
|
+
* @returns True if the result is a result, false otherwise.
|
|
41
|
+
*/
|
|
42
|
+
function is(result) {
|
|
43
|
+
return result instanceof Object && '_type' in result && '_kind' in result && result._type === resultSymbol;
|
|
44
|
+
}
|
|
45
|
+
Result.is = is;
|
|
46
|
+
/**
|
|
47
|
+
* Test if the given result is an Ok result.
|
|
48
|
+
*
|
|
49
|
+
* @param result - The result to check.
|
|
50
|
+
*
|
|
51
|
+
* @typeparam T - The value type.
|
|
52
|
+
*
|
|
53
|
+
* @returns True if the result is an Ok result, false otherwise.
|
|
54
|
+
*/
|
|
55
|
+
function isOk(result) {
|
|
56
|
+
if (is(result)) {
|
|
57
|
+
return result._kind === okSymbol;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
Result.isOk = isOk;
|
|
62
|
+
/**
|
|
63
|
+
* Test if the given result is an Err result.
|
|
64
|
+
*
|
|
65
|
+
* @param result - The result to check.
|
|
66
|
+
*
|
|
67
|
+
* @typeparam E - The error type.
|
|
68
|
+
*
|
|
69
|
+
* @returns True if the result is an Err result, false otherwise.
|
|
70
|
+
*/
|
|
71
|
+
function isErr(result) {
|
|
72
|
+
if (is(result)) {
|
|
73
|
+
return result._kind === errSymbol;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
Result.isErr = isErr;
|
|
78
|
+
/**
|
|
79
|
+
* Unwrap the value of the given result.
|
|
80
|
+
*
|
|
81
|
+
* @param result - The result to unwrap.
|
|
82
|
+
*
|
|
83
|
+
* @typeparam T - The value type.
|
|
84
|
+
* @typeparam E - The error type.
|
|
85
|
+
*
|
|
86
|
+
* @throws If the result is an Err result and the default value is the unset default value (default value
|
|
87
|
+
* is not provided), the error of the result is thrown.
|
|
88
|
+
*
|
|
89
|
+
* @returns The value of the result.
|
|
90
|
+
*/
|
|
91
|
+
function unwrap(result, defaultValue = unsetDefaultValueSymbol) {
|
|
92
|
+
if (isOk(result)) {
|
|
93
|
+
return result.value;
|
|
94
|
+
}
|
|
95
|
+
if (defaultValue === unsetDefaultValueSymbol) {
|
|
96
|
+
throw result.error;
|
|
97
|
+
}
|
|
98
|
+
return defaultValue;
|
|
99
|
+
}
|
|
100
|
+
Result.unwrap = unwrap;
|
|
101
|
+
/**
|
|
102
|
+
* Safe wrap a function in a Result.
|
|
103
|
+
*
|
|
104
|
+
* @param fn - The function to safe wrap.
|
|
105
|
+
* @param onError - The function to handle the error.
|
|
106
|
+
*
|
|
107
|
+
* @typeparam T - The value type.
|
|
108
|
+
* @typeparam E - The error type.
|
|
109
|
+
*
|
|
110
|
+
* @returns A new function that wraps the given function in a Result.
|
|
111
|
+
*/
|
|
112
|
+
function safe(fn, onError) {
|
|
113
|
+
return (...args) => {
|
|
114
|
+
try {
|
|
115
|
+
const value = fn(...args);
|
|
116
|
+
return ok(value);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (onError) {
|
|
120
|
+
return err(onError(error));
|
|
121
|
+
}
|
|
122
|
+
return err(error);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
Result.safe = safe;
|
|
127
|
+
})(Result || (exports.Result = Result = {}));
|
|
128
|
+
//# sourceMappingURL=result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":";;;AAAA,MAAM,YAAY,GAAkB,MAAM,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;AACnE,MAAM,QAAQ,GAAkB,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAA;AAClE,MAAM,SAAS,GAAkB,MAAM,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;AACpE,MAAM,uBAAuB,GAAkB,MAAM,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAA;AAoElG,IAAiB,MAAM,CAsItB;AAtID,WAAiB,MAAM;IACrB;;;;;;OAMG;IACH,SAAgB,EAAE,CAAI,KAAQ;QAC5B,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EA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EAAM,EAAE,OAA+B;QACzF,OAAO,CAAC,GAAG,IAAoB,EAA6B,EAAE;YAC5D,IAAI,CAAC;gBACH,MAAM,KAAK,GAAG,EAAE,CAAC,GAAG,IAAI,CAAmB,CAAA;gBAE3C,OAAO,EAAE,CAAC,KAAK,CAAC,CAAA;YAClB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,OAAO,EAAE,CAAC;oBACZ,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA;gBAC5B,CAAC;gBAED,OAAO,GAAG,CAAC,KAAU,CAAC,CAAA;YACxB,CAAC;QACH,CAAC,CAAA;IACH,CAAC;IAde,WAAI,OAcnB,CAAA;AACH,CAAC,EAtIgB,MAAM,sBAAN,MAAM,QAsItB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@theateros/result",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"module": "dist/result.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"lint": "biome check .",
|
|
8
|
+
"test": "bun test",
|
|
9
|
+
"build:dist": "tsc",
|
|
10
|
+
"release": "release-it"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/bun": "catalog:",
|
|
14
|
+
"typescript": "catalog:",
|
|
15
|
+
"release-it": "catalog:"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/result.d.ts",
|
|
20
|
+
"default": "./dist/result.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
}
|
|
26
|
+
}
|