@oscarpalmer/atoms 0.147.0 → 0.148.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/dist/atoms.full.js +90 -32
- package/dist/function/work.js +29 -10
- package/dist/index.js +2 -1
- package/dist/internal/result.js +25 -0
- package/dist/{result.js → result/index.js} +6 -25
- package/dist/result/models.js +0 -0
- package/dist/result/work/flow.js +25 -0
- package/dist/result/work/pipe.js +21 -0
- package/package.json +3 -3
- package/src/function/work.ts +316 -254
- package/src/index.ts +1 -1
- package/src/internal/result.ts +80 -0
- package/src/{result.ts → result/index.ts} +14 -113
- package/src/result/models.ts +57 -0
- package/src/result/work/flow.ts +433 -0
- package/src/result/work/pipe.ts +791 -0
- package/types/function/work.d.ts +51 -50
- package/types/index.d.ts +1 -1
- package/types/internal/result.d.ts +37 -0
- package/types/{result.d.ts → result/index.d.ts} +8 -66
- package/types/result/models.d.ts +38 -0
- package/types/result/work/flow.d.ts +134 -0
- package/types/result/work/pipe.d.ts +271 -0
package/src/index.ts
CHANGED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type {Err, ExtendedErr, Ok, Result} from '../result/models';
|
|
2
|
+
import {isPlainObject} from './is';
|
|
3
|
+
|
|
4
|
+
// #region Functions
|
|
5
|
+
|
|
6
|
+
function _isResult(value: unknown, okValue: boolean): value is Result<unknown, unknown> {
|
|
7
|
+
if (!isPlainObject(value)) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return value.ok === okValue && (okValue ? 'value' : 'error') in value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Is the result an extended error?
|
|
16
|
+
* @param result Result to check
|
|
17
|
+
* @returns `true` if the result is an extended error, `false` otherwise
|
|
18
|
+
*/
|
|
19
|
+
export function isError<Value, E = Error>(
|
|
20
|
+
value: ExtendedErr<E> | Result<Value, E>,
|
|
21
|
+
extended: true,
|
|
22
|
+
): value is ExtendedErr<E>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Is the result an error?
|
|
26
|
+
* @param result Result to check
|
|
27
|
+
* @returns `true` if the result is an error, `false` otherwise
|
|
28
|
+
*/
|
|
29
|
+
export function isError<Value, E = Error>(value: Result<Value, E>): value is Err<E>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Is the value an error?
|
|
33
|
+
* @param value Value to check
|
|
34
|
+
* @returns `true` if the value is an error, `false` otherwise
|
|
35
|
+
*/
|
|
36
|
+
export function isError(value: unknown): value is Err<unknown> | ExtendedErr<unknown>;
|
|
37
|
+
|
|
38
|
+
export function isError(
|
|
39
|
+
value: unknown,
|
|
40
|
+
extended?: boolean,
|
|
41
|
+
): value is Err<unknown> | ExtendedErr<unknown> {
|
|
42
|
+
return (
|
|
43
|
+
_isResult(value, false) &&
|
|
44
|
+
(extended === true ? (value as ExtendedErr<unknown>).original instanceof Error : true)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Is the result ok?
|
|
50
|
+
* @param value Result to check
|
|
51
|
+
* @returns `true` if the result is ok, `false` otherwise
|
|
52
|
+
*/
|
|
53
|
+
export function isOk<Value, E = Error>(value: Result<Value, E>): value is Ok<Value>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Is the value ok?
|
|
57
|
+
* @param value Value to check
|
|
58
|
+
* @returns `true` if the value is ok, `false` otherwise
|
|
59
|
+
*/
|
|
60
|
+
export function isOk(value: unknown): value is Ok<unknown>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Is the result ok?
|
|
64
|
+
* @param result Result to check
|
|
65
|
+
* @returns `true` if the result is ok, `false` otherwise
|
|
66
|
+
*/
|
|
67
|
+
export function isOk(value: unknown): value is Ok<unknown> {
|
|
68
|
+
return _isResult(value, true);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Is the value a result?
|
|
73
|
+
* @param value Value to check
|
|
74
|
+
* @returns `true` if the value is a result, `false` otherwise
|
|
75
|
+
*/
|
|
76
|
+
export function isResult(value: unknown): value is ExtendedErr<unknown> | Result<unknown, unknown> {
|
|
77
|
+
return _isResult(value, true) || _isResult(value, false);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// #endregion
|
|
@@ -1,53 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {attemptPromise} from '
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* An error result
|
|
8
|
-
*/
|
|
9
|
-
export type Err<Error> = {
|
|
10
|
-
ok: false;
|
|
11
|
-
error: Error;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* An extended error result
|
|
16
|
-
*/
|
|
17
|
-
export type ExtendedErr<E> = Err<E> & {
|
|
18
|
-
original: Error;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* An extended, unknown result
|
|
23
|
-
*/
|
|
24
|
-
export type ExtendedResult<Value, E> = ExtendedErr<E> | Ok<Value>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* A successful result
|
|
28
|
-
*/
|
|
29
|
-
export type Ok<Value> = {
|
|
30
|
-
ok: true;
|
|
31
|
-
value: Value;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* An unknown result
|
|
36
|
-
*/
|
|
37
|
-
export type Result<Value, E = Error> = Err<E> | Ok<Value>;
|
|
38
|
-
|
|
39
|
-
// #endregion
|
|
1
|
+
import {isOk} from '../internal/result';
|
|
2
|
+
import {attemptPromise} from '../promise';
|
|
3
|
+
import type {Err, ExtendedErr, ExtendedResult, Ok, Result} from './models';
|
|
4
|
+
import {attemptFlow} from './work/flow';
|
|
5
|
+
import {attemptPipe} from './work/pipe';
|
|
40
6
|
|
|
41
7
|
// #region Functions
|
|
42
8
|
|
|
43
|
-
function _isResult(value: unknown, okValue: boolean): value is Result<unknown, unknown> {
|
|
44
|
-
if (!isPlainObject(value)) {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return value.ok === okValue && (okValue ? 'value' : 'error') in value;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
9
|
/**
|
|
52
10
|
* Executes a promise, catching any errors, and returns a result
|
|
53
11
|
* @param promise Promise to execute
|
|
@@ -130,6 +88,8 @@ export function attempt<Value, E>(
|
|
|
130
88
|
}
|
|
131
89
|
|
|
132
90
|
attempt.async = asyncAttempt;
|
|
91
|
+
attempt.flow = attemptFlow;
|
|
92
|
+
attempt.pipe = attemptPipe;
|
|
133
93
|
attempt.promise = attemptPromise;
|
|
134
94
|
|
|
135
95
|
/**
|
|
@@ -164,72 +124,6 @@ function getError<E>(value: E, original?: Error): Err<E> | ExtendedErr<E> {
|
|
|
164
124
|
return errorResult;
|
|
165
125
|
}
|
|
166
126
|
|
|
167
|
-
/**
|
|
168
|
-
* Is the result an extended error?
|
|
169
|
-
* @param result Result to check
|
|
170
|
-
* @returns `true` if the result is an extended error, `false` otherwise
|
|
171
|
-
*/
|
|
172
|
-
export function isError<Value, E = Error>(
|
|
173
|
-
value: ExtendedErr<E> | Result<Value, E>,
|
|
174
|
-
extended: true,
|
|
175
|
-
): value is ExtendedErr<E>;
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Is the result an error?
|
|
179
|
-
* @param result Result to check
|
|
180
|
-
* @returns `true` if the result is an error, `false` otherwise
|
|
181
|
-
*/
|
|
182
|
-
export function isError<Value, E = Error>(value: Result<Value, E>): value is Err<E>;
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Is the value an error?
|
|
186
|
-
* @param value Value to check
|
|
187
|
-
* @returns `true` if the value is an error, `false` otherwise
|
|
188
|
-
*/
|
|
189
|
-
export function isError(value: unknown): value is Err<unknown> | ExtendedErr<unknown>;
|
|
190
|
-
|
|
191
|
-
export function isError(
|
|
192
|
-
value: unknown,
|
|
193
|
-
extended?: boolean,
|
|
194
|
-
): value is Err<unknown> | ExtendedErr<unknown> {
|
|
195
|
-
return (
|
|
196
|
-
_isResult(value, false) &&
|
|
197
|
-
(extended === true ? (value as ExtendedErr<unknown>).original instanceof Error : true)
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Is the result ok?
|
|
203
|
-
* @param value Result to check
|
|
204
|
-
* @returns `true` if the result is ok, `false` otherwise
|
|
205
|
-
*/
|
|
206
|
-
export function isOk<Value, E = Error>(value: Result<Value, E>): value is Ok<Value>;
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Is the value ok?
|
|
210
|
-
* @param value Value to check
|
|
211
|
-
* @returns `true` if the value is ok, `false` otherwise
|
|
212
|
-
*/
|
|
213
|
-
export function isOk(value: unknown): value is Ok<unknown>;
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Is the result ok?
|
|
217
|
-
* @param result Result to check
|
|
218
|
-
* @returns `true` if the result is ok, `false` otherwise
|
|
219
|
-
*/
|
|
220
|
-
export function isOk(value: unknown): value is Ok<unknown> {
|
|
221
|
-
return _isResult(value, true);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Is the value a result?
|
|
226
|
-
* @param value Value to check
|
|
227
|
-
* @returns `true` if the value is a result, `false` otherwise
|
|
228
|
-
*/
|
|
229
|
-
export function isResult(value: unknown): value is ExtendedErr<unknown> | Result<unknown, unknown> {
|
|
230
|
-
return _isResult(value, true) || _isResult(value, false);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
127
|
/**
|
|
234
128
|
* Creates an ok result
|
|
235
129
|
* @param value Value
|
|
@@ -263,3 +157,10 @@ export function unwrap(value: unknown, defaultValue: unknown): unknown {
|
|
|
263
157
|
}
|
|
264
158
|
|
|
265
159
|
// #endregion
|
|
160
|
+
|
|
161
|
+
// #region Exports
|
|
162
|
+
|
|
163
|
+
export {isError, isOk, isResult} from '../internal/result';
|
|
164
|
+
export type {Err, ExtendedErr, ExtendedResult, Ok, Result} from './models';
|
|
165
|
+
|
|
166
|
+
// #endregion
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type {GenericCallback} from '../models';
|
|
2
|
+
|
|
3
|
+
// #region Types
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An error result
|
|
7
|
+
*/
|
|
8
|
+
export type Err<Error> = {
|
|
9
|
+
ok: false;
|
|
10
|
+
error: Error;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* An extended error result
|
|
15
|
+
*/
|
|
16
|
+
export type ExtendedErr<E> = Err<E> & {
|
|
17
|
+
original: Error;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* An extended, unknown result
|
|
22
|
+
*/
|
|
23
|
+
export type ExtendedResult<Value, E> = ExtendedErr<E> | Ok<Value>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A successful result
|
|
27
|
+
*/
|
|
28
|
+
export type Ok<Value> = {
|
|
29
|
+
ok: true;
|
|
30
|
+
value: Value;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* An unknown result
|
|
35
|
+
*/
|
|
36
|
+
export type Result<Value, E = Error> = Err<E> | Ok<Value>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Unwrap a result value
|
|
40
|
+
*/
|
|
41
|
+
type UnwrapResult<Original extends Result<unknown, unknown>> =
|
|
42
|
+
Original extends Ok<infer Value> ? Value : never;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Unwrap any value
|
|
46
|
+
*/
|
|
47
|
+
export type UnwrapValue<Original> = Original extends GenericCallback
|
|
48
|
+
? ReturnType<Original> extends Result<unknown, unknown>
|
|
49
|
+
? UnwrapResult<ReturnType<Original>>
|
|
50
|
+
: ReturnType<Original>
|
|
51
|
+
: Original extends Result<unknown, unknown>
|
|
52
|
+
? UnwrapResult<Original>
|
|
53
|
+
: Original extends Promise<infer Value>
|
|
54
|
+
? Awaited<Value>
|
|
55
|
+
: Original;
|
|
56
|
+
|
|
57
|
+
// #endregion
|