@duplojs/utils 1.4.39 → 1.4.40
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/either/index.cjs
CHANGED
|
@@ -48,7 +48,9 @@ exports.whenHasInformation = whenHasInformation.whenHasInformation;
|
|
|
48
48
|
exports.createEitherKind = kind.createEitherKind;
|
|
49
49
|
exports.eitherInformationKind = kind.eitherInformationKind;
|
|
50
50
|
exports.callbackError = safeCallback.callbackError;
|
|
51
|
+
exports.callbackSuccess = safeCallback.callbackSuccess;
|
|
51
52
|
exports.eitherCallbackErrorKind = safeCallback.eitherCallbackErrorKind;
|
|
53
|
+
exports.eitherCallbackSuccessKind = safeCallback.eitherCallbackSuccessKind;
|
|
52
54
|
exports.safeCallback = safeCallback.safeCallback;
|
|
53
55
|
exports.bool = create.bool;
|
|
54
56
|
exports.boolFalsy = falsy.boolFalsy;
|
package/dist/either/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { hasInformation } from './hasInformation.mjs';
|
|
2
2
|
export { whenHasInformation } from './whenHasInformation.mjs';
|
|
3
3
|
export { createEitherKind, eitherInformationKind } from './kind.mjs';
|
|
4
|
-
export { callbackError, eitherCallbackErrorKind, safeCallback } from './safeCallback.mjs';
|
|
4
|
+
export { callbackError, callbackSuccess, eitherCallbackErrorKind, eitherCallbackSuccessKind, safeCallback } from './safeCallback.mjs';
|
|
5
5
|
export { bool } from './bool/create.mjs';
|
|
6
6
|
export { boolFalsy, eitherBoolFalsyKind, isBoolFalsy, whenIsBoolFalsy } from './bool/falsy.mjs';
|
|
7
7
|
export { boolTruthy, eitherBoolTruthyKind, isBoolTruthy, whenIsBoolTruthy } from './bool/truthy.mjs';
|
|
@@ -2,17 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
var kind = require('./kind.cjs');
|
|
4
4
|
var create = require('./left/create.cjs');
|
|
5
|
+
var create$1 = require('./right/create.cjs');
|
|
6
|
+
var is = require('./right/is.cjs');
|
|
7
|
+
var is$1 = require('./left/is.cjs');
|
|
5
8
|
|
|
6
9
|
const eitherCallbackErrorKind = kind.createEitherKind("callback-error");
|
|
7
|
-
|
|
8
|
-
* {@include either/safeCallback/index.md}
|
|
9
|
-
*/
|
|
10
|
+
const eitherCallbackSuccessKind = kind.createEitherKind("callback-success");
|
|
10
11
|
function callbackError(value) {
|
|
11
12
|
return eitherCallbackErrorKind.setTo(create.left("callback", value));
|
|
12
13
|
}
|
|
14
|
+
function callbackSuccess(value) {
|
|
15
|
+
return eitherCallbackSuccessKind.setTo(create$1.right("callback", value));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* {@include either/safeCallback/index.md}
|
|
19
|
+
*/
|
|
13
20
|
function safeCallback(theFunction) {
|
|
14
21
|
try {
|
|
15
|
-
|
|
22
|
+
const result = theFunction();
|
|
23
|
+
if (is.isRight(result) || is$1.isLeft(result)) {
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
return callbackSuccess(result);
|
|
16
27
|
}
|
|
17
28
|
catch (error) {
|
|
18
29
|
return callbackError(error);
|
|
@@ -20,5 +31,7 @@ function safeCallback(theFunction) {
|
|
|
20
31
|
}
|
|
21
32
|
|
|
22
33
|
exports.callbackError = callbackError;
|
|
34
|
+
exports.callbackSuccess = callbackSuccess;
|
|
23
35
|
exports.eitherCallbackErrorKind = eitherCallbackErrorKind;
|
|
36
|
+
exports.eitherCallbackSuccessKind = eitherCallbackSuccessKind;
|
|
24
37
|
exports.safeCallback = safeCallback;
|
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Kind, EscapeVoid } from "../common";
|
|
2
2
|
import { type EitherLeft } from "./left";
|
|
3
|
+
import { type EitherRight } from "./right";
|
|
3
4
|
export declare const eitherCallbackErrorKind: import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsEither/callback-error", unknown>>;
|
|
5
|
+
export declare const eitherCallbackSuccessKind: import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsEither/callback-success", unknown>>;
|
|
4
6
|
type _EitherCallbackError = (EitherLeft<"callback", unknown> & Kind<typeof eitherCallbackErrorKind.definition>);
|
|
7
|
+
type _EitherCallbackSuccess<GenericValue extends unknown> = (EitherRight<"callback", GenericValue> & Kind<typeof eitherCallbackSuccessKind.definition>);
|
|
5
8
|
export interface EitherCallbackError extends _EitherCallbackError {
|
|
6
9
|
}
|
|
10
|
+
export interface EitherCallbackSuccess<GenericValue extends unknown> extends _EitherCallbackSuccess<GenericValue> {
|
|
11
|
+
}
|
|
12
|
+
export declare function callbackError(value: unknown): EitherCallbackError;
|
|
13
|
+
export declare function callbackSuccess<GenericValue extends unknown>(value: GenericValue): EitherCallbackSuccess<GenericValue>;
|
|
14
|
+
type Either = EitherRight | EitherLeft;
|
|
15
|
+
type ComputeSafeCallbackResult<GenericOutput extends unknown> = GenericOutput extends Either ? GenericOutput : GenericOutput extends EscapeVoid ? EitherCallbackSuccess<undefined> : EitherCallbackSuccess<GenericOutput>;
|
|
7
16
|
/**
|
|
8
17
|
* Runs a callback in a safe block. If the callback throws, the function returns a "callback" typed EitherLeft instead of propagating the exception.
|
|
18
|
+
* If the callback returns an Either, it is returned as-is; otherwise the value is wrapped in an EitherRight.
|
|
9
19
|
*
|
|
10
20
|
* Signature: `safeCallback(theFunction)` → returns a value
|
|
11
21
|
*
|
|
@@ -13,16 +23,24 @@ export interface EitherCallbackError extends _EitherCallbackError {
|
|
|
13
23
|
*
|
|
14
24
|
* ```ts
|
|
15
25
|
* const success = E.safeCallback(() => 42);
|
|
26
|
+
* // E.EitherCallbackError | E.EitherCallbackSuccess<number>
|
|
16
27
|
*
|
|
17
28
|
* const failure = E.safeCallback(() => {
|
|
18
29
|
* throw new Error("boom");
|
|
19
30
|
* });
|
|
20
31
|
*
|
|
21
32
|
* const isFailure = E.isLeft(failure);
|
|
33
|
+
*
|
|
34
|
+
* const eitherResult = E.safeCallback(
|
|
35
|
+
* () => E.left("example", "already"),
|
|
36
|
+
* );
|
|
37
|
+
*
|
|
38
|
+
* const isEitherLeft = E.isLeft(eitherResult);
|
|
22
39
|
* ```
|
|
23
40
|
*
|
|
24
41
|
* @remarks
|
|
25
42
|
* - Catches exceptions thrown by the callback and wraps them in an `EitherLeft<"callback">`
|
|
43
|
+
* - Keeps an `EitherLeft` or `EitherRight` returned by the callback untouched
|
|
26
44
|
* - Useful for working in an unsafe environment (3rd party libraries, user code, etc.)
|
|
27
45
|
*
|
|
28
46
|
* @see https://utils.duplojs.dev/en/v1/api/either/safeCallback
|
|
@@ -30,6 +48,5 @@ export interface EitherCallbackError extends _EitherCallbackError {
|
|
|
30
48
|
* @namespace E
|
|
31
49
|
*
|
|
32
50
|
*/
|
|
33
|
-
export declare function
|
|
34
|
-
export declare function safeCallback<GenericOutput extends unknown>(theFunction: () => GenericOutput): GenericOutput | EitherCallbackError;
|
|
51
|
+
export declare function safeCallback<GenericOutput extends unknown>(theFunction: () => GenericOutput): ComputeSafeCallbackResult<GenericOutput> | EitherCallbackError;
|
|
35
52
|
export {};
|
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
import { createEitherKind } from './kind.mjs';
|
|
2
2
|
import { left } from './left/create.mjs';
|
|
3
|
+
import { right } from './right/create.mjs';
|
|
4
|
+
import { isRight } from './right/is.mjs';
|
|
5
|
+
import { isLeft } from './left/is.mjs';
|
|
3
6
|
|
|
4
7
|
const eitherCallbackErrorKind = createEitherKind("callback-error");
|
|
5
|
-
|
|
6
|
-
* {@include either/safeCallback/index.md}
|
|
7
|
-
*/
|
|
8
|
+
const eitherCallbackSuccessKind = createEitherKind("callback-success");
|
|
8
9
|
function callbackError(value) {
|
|
9
10
|
return eitherCallbackErrorKind.setTo(left("callback", value));
|
|
10
11
|
}
|
|
12
|
+
function callbackSuccess(value) {
|
|
13
|
+
return eitherCallbackSuccessKind.setTo(right("callback", value));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* {@include either/safeCallback/index.md}
|
|
17
|
+
*/
|
|
11
18
|
function safeCallback(theFunction) {
|
|
12
19
|
try {
|
|
13
|
-
|
|
20
|
+
const result = theFunction();
|
|
21
|
+
if (isRight(result) || isLeft(result)) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
return callbackSuccess(result);
|
|
14
25
|
}
|
|
15
26
|
catch (error) {
|
|
16
27
|
return callbackError(error);
|
|
17
28
|
}
|
|
18
29
|
}
|
|
19
30
|
|
|
20
|
-
export { callbackError, eitherCallbackErrorKind, safeCallback };
|
|
31
|
+
export { callbackError, callbackSuccess, eitherCallbackErrorKind, eitherCallbackSuccessKind, safeCallback };
|