@zokugun/xtry 0.11.5 → 0.12.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/README.md +118 -79
- package/lib/cjs/async.cjs +14 -9
- package/lib/cjs/async.d.cts +8 -7
- package/lib/cjs/defer.cjs +12 -12
- package/lib/cjs/defer.d.cts +9 -9
- package/lib/cjs/index.cjs +13 -8
- package/lib/cjs/index.d.cts +6 -5
- package/lib/cjs/partial.cjs +18 -18
- package/lib/cjs/partial.d.cts +16 -13
- package/lib/cjs/result.cjs +9 -9
- package/lib/cjs/result.d.cts +11 -11
- package/lib/cjs/sync.cjs +17 -12
- package/lib/cjs/sync.d.cts +9 -8
- package/lib/cjs/try.cjs +46 -42
- package/lib/cjs/try.d.cts +4 -4
- package/lib/cjs/tryify.cjs +5 -0
- package/lib/cjs/tryify.d.cts +22 -22
- package/lib/cjs/unwrap.cjs +43 -0
- package/lib/cjs/unwrap.d.cts +8 -0
- package/lib/cjs/utils/is-promise-like.d.cts +1 -1
- package/lib/cjs/utils/types.d.cts +2 -2
- package/lib/esm/async.d.mts +8 -7
- package/lib/esm/async.mjs +5 -4
- package/lib/esm/defer.d.mts +9 -9
- package/lib/esm/defer.mjs +12 -12
- package/lib/esm/index.d.mts +6 -5
- package/lib/esm/index.mjs +3 -2
- package/lib/esm/partial.d.mts +16 -13
- package/lib/esm/partial.mjs +15 -15
- package/lib/esm/result.d.mts +11 -11
- package/lib/esm/result.mjs +8 -8
- package/lib/esm/sync.d.mts +9 -8
- package/lib/esm/sync.mjs +6 -5
- package/lib/esm/try.d.mts +4 -4
- package/lib/esm/try.mjs +44 -40
- package/lib/esm/tryify.d.mts +22 -22
- package/lib/esm/tryify.mjs +5 -0
- package/lib/esm/unwrap.d.mts +8 -0
- package/lib/esm/unwrap.mjs +37 -0
- package/lib/esm/utils/is-promise-like.d.mts +1 -1
- package/lib/esm/utils/types.d.mts +2 -2
- package/package.json +60 -24
package/lib/esm/tryify.mjs
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
|
+
/* eslint-disable ts/no-explicit-any */
|
|
1
2
|
import { xtryAsync, xtryAsyncIterable, xtrySync, xtrySyncIterable } from './try.mjs';
|
|
2
3
|
export function xtryifyAsync(fn) {
|
|
4
|
+
// eslint-disable-next-line func-names
|
|
3
5
|
return async function (...args) {
|
|
4
6
|
return xtryAsync(async () => fn(...args));
|
|
5
7
|
};
|
|
6
8
|
}
|
|
7
9
|
export function xtryifyAsyncIterable(fn) {
|
|
10
|
+
// eslint-disable-next-line func-names
|
|
8
11
|
return function (...args) {
|
|
9
12
|
return xtryAsyncIterable(fn(...args));
|
|
10
13
|
};
|
|
11
14
|
}
|
|
12
15
|
export function xtryifySync(fn) {
|
|
16
|
+
// eslint-disable-next-line func-names
|
|
13
17
|
return function (...args) {
|
|
14
18
|
return xtrySync(() => fn(...args));
|
|
15
19
|
};
|
|
16
20
|
}
|
|
17
21
|
export function xtryifySyncIterable(fn) {
|
|
22
|
+
// eslint-disable-next-line func-names
|
|
18
23
|
return function (...args) {
|
|
19
24
|
return xtrySyncIterable(fn(...args));
|
|
20
25
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Result } from './result.mjs';
|
|
2
|
+
export declare function map<T, E, U>(result: Result<T, E>, transform: (value: T) => U): Result<U, E>;
|
|
3
|
+
export declare function match<T, E, R>(result: Result<T, E>, handlers: {
|
|
4
|
+
failure: (error: E) => R;
|
|
5
|
+
success: (value: T) => R;
|
|
6
|
+
}): R;
|
|
7
|
+
export declare function unwrap<T, E>(result: Result<T, E>): T;
|
|
8
|
+
export declare function unwrapOr<T, E, F>(result: Result<T, E>, fallback: F): F | T;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ok } from './result.mjs';
|
|
2
|
+
import { stringifyError } from './stringify-error.mjs';
|
|
3
|
+
export function map(result, transform) {
|
|
4
|
+
if (result.fails) {
|
|
5
|
+
return result;
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
return ok(transform(result.value));
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function match(result, handlers) {
|
|
12
|
+
if (result.fails) {
|
|
13
|
+
return handlers.failure(result.error);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return handlers.success(result.value);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function unwrap(result) {
|
|
20
|
+
if (result.fails) {
|
|
21
|
+
if (result.error instanceof Error) {
|
|
22
|
+
throw result.error;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw new Error(stringifyError(result.error));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result.value;
|
|
29
|
+
}
|
|
30
|
+
export function unwrapOr(result, fallback) {
|
|
31
|
+
if (result.fails) {
|
|
32
|
+
return fallback;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
return result.value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type Callback<T, Args extends unknown[] = []> = NonPromiseCallback<T, Args> | PromiseCallback<T, Args>;
|
|
2
|
-
export type MaybePromise<T> = T |
|
|
3
|
-
export type NotPromise<T> = Exclude<T, Promise<unknown>>;
|
|
2
|
+
export type MaybePromise<T> = Promise<T> | T;
|
|
4
3
|
export type NonPromiseCallback<T, Args extends unknown[] = []> = (...args: Args) => NotPromise<T>;
|
|
4
|
+
export type NotPromise<T> = Exclude<T, Promise<unknown>>;
|
|
5
5
|
export type PromiseCallback<T, Args extends unknown[] = []> = ((...args: Args) => Promise<T>) | Promise<T>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zokugun/xtry",
|
|
3
|
-
"
|
|
4
|
-
"version": "0.11.5",
|
|
3
|
+
"version": "0.12.0",
|
|
5
4
|
"author": {
|
|
6
5
|
"name": "Baptiste Augrain",
|
|
7
6
|
"email": "daiyam@zokugun.org"
|
|
@@ -36,6 +35,10 @@
|
|
|
36
35
|
"./sync": {
|
|
37
36
|
"import": "./lib/esm/sync.mjs",
|
|
38
37
|
"require": "./lib/cjs/sync.cjs"
|
|
38
|
+
},
|
|
39
|
+
"./unwrap": {
|
|
40
|
+
"import": "./lib/esm/unwrap.mjs",
|
|
41
|
+
"require": "./lib/cjs/unwrap.cjs"
|
|
39
42
|
}
|
|
40
43
|
},
|
|
41
44
|
"main": "lib/cjs/index.cjs",
|
|
@@ -56,57 +59,90 @@
|
|
|
56
59
|
],
|
|
57
60
|
"sync": [
|
|
58
61
|
"./lib/cjs/sync.d.cts"
|
|
62
|
+
],
|
|
63
|
+
"unwrap": [
|
|
64
|
+
"./lib/cjs/unwrap.d.cts"
|
|
59
65
|
]
|
|
60
66
|
}
|
|
61
67
|
},
|
|
62
68
|
"scripts": {
|
|
63
69
|
"audit:fix": "npm audit fix --min-release-age=0",
|
|
64
|
-
"build": "npm run clean && npm run build:
|
|
65
|
-
"build:
|
|
66
|
-
"build:package": "tsc-leda update-package",
|
|
70
|
+
"build": "npm run clean && npm run build:out",
|
|
71
|
+
"build:out": "tsc-leda generate",
|
|
72
|
+
"build:package": "tsc-leda update-package && npm run lint:package",
|
|
67
73
|
"ci:lint": "zizmor .",
|
|
68
74
|
"ci:lint:fix": "zizmor . --fix=all",
|
|
69
75
|
"clean": "rimraf .src .test lib",
|
|
70
76
|
"compile:src": "tsc -p src",
|
|
71
77
|
"compile:test": "tsc -p test",
|
|
72
|
-
"lint": "
|
|
73
|
-
"lint:all": "
|
|
74
|
-
"lint:fix": "
|
|
78
|
+
"lint": "eslint",
|
|
79
|
+
"lint:all": "npm audit && npm run lint:package && npm run ci:lint && npm run lint",
|
|
80
|
+
"lint:fix": "eslint --fix",
|
|
81
|
+
"lint:package": "fixpack || true",
|
|
75
82
|
"prepack": "npm run build",
|
|
76
|
-
"prepare": "husky;
|
|
83
|
+
"prepare": "husky; npm run lint:package",
|
|
77
84
|
"release": "release-it",
|
|
78
85
|
"test": "vitest run --reporter tree",
|
|
79
86
|
"test:coverage": "vitest run --reporter tree --coverage --coverage.reporter text",
|
|
80
87
|
"test:ui": "vitest --ui --coverage",
|
|
81
88
|
"test:watch": "vitest",
|
|
82
89
|
"update:artifacts": "artifact update",
|
|
83
|
-
"update:ci": "
|
|
84
|
-
"update:deps": "taze",
|
|
90
|
+
"update:ci": "pinact run -min-age 7 -update",
|
|
91
|
+
"update:deps": "taze --all",
|
|
85
92
|
"watch:build": "tsc-watch -p src --onSuccess 'npm run build'",
|
|
86
93
|
"watch:src": "tsc-watch -p src",
|
|
87
94
|
"watch:test": "tsc-watch -p test"
|
|
88
95
|
},
|
|
89
96
|
"dependencies": {},
|
|
90
97
|
"devDependencies": {
|
|
91
|
-
"@commitlint/cli": "
|
|
92
|
-
"@commitlint/config-conventional": "
|
|
98
|
+
"@commitlint/cli": "20.5.3",
|
|
99
|
+
"@commitlint/config-conventional": "20.5.3",
|
|
93
100
|
"@types/fs-extra": "11.0.4",
|
|
94
|
-
"@types/node": "20.19.
|
|
95
|
-
"@vitest/coverage-v8": "4.1.
|
|
96
|
-
"@vitest/ui": "4.1.
|
|
97
|
-
"@zokugun/
|
|
98
|
-
"@zokugun/
|
|
101
|
+
"@types/node": "20.19.43",
|
|
102
|
+
"@vitest/coverage-v8": "4.1.10",
|
|
103
|
+
"@vitest/ui": "4.1.10",
|
|
104
|
+
"@zokugun/eslint-config": "0.2.1",
|
|
105
|
+
"@zokugun/eslint-config-data": "0.2.1",
|
|
106
|
+
"@zokugun/eslint-config-glossary": "0.2.0",
|
|
107
|
+
"@zokugun/eslint-config-js": "0.2.1",
|
|
108
|
+
"@zokugun/eslint-config-md": "0.4.1",
|
|
109
|
+
"@zokugun/eslint-config-nodejs": "0.2.0",
|
|
110
|
+
"@zokugun/eslint-config-style": "0.3.1",
|
|
111
|
+
"@zokugun/eslint-config-test": "0.2.0",
|
|
112
|
+
"@zokugun/eslint-config-ts": "0.3.2",
|
|
113
|
+
"@zokugun/fs-extra-plus": "0.6.1",
|
|
114
|
+
"@zokugun/tsc-leda": "0.8.3",
|
|
115
|
+
"eslint": "10.8.1",
|
|
99
116
|
"fixpack": "4.0.0",
|
|
100
|
-
"fs-extra": "11.3.
|
|
117
|
+
"fs-extra": "11.3.5",
|
|
101
118
|
"globby": "16.2.0",
|
|
102
119
|
"husky": "9.1.7",
|
|
103
120
|
"lint-staged": "16.4.0",
|
|
104
|
-
"release-it": "20.
|
|
105
|
-
"
|
|
106
|
-
"
|
|
121
|
+
"release-it": "20.2.1",
|
|
122
|
+
"rimraf": "6.1.3",
|
|
123
|
+
"taze": "19.16.0",
|
|
124
|
+
"tsc-watch": "7.2.1",
|
|
107
125
|
"typescript": "5.9.3",
|
|
108
|
-
"vitest": "4.1.
|
|
109
|
-
|
|
126
|
+
"vitest": "4.1.10"
|
|
127
|
+
},
|
|
128
|
+
"overrides": {
|
|
129
|
+
"@inquirer/core": {
|
|
130
|
+
"mute-stream": "3.0.0"
|
|
131
|
+
},
|
|
132
|
+
"@octokit/request": {
|
|
133
|
+
"content-type": "2.1.0"
|
|
134
|
+
},
|
|
135
|
+
"release-it": {
|
|
136
|
+
"undici": "7.29.0"
|
|
137
|
+
},
|
|
138
|
+
"vite": {
|
|
139
|
+
"nanoid": "3.3.18"
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
"allowScripts": {
|
|
143
|
+
"@zokugun/cli-utils": true,
|
|
144
|
+
"fsevents": true,
|
|
145
|
+
"unrs-resolver": true
|
|
110
146
|
},
|
|
111
147
|
"keywords": [
|
|
112
148
|
"catch",
|