@oscarpalmer/atoms 0.148.0 → 0.149.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 +24 -0
- package/dist/result/index.js +2 -0
- package/dist/result/match.js +25 -0
- package/package.json +1 -1
- package/src/result/index.ts +3 -1
- package/src/result/match.ts +112 -0
- package/src/result/models.ts +24 -0
- package/types/result/index.d.ts +3 -1
- package/types/result/match.d.ts +31 -0
- package/types/result/models.d.ts +22 -0
package/dist/atoms.full.js
CHANGED
|
@@ -3536,6 +3536,29 @@ const ALPHABET = "abcdefghijklmnopqrstuvwxyz";
|
|
|
3536
3536
|
const BOOLEAN_MODIFIER = .5;
|
|
3537
3537
|
const HEX_CHARACTERS = "0123456789ABCDEF";
|
|
3538
3538
|
const HEX_MAXIMUM = 15;
|
|
3539
|
+
async function asyncMatchResult(result, first, error) {
|
|
3540
|
+
let value;
|
|
3541
|
+
if (typeof result === "function") value = await result();
|
|
3542
|
+
else if (result instanceof Promise) value = await result;
|
|
3543
|
+
else value = result;
|
|
3544
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3545
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
3546
|
+
const okHandler = hasObj ? first.ok : first;
|
|
3547
|
+
const errorHandler = hasObj ? first.error : error;
|
|
3548
|
+
if (isOk(value)) return okHandler(value.value);
|
|
3549
|
+
return errorHandler(value.error, value.original);
|
|
3550
|
+
}
|
|
3551
|
+
function matchResult(result, first, error) {
|
|
3552
|
+
const value = typeof result === "function" ? result() : result;
|
|
3553
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3554
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
3555
|
+
const okHandler = hasObj ? first.ok : first;
|
|
3556
|
+
const errorHandler = hasObj ? first.error : error;
|
|
3557
|
+
if (isOk(value)) return okHandler(value.value);
|
|
3558
|
+
return errorHandler(value.error, value.original);
|
|
3559
|
+
}
|
|
3560
|
+
matchResult.async = asyncMatchResult;
|
|
3561
|
+
const MESSAGE_RESULT = "`result.match` expected a Result or a function that returns a Result";
|
|
3539
3562
|
function attemptAsyncFlow(...fns) {
|
|
3540
3563
|
let Flow;
|
|
3541
3564
|
return (...args) => attempt.async(() => {
|
|
@@ -3592,6 +3615,7 @@ function attempt(callback, err) {
|
|
|
3592
3615
|
}
|
|
3593
3616
|
attempt.async = asyncAttempt;
|
|
3594
3617
|
attempt.flow = attemptFlow;
|
|
3618
|
+
attempt.match = matchResult;
|
|
3595
3619
|
attempt.pipe = attemptPipe;
|
|
3596
3620
|
attempt.promise = attemptPromise;
|
|
3597
3621
|
function error(value, original) {
|
package/dist/result/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isError, isOk, isResult } from "../internal/result.js";
|
|
2
2
|
import { attemptPromise } from "../promise.js";
|
|
3
|
+
import { matchResult } from "./match.js";
|
|
3
4
|
import { attemptFlow } from "./work/flow.js";
|
|
4
5
|
import { attemptPipe } from "./work/pipe.js";
|
|
5
6
|
async function asyncAttempt(value, err) {
|
|
@@ -20,6 +21,7 @@ function attempt(callback, err) {
|
|
|
20
21
|
}
|
|
21
22
|
attempt.async = asyncAttempt;
|
|
22
23
|
attempt.flow = attemptFlow;
|
|
24
|
+
attempt.match = matchResult;
|
|
23
25
|
attempt.pipe = attemptPipe;
|
|
24
26
|
attempt.promise = attemptPromise;
|
|
25
27
|
function error(value, original) {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isOk, isResult } from "../internal/result.js";
|
|
2
|
+
async function asyncMatchResult(result, first, error) {
|
|
3
|
+
let value;
|
|
4
|
+
if (typeof result === "function") value = await result();
|
|
5
|
+
else if (result instanceof Promise) value = await result;
|
|
6
|
+
else value = result;
|
|
7
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
8
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
9
|
+
const okHandler = hasObj ? first.ok : first;
|
|
10
|
+
const errorHandler = hasObj ? first.error : error;
|
|
11
|
+
if (isOk(value)) return okHandler(value.value);
|
|
12
|
+
return errorHandler(value.error, value.original);
|
|
13
|
+
}
|
|
14
|
+
function matchResult(result, first, error) {
|
|
15
|
+
const value = typeof result === "function" ? result() : result;
|
|
16
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
17
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
18
|
+
const okHandler = hasObj ? first.ok : first;
|
|
19
|
+
const errorHandler = hasObj ? first.error : error;
|
|
20
|
+
if (isOk(value)) return okHandler(value.value);
|
|
21
|
+
return errorHandler(value.error, value.original);
|
|
22
|
+
}
|
|
23
|
+
matchResult.async = asyncMatchResult;
|
|
24
|
+
var MESSAGE_RESULT = "`result.match` expected a Result or a function that returns a Result";
|
|
25
|
+
export { matchResult };
|
package/package.json
CHANGED
package/src/result/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {isOk} from '../internal/result';
|
|
2
2
|
import {attemptPromise} from '../promise';
|
|
3
|
+
import {matchResult} from './match';
|
|
3
4
|
import type {Err, ExtendedErr, ExtendedResult, Ok, Result} from './models';
|
|
4
5
|
import {attemptFlow} from './work/flow';
|
|
5
6
|
import {attemptPipe} from './work/pipe';
|
|
@@ -65,7 +66,7 @@ async function asyncAttempt<Value, E>(
|
|
|
65
66
|
* @param error Error value
|
|
66
67
|
* @returns Callback result
|
|
67
68
|
*/
|
|
68
|
-
export function attempt<Value, E>(callback: () => Value, error: E):
|
|
69
|
+
export function attempt<Value, E>(callback: () => Value, error: E): ExtendedResult<Value, E>;
|
|
69
70
|
|
|
70
71
|
/**
|
|
71
72
|
* Executes a callback, catching any errors, and returns a result
|
|
@@ -89,6 +90,7 @@ export function attempt<Value, E>(
|
|
|
89
90
|
|
|
90
91
|
attempt.async = asyncAttempt;
|
|
91
92
|
attempt.flow = attemptFlow;
|
|
93
|
+
attempt.match = matchResult;
|
|
92
94
|
attempt.pipe = attemptPipe;
|
|
93
95
|
attempt.promise = attemptPromise;
|
|
94
96
|
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {isOk, isResult} from '../internal/result';
|
|
2
|
+
import type {AnyResult, ExtendedErr, ResultMatch} from './models';
|
|
3
|
+
|
|
4
|
+
// #region Functions
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Handles a result with match callbacks
|
|
8
|
+
* @param result Result to handle
|
|
9
|
+
* @param handler Match callbacks
|
|
10
|
+
*/
|
|
11
|
+
async function asyncMatchResult<Value, Returned, E = Error>(
|
|
12
|
+
result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
|
|
13
|
+
handler: ResultMatch<Value, Returned, E>,
|
|
14
|
+
): Promise<Returned>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Handles a result with match callbacks
|
|
18
|
+
* @param result Result to handle
|
|
19
|
+
* @param ok Ok callback
|
|
20
|
+
* @param error Error callback
|
|
21
|
+
*/
|
|
22
|
+
async function asyncMatchResult<Value, Returned, E = Error>(
|
|
23
|
+
result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
|
|
24
|
+
ok: ResultMatch<Value, Returned, E>['ok'],
|
|
25
|
+
error: ResultMatch<Value, Returned, E>['error'],
|
|
26
|
+
): Promise<Returned>;
|
|
27
|
+
|
|
28
|
+
async function asyncMatchResult<Value, Returned, E = Error>(
|
|
29
|
+
result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>),
|
|
30
|
+
first: ResultMatch<Value, Returned, E> | ResultMatch<Value, Returned, E>['ok'],
|
|
31
|
+
error?: ResultMatch<Value, Returned, E>['error'],
|
|
32
|
+
): Promise<Returned> {
|
|
33
|
+
let value: AnyResult<Value, E>;
|
|
34
|
+
|
|
35
|
+
if (typeof result === 'function') {
|
|
36
|
+
value = await result();
|
|
37
|
+
} else if (result instanceof Promise) {
|
|
38
|
+
value = await result;
|
|
39
|
+
} else {
|
|
40
|
+
value = result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!isResult(value)) {
|
|
44
|
+
throw new Error(MESSAGE_RESULT);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const hasObj = typeof first === 'object' && first !== null;
|
|
48
|
+
|
|
49
|
+
const okHandler = hasObj ? first.ok : first;
|
|
50
|
+
const errorHandler = hasObj ? first.error : error;
|
|
51
|
+
|
|
52
|
+
if (isOk(value)) {
|
|
53
|
+
return okHandler(value.value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return errorHandler!(value.error, (value as ExtendedErr<E>).original);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Handles a result with match callbacks
|
|
61
|
+
* @param result Result to handle
|
|
62
|
+
* @param handler Match callbacks
|
|
63
|
+
*/
|
|
64
|
+
export function matchResult<Value, Returned, E = Error>(
|
|
65
|
+
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
66
|
+
handler: ResultMatch<Value, Returned, E>,
|
|
67
|
+
): Returned;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Handles a result with match callbacks
|
|
71
|
+
* @param result Result to handle
|
|
72
|
+
* @param ok Ok callback
|
|
73
|
+
* @param error Error callback
|
|
74
|
+
*/
|
|
75
|
+
export function matchResult<Value, Returned, E = Error>(
|
|
76
|
+
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
77
|
+
ok: ResultMatch<Value, Returned, E>['ok'],
|
|
78
|
+
error: ResultMatch<Value, Returned, E>['error'],
|
|
79
|
+
): Returned;
|
|
80
|
+
|
|
81
|
+
export function matchResult<Value, Returned, E = Error>(
|
|
82
|
+
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
83
|
+
first: ResultMatch<Value, Returned, E> | ResultMatch<Value, Returned, E>['ok'],
|
|
84
|
+
error?: ResultMatch<Value, Returned, E>['error'],
|
|
85
|
+
): Returned {
|
|
86
|
+
const value = typeof result === 'function' ? result() : result;
|
|
87
|
+
|
|
88
|
+
if (!isResult(value)) {
|
|
89
|
+
throw new Error(MESSAGE_RESULT);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const hasObj = typeof first === 'object' && first !== null;
|
|
93
|
+
|
|
94
|
+
const okHandler = hasObj ? first.ok : first;
|
|
95
|
+
const errorHandler = hasObj ? first.error : error;
|
|
96
|
+
|
|
97
|
+
if (isOk(value)) {
|
|
98
|
+
return okHandler(value.value);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return errorHandler!(value.error, (value as ExtendedErr<E>).original);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
matchResult.async = asyncMatchResult;
|
|
105
|
+
|
|
106
|
+
// #endregion
|
|
107
|
+
|
|
108
|
+
// #region Variables
|
|
109
|
+
|
|
110
|
+
const MESSAGE_RESULT = '`result.match` expected a Result or a function that returns a Result';
|
|
111
|
+
|
|
112
|
+
// #endregion
|
package/src/result/models.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type {GenericCallback} from '../models';
|
|
|
2
2
|
|
|
3
3
|
// #region Types
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* An unknown result
|
|
7
|
+
*/
|
|
8
|
+
export type AnyResult<Value, E> = Err<E> | ExtendedErr<E> | Ok<Value>;
|
|
9
|
+
|
|
5
10
|
/**
|
|
6
11
|
* An error result
|
|
7
12
|
*/
|
|
@@ -35,6 +40,25 @@ export type Ok<Value> = {
|
|
|
35
40
|
*/
|
|
36
41
|
export type Result<Value, E = Error> = Err<E> | Ok<Value>;
|
|
37
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Match callbacks for a result
|
|
45
|
+
*/
|
|
46
|
+
export type ResultMatch<Value, Returned, E = Error> = {
|
|
47
|
+
/**
|
|
48
|
+
* Callback for error result
|
|
49
|
+
* @param error Error value
|
|
50
|
+
* @param original Original error, if available
|
|
51
|
+
* @returns Value to return
|
|
52
|
+
*/
|
|
53
|
+
error: (error: E, original?: Error) => Returned;
|
|
54
|
+
/**
|
|
55
|
+
* Callback for ok result
|
|
56
|
+
* @param value Ok value
|
|
57
|
+
* @returns Value to return
|
|
58
|
+
*/
|
|
59
|
+
ok: (value: Value) => Returned;
|
|
60
|
+
};
|
|
61
|
+
|
|
38
62
|
/**
|
|
39
63
|
* Unwrap a result value
|
|
40
64
|
*/
|
package/types/result/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { attemptPromise } from '../promise';
|
|
2
|
+
import { matchResult } from './match';
|
|
2
3
|
import type { Err, ExtendedErr, ExtendedResult, Ok, Result } from './models';
|
|
3
4
|
import { attemptFlow } from './work/flow';
|
|
4
5
|
import { attemptPipe } from './work/pipe';
|
|
@@ -34,7 +35,7 @@ declare function asyncAttempt<Value>(callback: () => Promise<Value>): Promise<Re
|
|
|
34
35
|
* @param error Error value
|
|
35
36
|
* @returns Callback result
|
|
36
37
|
*/
|
|
37
|
-
export declare function attempt<Value, E>(callback: () => Value, error: E):
|
|
38
|
+
export declare function attempt<Value, E>(callback: () => Value, error: E): ExtendedResult<Value, E>;
|
|
38
39
|
/**
|
|
39
40
|
* Executes a callback, catching any errors, and returns a result
|
|
40
41
|
* @param callback Callback to execute
|
|
@@ -44,6 +45,7 @@ export declare function attempt<Value>(callback: () => Value): Result<Value, Err
|
|
|
44
45
|
export declare namespace attempt {
|
|
45
46
|
var async: typeof asyncAttempt;
|
|
46
47
|
var flow: typeof attemptFlow;
|
|
48
|
+
var match: typeof matchResult;
|
|
47
49
|
var pipe: typeof attemptPipe;
|
|
48
50
|
var promise: typeof attemptPromise;
|
|
49
51
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AnyResult, ResultMatch } from './models';
|
|
2
|
+
/**
|
|
3
|
+
* Handles a result with match callbacks
|
|
4
|
+
* @param result Result to handle
|
|
5
|
+
* @param handler Match callbacks
|
|
6
|
+
*/
|
|
7
|
+
declare function asyncMatchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>), handler: ResultMatch<Value, Returned, E>): Promise<Returned>;
|
|
8
|
+
/**
|
|
9
|
+
* Handles a result with match callbacks
|
|
10
|
+
* @param result Result to handle
|
|
11
|
+
* @param ok Ok callback
|
|
12
|
+
* @param error Error callback
|
|
13
|
+
*/
|
|
14
|
+
declare function asyncMatchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | Promise<AnyResult<Value, E>> | (() => Promise<AnyResult<Value, E>>), ok: ResultMatch<Value, Returned, E>['ok'], error: ResultMatch<Value, Returned, E>['error']): Promise<Returned>;
|
|
15
|
+
/**
|
|
16
|
+
* Handles a result with match callbacks
|
|
17
|
+
* @param result Result to handle
|
|
18
|
+
* @param handler Match callbacks
|
|
19
|
+
*/
|
|
20
|
+
export declare function matchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | (() => AnyResult<Value, E>), handler: ResultMatch<Value, Returned, E>): Returned;
|
|
21
|
+
/**
|
|
22
|
+
* Handles a result with match callbacks
|
|
23
|
+
* @param result Result to handle
|
|
24
|
+
* @param ok Ok callback
|
|
25
|
+
* @param error Error callback
|
|
26
|
+
*/
|
|
27
|
+
export declare function matchResult<Value, Returned, E = Error>(result: AnyResult<Value, E> | (() => AnyResult<Value, E>), ok: ResultMatch<Value, Returned, E>['ok'], error: ResultMatch<Value, Returned, E>['error']): Returned;
|
|
28
|
+
export declare namespace matchResult {
|
|
29
|
+
var async: typeof asyncMatchResult;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
package/types/result/models.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { GenericCallback } from '../models';
|
|
2
|
+
/**
|
|
3
|
+
* An unknown result
|
|
4
|
+
*/
|
|
5
|
+
export type AnyResult<Value, E> = Err<E> | ExtendedErr<E> | Ok<Value>;
|
|
2
6
|
/**
|
|
3
7
|
* An error result
|
|
4
8
|
*/
|
|
@@ -27,6 +31,24 @@ export type Ok<Value> = {
|
|
|
27
31
|
* An unknown result
|
|
28
32
|
*/
|
|
29
33
|
export type Result<Value, E = Error> = Err<E> | Ok<Value>;
|
|
34
|
+
/**
|
|
35
|
+
* Match callbacks for a result
|
|
36
|
+
*/
|
|
37
|
+
export type ResultMatch<Value, Returned, E = Error> = {
|
|
38
|
+
/**
|
|
39
|
+
* Callback for error result
|
|
40
|
+
* @param error Error value
|
|
41
|
+
* @param original Original error, if available
|
|
42
|
+
* @returns Value to return
|
|
43
|
+
*/
|
|
44
|
+
error: (error: E, original?: Error) => Returned;
|
|
45
|
+
/**
|
|
46
|
+
* Callback for ok result
|
|
47
|
+
* @param value Ok value
|
|
48
|
+
* @returns Value to return
|
|
49
|
+
*/
|
|
50
|
+
ok: (value: Value) => Returned;
|
|
51
|
+
};
|
|
30
52
|
/**
|
|
31
53
|
* Unwrap a result value
|
|
32
54
|
*/
|