@ntangled/kit 0.0.0-alpha.2 → 0.0.0-alpha.4
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/safe.d.ts +5 -48
- package/dist/safe.d.ts.map +1 -1
- package/dist/safe.js +35 -81
- package/dist/safe.js.map +1 -1
- package/package.json +3 -1
package/dist/safe.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { NonNull } from './types';
|
|
1
2
|
/**
|
|
2
3
|
* Error thrown when a null value is encountered where not expected.
|
|
3
4
|
* @class NullError
|
|
@@ -6,52 +7,8 @@
|
|
|
6
7
|
export declare class NullError extends Error {
|
|
7
8
|
constructor();
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* @template Callback
|
|
14
|
-
* @param {Callback} callback - An async function to execute.
|
|
15
|
-
* @returns {Promise<[NonNull|null, Awaited<ReturnType<Callback>>|null]>} Tuple: [error, result].
|
|
16
|
-
* @example
|
|
17
|
-
* const [err, data] = await px(async () => await fetchData());
|
|
18
|
-
*/
|
|
19
|
-
export declare const px: <Callback extends () => unknown>(callback: Callback) => Promise<[null, Awaited<ReturnType<Callback>>] | readonly [{}, null]>;
|
|
20
|
-
/**
|
|
21
|
-
* Safely executes a synchronous callback, returning a tuple of [error, result].
|
|
22
|
-
* Never throws; errors are returned as the first tuple element.
|
|
23
|
-
*
|
|
24
|
-
* @template Callback
|
|
25
|
-
* @param {Callback} callback - A synchronous function to execute.
|
|
26
|
-
* @returns {[NonNull|null, ReturnType<Callback>|null]} Tuple: [error, result].
|
|
27
|
-
* @example
|
|
28
|
-
* const [err, value] = rx(() => computeValue());
|
|
29
|
-
*/
|
|
30
|
-
export declare const rx: <Callback extends () => unknown>(callback: Callback) => readonly [{}, null] | [null, ReturnType<Callback>];
|
|
31
|
-
/**
|
|
32
|
-
* Wraps an async function to always return a tuple of [error, result].
|
|
33
|
-
* Returned function never throws; errors are returned as the first tuple element.
|
|
34
|
-
*
|
|
35
|
-
* @template Callback
|
|
36
|
-
* @param {Callback} callback - An async function to wrap.
|
|
37
|
-
* @returns {(...params: Parameters<Callback>) => Promise<[NonNull|null, Awaited<ReturnType<Callback>>|null]>}
|
|
38
|
-
* Wrapped function returning [error, result].
|
|
39
|
-
* @example
|
|
40
|
-
* const safeFetch = pw(fetchData);
|
|
41
|
-
* const [err, data] = await safeFetch(arg1, arg2);
|
|
42
|
-
*/
|
|
43
|
-
export declare const pw: <Callback extends (...params: any[]) => unknown>(callback: Callback) => (...params: Parameters<Callback>) => Promise<readonly [{}, null] | [null, Awaited<ReturnType<Callback>>]>;
|
|
44
|
-
/**
|
|
45
|
-
* Wraps a synchronous function to always return a tuple of [error, result].
|
|
46
|
-
* Returned function never throws; errors are returned as the first tuple element.
|
|
47
|
-
*
|
|
48
|
-
* @template Callback
|
|
49
|
-
* @param {Callback} callback - A synchronous function to wrap.
|
|
50
|
-
* @returns {(...params: Parameters<Callback>) => [NonNull|null, ReturnType<Callback>|null]}
|
|
51
|
-
* Wrapped function returning [error, result].
|
|
52
|
-
* @example
|
|
53
|
-
* const safeCompute = rw(computeValue);
|
|
54
|
-
* const [err, value] = safeCompute(arg1, arg2);
|
|
55
|
-
*/
|
|
56
|
-
export declare const rw: <Callback extends (...params: any[]) => unknown>(callback: Callback) => (...params: Parameters<Callback>) => readonly [{}, null] | [null, ReturnType<Callback>];
|
|
10
|
+
export declare function x<Callback extends (...params: any[]) => Promise<any>>(callback: Callback): (...params: Parameters<Callback>) => Promise<[NonNull, null] | [null, Awaited<ReturnType<Callback>>]>;
|
|
11
|
+
export declare function x<Callback extends (...params: any[]) => any>(callback: Callback): (...params: Parameters<Callback>) => [NonNull, null] | [null, ReturnType<Callback>];
|
|
12
|
+
export declare function w<Callback extends (...params: any[]) => Promise<any>>(callback: Callback, ...params: Parameters<Callback>): Promise<[NonNull, null] | [null, Awaited<ReturnType<Callback>>]>;
|
|
13
|
+
export declare function w<Callback extends (...params: any[]) => any>(callback: Callback, ...params: Parameters<Callback>): [NonNull, null] | [null, ReturnType<Callback>];
|
|
57
14
|
//# sourceMappingURL=safe.d.ts.map
|
package/dist/safe.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"safe.d.ts","sourceRoot":"","sources":["../src/safe.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"safe.d.ts","sourceRoot":"","sources":["../src/safe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvC;;;;GAIG;AACH,qBAAa,SAAU,SAAQ,KAAK;;CAMnC;AAGD,wBAAgB,CAAC,CAAC,QAAQ,SAAS,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,EACpE,QAAQ,EAAE,QAAQ,GAChB,CACF,GAAG,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,KAC3B,OAAO,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtE,wBAAgB,CAAC,CAAC,QAAQ,SAAS,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,EAC3D,QAAQ,EAAE,QAAQ,GAChB,CAAC,GAAG,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC;AAyBvF,wBAAgB,CAAC,CAAC,QAAQ,SAAS,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,EACpE,QAAQ,EAAE,QAAQ,EAClB,GAAG,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,GAC7B,OAAO,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;AAEpE,wBAAgB,CAAC,CAAC,QAAQ,SAAS,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,EAC3D,QAAQ,EAAE,QAAQ,EAClB,GAAG,MAAM,EAAE,UAAU,CAAC,QAAQ,CAAC,GAC7B,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC"}
|
package/dist/safe.js
CHANGED
|
@@ -10,89 +10,21 @@ export class NullError extends Error {
|
|
|
10
10
|
Object.setPrototypeOf(this, NullError.prototype);
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
* Safely executes an async callback, returning a tuple of [error, result].
|
|
15
|
-
* Never throws; errors are returned as the first tuple element.
|
|
16
|
-
*
|
|
17
|
-
* @template Callback
|
|
18
|
-
* @param {Callback} callback - An async function to execute.
|
|
19
|
-
* @returns {Promise<[NonNull|null, Awaited<ReturnType<Callback>>|null]>} Tuple: [error, result].
|
|
20
|
-
* @example
|
|
21
|
-
* const [err, data] = await px(async () => await fetchData());
|
|
22
|
-
*/
|
|
23
|
-
export const px = async (callback) => {
|
|
24
|
-
try {
|
|
25
|
-
const result = await callback();
|
|
26
|
-
return [null, result];
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
if (error === null)
|
|
30
|
-
return [new NullError(), null];
|
|
31
|
-
return [error, null];
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* Safely executes a synchronous callback, returning a tuple of [error, result].
|
|
36
|
-
* Never throws; errors are returned as the first tuple element.
|
|
37
|
-
*
|
|
38
|
-
* @template Callback
|
|
39
|
-
* @param {Callback} callback - A synchronous function to execute.
|
|
40
|
-
* @returns {[NonNull|null, ReturnType<Callback>|null]} Tuple: [error, result].
|
|
41
|
-
* @example
|
|
42
|
-
* const [err, value] = rx(() => computeValue());
|
|
43
|
-
*/
|
|
44
|
-
export const rx = (callback) => {
|
|
45
|
-
try {
|
|
46
|
-
const result = callback();
|
|
47
|
-
return [null, result];
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
if (error === null)
|
|
51
|
-
return [new NullError(), null];
|
|
52
|
-
return [error, null];
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Wraps an async function to always return a tuple of [error, result].
|
|
57
|
-
* Returned function never throws; errors are returned as the first tuple element.
|
|
58
|
-
*
|
|
59
|
-
* @template Callback
|
|
60
|
-
* @param {Callback} callback - An async function to wrap.
|
|
61
|
-
* @returns {(...params: Parameters<Callback>) => Promise<[NonNull|null, Awaited<ReturnType<Callback>>|null]>}
|
|
62
|
-
* Wrapped function returning [error, result].
|
|
63
|
-
* @example
|
|
64
|
-
* const safeFetch = pw(fetchData);
|
|
65
|
-
* const [err, data] = await safeFetch(arg1, arg2);
|
|
66
|
-
*/
|
|
67
|
-
export const pw = (callback) => {
|
|
68
|
-
return async (...params) => {
|
|
69
|
-
try {
|
|
70
|
-
const result = await callback(...params);
|
|
71
|
-
return [null, result];
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
if (error === null)
|
|
75
|
-
return [new NullError(), null];
|
|
76
|
-
return [error, null];
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* Wraps a synchronous function to always return a tuple of [error, result].
|
|
82
|
-
* Returned function never throws; errors are returned as the first tuple element.
|
|
83
|
-
*
|
|
84
|
-
* @template Callback
|
|
85
|
-
* @param {Callback} callback - A synchronous function to wrap.
|
|
86
|
-
* @returns {(...params: Parameters<Callback>) => [NonNull|null, ReturnType<Callback>|null]}
|
|
87
|
-
* Wrapped function returning [error, result].
|
|
88
|
-
* @example
|
|
89
|
-
* const safeCompute = rw(computeValue);
|
|
90
|
-
* const [err, value] = safeCompute(arg1, arg2);
|
|
91
|
-
*/
|
|
92
|
-
export const rw = (callback) => {
|
|
13
|
+
export function x(callback) {
|
|
93
14
|
return (...params) => {
|
|
94
15
|
try {
|
|
95
16
|
const result = callback(...params);
|
|
17
|
+
if (result && typeof result.then === 'function') {
|
|
18
|
+
// It's a Promise (async)
|
|
19
|
+
return result
|
|
20
|
+
.then((value) => [null, value])
|
|
21
|
+
.catch((error) => {
|
|
22
|
+
if (error === null)
|
|
23
|
+
return [new NullError(), null];
|
|
24
|
+
return [error, null];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
// Synchronous
|
|
96
28
|
return [null, result];
|
|
97
29
|
}
|
|
98
30
|
catch (error) {
|
|
@@ -101,5 +33,27 @@ export const rw = (callback) => {
|
|
|
101
33
|
return [error, null];
|
|
102
34
|
}
|
|
103
35
|
};
|
|
104
|
-
}
|
|
36
|
+
}
|
|
37
|
+
export function w(callback, ...params) {
|
|
38
|
+
try {
|
|
39
|
+
const result = callback(...params);
|
|
40
|
+
if (result && typeof result.then === 'function') {
|
|
41
|
+
// It's a Promise (async)
|
|
42
|
+
return result
|
|
43
|
+
.then((value) => [null, value])
|
|
44
|
+
.catch((error) => {
|
|
45
|
+
if (error === null)
|
|
46
|
+
return [new NullError(), null];
|
|
47
|
+
return [error, null];
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
// Synchronous
|
|
51
|
+
return [null, result];
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (error === null)
|
|
55
|
+
return [new NullError(), null];
|
|
56
|
+
return [error, null];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
105
59
|
//# sourceMappingURL=safe.js.map
|
package/dist/safe.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"safe.js","sourceRoot":"","sources":["../src/safe.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,SAAU,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AAE,IAAI,CAAC,CAAC;YACnD,OAAO,CAAC,KAAgB,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;IACF,CAAC,CAAC;AACH,CAAC;AAaD,MAAM,UAAU,CAAC,CAChB,QAAkB,EAClB,GAAG,MAA4B;IAE/B,IAAI,CAAC;QACJ,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,MAAM,CAAC,CAAC;QACnC,IAAI,MAAM,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YACjD,yBAAyB;YACzB,OAAO,MAAM;iBACX,IAAI,CAAC,CAAC,KAAU,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,KAAK,CAA0C,CAAC;iBAC5E,KAAK,CAAC,CAAC,KAAU,EAAE,EAAE;gBACrB,IAAI,KAAK,KAAK,IAAI;oBAAE,OAAO,CAAC,IAAI,SAAS,EAAE,EAAE,IAAI,CAAoB,CAAC;gBACtE,OAAO,CAAC,KAAgB,EAAE,IAAI,CAAoB,CAAC;YACpD,CAAC,CAAC,CAAC;QACL,CAAC;QACD,cAAc;QACd,OAAO,CAAC,IAAI,EAAE,MAA8B,CAAC,CAAC;IAC/C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,CAAC,IAAI,SAAS,EAAE,EAAE,IAAI,CAAC,CAAC;QACnD,OAAO,CAAC,KAAgB,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ntangled/kit",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.4",
|
|
4
4
|
"description": "A TypeScript utility package for safe function execution and other helpers.",
|
|
5
5
|
"author": "Stephan D. <stephan.mdd@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/ntangled/kit#readme",
|
|
7
7
|
"license": "MIT",
|
|
8
|
+
"type": "commonjs",
|
|
8
9
|
"main": "dist/main.js",
|
|
9
10
|
"types": "dist/main.d.ts",
|
|
10
11
|
"files": [
|
|
@@ -30,6 +31,7 @@
|
|
|
30
31
|
"url": "https://github.com/ntangled/kit/issues"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
34
|
+
"ts-node": "^10.9.2",
|
|
33
35
|
"typescript": "^5.9.3"
|
|
34
36
|
}
|
|
35
37
|
}
|