@zokugun/xtry 0.6.0 → 0.6.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/lib/cjs/async.js +25 -0
- package/lib/cjs/defer.js +50 -0
- package/lib/cjs/index.js +31 -0
- package/lib/cjs/partial.js +52 -0
- package/lib/cjs/result.js +23 -0
- package/lib/cjs/stringify-error.js +21 -0
- package/lib/cjs/sync.js +25 -0
- package/lib/cjs/try.js +70 -0
- package/lib/cjs/utils/is-promise-like.js +6 -0
- package/lib/cjs/utils/types.js +2 -0
- package/package.json +2 -2
package/lib/cjs/async.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xtry = exports.stringifyError = exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = exports.err = exports.ok = exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = exports.yep = exports.yres = exports.yerr = exports.yok = exports.xdefer = void 0;
|
|
4
|
+
var defer_js_1 = require("./defer.js");
|
|
5
|
+
Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
|
|
6
|
+
var partial_js_1 = require("./partial.js");
|
|
7
|
+
Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
|
|
8
|
+
Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
|
|
9
|
+
Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
|
|
10
|
+
Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
|
|
11
|
+
Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
|
|
12
|
+
Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
|
|
13
|
+
Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
|
|
14
|
+
Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
|
|
15
|
+
var result_js_1 = require("./result.js");
|
|
16
|
+
Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
|
|
17
|
+
Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
|
|
18
|
+
Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
|
|
19
|
+
Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
|
|
20
|
+
Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
|
|
21
|
+
Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
|
|
22
|
+
var stringify_error_js_1 = require("./stringify-error.js");
|
|
23
|
+
Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
|
|
24
|
+
var try_js_1 = require("./try.js");
|
|
25
|
+
Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtryAsync; } });
|
package/lib/cjs/defer.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xdefer = xdefer;
|
|
4
|
+
exports.xdeferAsync = xdeferAsync;
|
|
5
|
+
exports.xdeferSync = xdeferSync;
|
|
6
|
+
const result_js_1 = require("./result.js");
|
|
7
|
+
const is_promise_like_js_1 = require("./utils/is-promise-like.js");
|
|
8
|
+
function xdefer(callback) {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
10
|
+
return ((result) => {
|
|
11
|
+
const finalize = (deferResult) => {
|
|
12
|
+
if (deferResult.fails) {
|
|
13
|
+
if (result?.fails) {
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
return deferResult;
|
|
17
|
+
}
|
|
18
|
+
return result ?? (0, result_js_1.ok)();
|
|
19
|
+
};
|
|
20
|
+
const deferredValue = callback instanceof Function ? callback() : callback;
|
|
21
|
+
if ((0, is_promise_like_js_1.isPromiseLike)(deferredValue)) {
|
|
22
|
+
return Promise.resolve(deferredValue).then(finalize);
|
|
23
|
+
}
|
|
24
|
+
return finalize(deferredValue);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function xdeferAsync(callback) {
|
|
28
|
+
return (async (result) => {
|
|
29
|
+
const deferResult = await (callback instanceof Promise ? callback : callback());
|
|
30
|
+
if (deferResult.fails) {
|
|
31
|
+
if (result?.fails) {
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
return deferResult;
|
|
35
|
+
}
|
|
36
|
+
return result ?? (0, result_js_1.ok)();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function xdeferSync(callback) {
|
|
40
|
+
return ((result) => {
|
|
41
|
+
const deferResult = callback();
|
|
42
|
+
if (deferResult.fails) {
|
|
43
|
+
if (result?.fails) {
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
return deferResult;
|
|
47
|
+
}
|
|
48
|
+
return result ?? (0, result_js_1.ok)();
|
|
49
|
+
});
|
|
50
|
+
}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xtrySync = exports.xtryAsync = exports.xtry = exports.stringifyError = exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = exports.err = exports.ok = exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = exports.yep = exports.yresSync = exports.yresAsync = exports.yres = exports.yerr = exports.yok = exports.xdeferSync = exports.xdeferAsync = exports.xdefer = void 0;
|
|
4
|
+
var defer_js_1 = require("./defer.js");
|
|
5
|
+
Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdefer; } });
|
|
6
|
+
Object.defineProperty(exports, "xdeferAsync", { enumerable: true, get: function () { return defer_js_1.xdeferAsync; } });
|
|
7
|
+
Object.defineProperty(exports, "xdeferSync", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
|
|
8
|
+
var partial_js_1 = require("./partial.js");
|
|
9
|
+
Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
|
|
10
|
+
Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
|
|
11
|
+
Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yres; } });
|
|
12
|
+
Object.defineProperty(exports, "yresAsync", { enumerable: true, get: function () { return partial_js_1.yresAsync; } });
|
|
13
|
+
Object.defineProperty(exports, "yresSync", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
|
|
14
|
+
Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
|
|
15
|
+
Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
|
|
16
|
+
Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
|
|
17
|
+
Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
|
|
18
|
+
Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
|
|
19
|
+
var result_js_1 = require("./result.js");
|
|
20
|
+
Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
|
|
21
|
+
Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
|
|
22
|
+
Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
|
|
23
|
+
Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
|
|
24
|
+
Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
|
|
25
|
+
Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
|
|
26
|
+
var stringify_error_js_1 = require("./stringify-error.js");
|
|
27
|
+
Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
|
|
28
|
+
var try_js_1 = require("./try.js");
|
|
29
|
+
Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtry; } });
|
|
30
|
+
Object.defineProperty(exports, "xtryAsync", { enumerable: true, get: function () { return try_js_1.xtryAsync; } });
|
|
31
|
+
Object.defineProperty(exports, "xtrySync", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = void 0;
|
|
4
|
+
exports.yok = yok;
|
|
5
|
+
exports.yerr = yerr;
|
|
6
|
+
exports.yres = yres;
|
|
7
|
+
exports.yresSync = yresSync;
|
|
8
|
+
exports.yresAsync = yresAsync;
|
|
9
|
+
exports.yep = yep;
|
|
10
|
+
const is_promise_like_js_1 = require("./utils/is-promise-like.js");
|
|
11
|
+
function yok(value) {
|
|
12
|
+
return {
|
|
13
|
+
fails: false,
|
|
14
|
+
success: true,
|
|
15
|
+
value: value,
|
|
16
|
+
error: undefined,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function yerr(miscue) {
|
|
20
|
+
return {
|
|
21
|
+
fails: false,
|
|
22
|
+
success: false,
|
|
23
|
+
miscue,
|
|
24
|
+
value: undefined,
|
|
25
|
+
error: undefined,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function yres(result) {
|
|
29
|
+
if ((0, is_promise_like_js_1.isPromiseLike)(result)) {
|
|
30
|
+
return result.then(yresSync);
|
|
31
|
+
}
|
|
32
|
+
return yresSync(result);
|
|
33
|
+
}
|
|
34
|
+
function yresSync(result) {
|
|
35
|
+
if (result.fails) {
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
return yep(result);
|
|
39
|
+
}
|
|
40
|
+
async function yresAsync(promise) {
|
|
41
|
+
return promise.then(yresSync);
|
|
42
|
+
}
|
|
43
|
+
function yep(result) {
|
|
44
|
+
return {
|
|
45
|
+
...result,
|
|
46
|
+
success: true,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
exports.YOK = Object.freeze(yok());
|
|
50
|
+
exports.YOK_NULL = Object.freeze(yok(null));
|
|
51
|
+
exports.YOK_TRUE = Object.freeze(yok(true));
|
|
52
|
+
exports.YOK_FALSE = Object.freeze(yok(false));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = void 0;
|
|
4
|
+
exports.ok = ok;
|
|
5
|
+
exports.err = err;
|
|
6
|
+
function ok(value) {
|
|
7
|
+
return {
|
|
8
|
+
fails: false,
|
|
9
|
+
value: value,
|
|
10
|
+
error: undefined,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function err(error) {
|
|
14
|
+
return {
|
|
15
|
+
fails: true,
|
|
16
|
+
value: undefined,
|
|
17
|
+
error,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
exports.OK = Object.freeze(ok());
|
|
21
|
+
exports.OK_NULL = Object.freeze(ok(null));
|
|
22
|
+
exports.OK_TRUE = Object.freeze(ok(true));
|
|
23
|
+
exports.OK_FALSE = Object.freeze(ok(false));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringifyError = stringifyError;
|
|
4
|
+
function stringifyError(error) {
|
|
5
|
+
if (typeof error === 'string') {
|
|
6
|
+
return error;
|
|
7
|
+
}
|
|
8
|
+
if (error instanceof Error) {
|
|
9
|
+
return error.message ?? String(error);
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
const json = JSON.stringify(error);
|
|
13
|
+
if (typeof json === 'string') {
|
|
14
|
+
return json;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
// fallthrough
|
|
19
|
+
}
|
|
20
|
+
return String(error);
|
|
21
|
+
}
|
package/lib/cjs/sync.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xtry = exports.stringifyError = exports.OK_FALSE = exports.OK_TRUE = exports.OK_NULL = exports.OK = exports.err = exports.ok = exports.YOK_FALSE = exports.YOK_TRUE = exports.YOK_NULL = exports.YOK = exports.yep = exports.yres = exports.yerr = exports.yok = exports.xdefer = void 0;
|
|
4
|
+
var defer_js_1 = require("./defer.js");
|
|
5
|
+
Object.defineProperty(exports, "xdefer", { enumerable: true, get: function () { return defer_js_1.xdeferSync; } });
|
|
6
|
+
var partial_js_1 = require("./partial.js");
|
|
7
|
+
Object.defineProperty(exports, "yok", { enumerable: true, get: function () { return partial_js_1.yok; } });
|
|
8
|
+
Object.defineProperty(exports, "yerr", { enumerable: true, get: function () { return partial_js_1.yerr; } });
|
|
9
|
+
Object.defineProperty(exports, "yres", { enumerable: true, get: function () { return partial_js_1.yresSync; } });
|
|
10
|
+
Object.defineProperty(exports, "yep", { enumerable: true, get: function () { return partial_js_1.yep; } });
|
|
11
|
+
Object.defineProperty(exports, "YOK", { enumerable: true, get: function () { return partial_js_1.YOK; } });
|
|
12
|
+
Object.defineProperty(exports, "YOK_NULL", { enumerable: true, get: function () { return partial_js_1.YOK_NULL; } });
|
|
13
|
+
Object.defineProperty(exports, "YOK_TRUE", { enumerable: true, get: function () { return partial_js_1.YOK_TRUE; } });
|
|
14
|
+
Object.defineProperty(exports, "YOK_FALSE", { enumerable: true, get: function () { return partial_js_1.YOK_FALSE; } });
|
|
15
|
+
var result_js_1 = require("./result.js");
|
|
16
|
+
Object.defineProperty(exports, "ok", { enumerable: true, get: function () { return result_js_1.ok; } });
|
|
17
|
+
Object.defineProperty(exports, "err", { enumerable: true, get: function () { return result_js_1.err; } });
|
|
18
|
+
Object.defineProperty(exports, "OK", { enumerable: true, get: function () { return result_js_1.OK; } });
|
|
19
|
+
Object.defineProperty(exports, "OK_NULL", { enumerable: true, get: function () { return result_js_1.OK_NULL; } });
|
|
20
|
+
Object.defineProperty(exports, "OK_TRUE", { enumerable: true, get: function () { return result_js_1.OK_TRUE; } });
|
|
21
|
+
Object.defineProperty(exports, "OK_FALSE", { enumerable: true, get: function () { return result_js_1.OK_FALSE; } });
|
|
22
|
+
var stringify_error_js_1 = require("./stringify-error.js");
|
|
23
|
+
Object.defineProperty(exports, "stringifyError", { enumerable: true, get: function () { return stringify_error_js_1.stringifyError; } });
|
|
24
|
+
var try_js_1 = require("./try.js");
|
|
25
|
+
Object.defineProperty(exports, "xtry", { enumerable: true, get: function () { return try_js_1.xtrySync; } });
|
package/lib/cjs/try.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.xtry = xtry;
|
|
4
|
+
exports.xtrySync = xtrySync;
|
|
5
|
+
exports.xtryAsync = xtryAsync;
|
|
6
|
+
const result_js_1 = require("./result.js");
|
|
7
|
+
const is_promise_like_js_1 = require("./utils/is-promise-like.js");
|
|
8
|
+
function xtry(func, handler) {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
10
|
+
const run = func instanceof Function ? func : () => func;
|
|
11
|
+
const handleError = (error) => {
|
|
12
|
+
if (handler) {
|
|
13
|
+
const newError = handler(error);
|
|
14
|
+
if (newError !== undefined) {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
16
|
+
return (0, result_js_1.err)(newError);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return (0, result_js_1.err)(error);
|
|
20
|
+
};
|
|
21
|
+
const handleSuccess = (value) => (0, result_js_1.ok)(value);
|
|
22
|
+
try {
|
|
23
|
+
const value = run();
|
|
24
|
+
if ((0, is_promise_like_js_1.isPromiseLike)(value)) {
|
|
25
|
+
return Promise.resolve(value).then(handleSuccess, handleError);
|
|
26
|
+
}
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
28
|
+
return (0, result_js_1.ok)(value);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
32
|
+
return handleError(error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function xtrySync(func, handler) {
|
|
36
|
+
try {
|
|
37
|
+
const value = func();
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
39
|
+
return (0, result_js_1.ok)(value);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (handler) {
|
|
43
|
+
const newError = handler(error);
|
|
44
|
+
if (newError !== undefined) {
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
46
|
+
return (0, result_js_1.err)(newError);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
50
|
+
return (0, result_js_1.err)(error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function xtryAsync(func, handler) {
|
|
54
|
+
try {
|
|
55
|
+
const value = await (func instanceof Promise ? func : Promise.resolve().then(func));
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
57
|
+
return (0, result_js_1.ok)(value);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (handler) {
|
|
61
|
+
const newError = handler(error);
|
|
62
|
+
if (newError !== undefined) {
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
64
|
+
return (0, result_js_1.err)(newError);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
68
|
+
return (0, result_js_1.err)(error);
|
|
69
|
+
}
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zokugun/xtry",
|
|
3
3
|
"description": "simple try/catch wrapper returning Result",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Baptiste Augrain",
|
|
7
7
|
"email": "daiyam@zokugun.org"
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"lint": "xo",
|
|
60
60
|
"lint:fix": "xo --fix",
|
|
61
61
|
"prepare": "husky; fixpack || true",
|
|
62
|
-
"prepublishOnly": "npm run
|
|
62
|
+
"prepublishOnly": "npm run build",
|
|
63
63
|
"release": "release-it",
|
|
64
64
|
"test": "vitest run",
|
|
65
65
|
"watch:source": "tsc-watch -p src",
|