@ntangled/kit 0.0.0-alpha.0 → 0.0.0-alpha.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/README.md +56 -0
- package/dist/safe.d.ts +44 -13
- package/dist/safe.d.ts.map +1 -1
- package/dist/safe.js +52 -16
- package/dist/safe.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
1
|
# @ntangled/kit
|
|
2
2
|
|
|
3
3
|
A TypeScript utililty package.
|
|
4
|
+
|
|
5
|
+
## Safe
|
|
6
|
+
|
|
7
|
+
The `safe.ts` module provides utility functions for safely executing synchronous and asynchronous code, ensuring that errors are handled gracefully and never thrown. Instead, each function returns a tuple of `[error, result]`, making error handling explicit and consistent.
|
|
8
|
+
|
|
9
|
+
> [!IMPORTANT]
|
|
10
|
+
> Throwing `null` will result in a NullError in the error entry. Use `instance of NullError` if you expect `null` to be thrown.
|
|
11
|
+
|
|
12
|
+
### Utilities
|
|
13
|
+
|
|
14
|
+
- **NullError**: Custom error thrown when a `null` value is encountered where not expected.
|
|
15
|
+
- **px**: Safely executes an async callback, returns `[error, result]`.
|
|
16
|
+
- **rx**: Safely executes a sync callback, returns `[error, result]`.
|
|
17
|
+
- **pw**: Wraps an async function, returns a new function that always returns `[error, result]`.
|
|
18
|
+
- **rw**: Wraps a sync function, returns a new function that always returns `[error, result]`.
|
|
19
|
+
|
|
20
|
+
### Usage
|
|
21
|
+
|
|
22
|
+
#### px (Promise Executor)
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const [err, data] = await px(async () => await fetchData());
|
|
26
|
+
if (err) {
|
|
27
|
+
// handle error
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
#### rx (Result Executor)
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const [err, value] = rx(() => computeValue());
|
|
35
|
+
if (err) {
|
|
36
|
+
// handle error
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### pw (Promise Wrapper)
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
const safeFetch = pw(fetchData);
|
|
44
|
+
const [err, data] = await safeFetch(arg1, arg2);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### rw (Result Wrapper)
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
const safeCompute = rw(computeValue);
|
|
51
|
+
const [err, value] = safeCompute(arg1, arg2);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Why use Safe utilities?
|
|
55
|
+
|
|
56
|
+
- Avoids try/catch boilerplate.
|
|
57
|
+
- Makes error handling explicit and consistent.
|
|
58
|
+
- Works for both sync and async functions.
|
|
59
|
+
- Prevents uncaught exceptions and null errors.
|
package/dist/safe.d.ts
CHANGED
|
@@ -1,26 +1,57 @@
|
|
|
1
|
-
export type NonNull = NonNullable<unknown>;
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
2
|
+
* Error thrown when a null value is encountered where not expected.
|
|
3
|
+
* @class NullError
|
|
4
|
+
* @extends Error
|
|
5
|
+
*/
|
|
6
|
+
export declare class NullError extends Error {
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Safely executes an async callback, returning a tuple of [error, result].
|
|
11
|
+
* Never throws; errors are returned as the first tuple element.
|
|
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());
|
|
6
18
|
*/
|
|
7
19
|
export declare const px: <Callback extends () => unknown>(callback: Callback) => Promise<[null, Awaited<ReturnType<Callback>>] | readonly [{}, null]>;
|
|
8
20
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
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());
|
|
12
29
|
*/
|
|
13
30
|
export declare const rx: <Callback extends () => unknown>(callback: Callback) => readonly [{}, null] | [null, ReturnType<Callback>];
|
|
14
31
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
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);
|
|
18
42
|
*/
|
|
19
43
|
export declare const pw: <Callback extends (...params: any[]) => unknown>(callback: Callback) => (...params: Parameters<Callback>) => Promise<readonly [{}, null] | [null, Awaited<ReturnType<Callback>>]>;
|
|
20
44
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
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);
|
|
24
55
|
*/
|
|
25
56
|
export declare const rw: <Callback extends (...params: any[]) => unknown>(callback: Callback) => (...params: Parameters<Callback>) => readonly [{}, null] | [null, ReturnType<Callback>];
|
|
26
57
|
//# 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":"AAEA;;;;GAIG;AACH,qBAAa,SAAU,SAAQ,KAAK;;CAMnC;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,EAAE,GAAU,QAAQ,SAAS,MAAM,OAAO,EAAE,UAAU,QAAQ,yEAQ1E,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,EAAE,GAAI,QAAQ,SAAS,MAAM,OAAO,EAAE,UAAU,QAAQ,uDAQpE,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,EAAE,GAAI,QAAQ,SAAS,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,EAAE,UAAU,QAAQ,MACtE,GAAG,QAAQ,UAAU,CAAC,QAAQ,CAAC,yEAS7C,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,EAAE,GAAI,QAAQ,SAAS,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,EAAE,UAAU,QAAQ,MAC5E,GAAG,QAAQ,UAAU,CAAC,QAAQ,CAAC,uDASvC,CAAC"}
|
package/dist/safe.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @
|
|
4
|
-
* @
|
|
2
|
+
* Error thrown when a null value is encountered where not expected.
|
|
3
|
+
* @class NullError
|
|
4
|
+
* @extends Error
|
|
5
|
+
*/
|
|
6
|
+
export class NullError extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super('null');
|
|
9
|
+
this.name = 'NullError';
|
|
10
|
+
Object.setPrototypeOf(this, NullError.prototype);
|
|
11
|
+
}
|
|
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());
|
|
5
22
|
*/
|
|
6
23
|
export const px = async (callback) => {
|
|
7
24
|
try {
|
|
@@ -10,14 +27,19 @@ export const px = async (callback) => {
|
|
|
10
27
|
}
|
|
11
28
|
catch (error) {
|
|
12
29
|
if (error === null)
|
|
13
|
-
return [new
|
|
30
|
+
return [new NullError(), null];
|
|
14
31
|
return [error, null];
|
|
15
32
|
}
|
|
16
33
|
};
|
|
17
34
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
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());
|
|
21
43
|
*/
|
|
22
44
|
export const rx = (callback) => {
|
|
23
45
|
try {
|
|
@@ -26,14 +48,21 @@ export const rx = (callback) => {
|
|
|
26
48
|
}
|
|
27
49
|
catch (error) {
|
|
28
50
|
if (error === null)
|
|
29
|
-
return [new
|
|
51
|
+
return [new NullError(), null];
|
|
30
52
|
return [error, null];
|
|
31
53
|
}
|
|
32
54
|
};
|
|
33
55
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
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);
|
|
37
66
|
*/
|
|
38
67
|
export const pw = (callback) => {
|
|
39
68
|
return async (...params) => {
|
|
@@ -43,15 +72,22 @@ export const pw = (callback) => {
|
|
|
43
72
|
}
|
|
44
73
|
catch (error) {
|
|
45
74
|
if (error === null)
|
|
46
|
-
return [new
|
|
75
|
+
return [new NullError(), null];
|
|
47
76
|
return [error, null];
|
|
48
77
|
}
|
|
49
78
|
};
|
|
50
79
|
};
|
|
51
80
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
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);
|
|
55
91
|
*/
|
|
56
92
|
export const rw = (callback) => {
|
|
57
93
|
return (...params) => {
|
|
@@ -61,7 +97,7 @@ export const rw = (callback) => {
|
|
|
61
97
|
}
|
|
62
98
|
catch (error) {
|
|
63
99
|
if (error === null)
|
|
64
|
-
return [new
|
|
100
|
+
return [new NullError(), null];
|
|
65
101
|
return [error, null];
|
|
66
102
|
}
|
|
67
103
|
};
|
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,CAAC,MAAM,EAAE,GAAG,KAAK,EAAkC,QAAkB,EAAE,EAAE;IAC9E,IAAI,CAAC;QACJ,MAAM,MAAM,GAAG,MAAM,QAAQ,EAAE,CAAC;QAChC,OAAO,CAAC,IAAI,EAAE,MAAM,CAA0C,CAAC;IAChE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"safe.js","sourceRoot":"","sources":["../src/safe.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IACnC;QACC,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IAClD,CAAC;CACD;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,KAAK,EAAkC,QAAkB,EAAE,EAAE;IAC9E,IAAI,CAAC;QACJ,MAAM,MAAM,GAAG,MAAM,QAAQ,EAAE,CAAC;QAChC,OAAO,CAAC,IAAI,EAAE,MAAM,CAA0C,CAAC;IAChE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,K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,QAAQ,CAAC,GAAG,MAAM,CAAC,CAAC;YACnC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAiC,CAAC;QACvD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,KAAK,KAAK,IAAI;gBAAE,OAAO,CAAC,IAAI,SAAS,EAAa,EAAE,IAAI,CAAU,CAAC;YACvE,OAAO,CAAC,KAAgB,EAAE,IAAI,CAAU,CAAC;QAC1C,CAAC;IACF,CAAC,CAAC;AACH,CAAC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|