@gershy/clearing 0.0.17 → 0.0.20
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/cmp/cjs/main.js +1 -1
- package/cmp/mjs/main.js +1 -1
- package/cmp/sideEffects.d.ts +1 -1
- package/package.json +3 -4
- package/readme.md +86 -0
package/cmp/cjs/main.js
CHANGED
|
@@ -459,7 +459,7 @@ const applyClearing = (() => {
|
|
|
459
459
|
return Object.assign(this, (0, exports.inCls)(cause, Error) ? cause : {}, moreProps, cause ? { message, cause } : { message });
|
|
460
460
|
},
|
|
461
461
|
[suppress]() {
|
|
462
|
-
this[Symbol.for('clearing.err.suppressed')] = true;
|
|
462
|
+
this[Symbol.for('@gershy.clearing.err.suppressed')] = true;
|
|
463
463
|
if (this.cause) {
|
|
464
464
|
const causes = (0, exports.inCls)(this.cause, Error) ? [this.cause] : this.cause;
|
|
465
465
|
for (const err of causes)
|
package/cmp/mjs/main.js
CHANGED
|
@@ -450,7 +450,7 @@ const applyClearing = (() => {
|
|
|
450
450
|
return Object.assign(this, inCls(cause, Error) ? cause : {}, moreProps, cause ? { message, cause } : { message });
|
|
451
451
|
},
|
|
452
452
|
[suppress]() {
|
|
453
|
-
this[Symbol.for('clearing.err.suppressed')] = true;
|
|
453
|
+
this[Symbol.for('@gershy.clearing.err.suppressed')] = true;
|
|
454
454
|
if (this.cause) {
|
|
455
455
|
const causes = inCls(this.cause, Error) ? [this.cause] : this.cause;
|
|
456
456
|
for (const err of causes)
|
package/cmp/sideEffects.d.ts
CHANGED
|
@@ -204,7 +204,7 @@ declare global {
|
|
|
204
204
|
|
|
205
205
|
interface PromiseConstructor {
|
|
206
206
|
[allArr]: PromiseConstructor['all'],
|
|
207
|
-
[allObj]: <O extends { [K: string]: Promise<any> }>(obj: O) => Promise<{ [K in keyof O]: Awaited<O[K]> }>,
|
|
207
|
+
[allObj]: <O extends { [K: string]: Promise<any> }>(obj: O) => Promise<{ [K in keyof O]: Exclude<Awaited<O[K]>, Skip> }>,
|
|
208
208
|
[later]: <T=void>() => PromiseLater<T>
|
|
209
209
|
}
|
|
210
210
|
interface Promise<T> {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gershy/clearing",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
|
+
"versionCommit": "ccce646c1a4bc2ec3f349a1b02a8498a930c373e",
|
|
4
5
|
"description": "Adds the features you always wish javascript had!",
|
|
5
6
|
"keywords": [
|
|
6
7
|
"clearing",
|
|
@@ -22,7 +23,6 @@
|
|
|
22
23
|
"tsx": "^4.21.0",
|
|
23
24
|
"typescript": "^5.9.3"
|
|
24
25
|
},
|
|
25
|
-
|
|
26
26
|
"type": "module",
|
|
27
27
|
"files": [
|
|
28
28
|
"cmp"
|
|
@@ -34,7 +34,6 @@
|
|
|
34
34
|
"require": "./cmp/cjs/main.js"
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
|
-
|
|
38
37
|
"scripts": {
|
|
39
38
|
"test": "npm run ts.check && npx tsx ./src/main.test.ts",
|
|
40
39
|
"ts.check": "npx tsc --noEmit",
|
|
@@ -45,4 +44,4 @@
|
|
|
45
44
|
"npm.login": "npm login",
|
|
46
45
|
"npm.pub": "npm run test && npm run build && npm publish --access public"
|
|
47
46
|
}
|
|
48
|
-
}
|
|
47
|
+
}
|
package/readme.md
CHANGED
|
@@ -32,6 +32,92 @@ Symbol properties maintains compatibility with the vast majority of npm modules.
|
|
|
32
32
|
more thing - the symbols used to extend functionality are set in the global scope, so they are
|
|
33
33
|
always globally available.
|
|
34
34
|
|
|
35
|
+
## Exported helpers
|
|
36
|
+
|
|
37
|
+
In addition to prototype extensions, this module exports several utility helpers for direct import:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { isCls, inCls, getClsName, getCls, skip, then, safe } from '@gershy/clearing';
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### `isCls(value, Constructor)`
|
|
44
|
+
Type guard for strict class/constructor matching. Handles primitives, null, undefined, and NaN correctly.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
isCls(5, Number); // true
|
|
48
|
+
isCls(5, NaN); // false
|
|
49
|
+
isCls(5, String); // false
|
|
50
|
+
isCls('abc', String); // true
|
|
51
|
+
isCls(null, null); // true
|
|
52
|
+
isCls(undefined, undefined); // true
|
|
53
|
+
isCls(null, undefined); // false
|
|
54
|
+
isCls(undefined, null); // false
|
|
55
|
+
isCls(NaN, NaN); // true
|
|
56
|
+
isCls({}, Object); // true
|
|
57
|
+
isCls([], Array); // true
|
|
58
|
+
isCls(new Map(), Map); // true
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### `inCls(value, Constructor)`
|
|
62
|
+
Type guard for `instanceof` checks (including inheritance).
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
inCls([], Array); // true
|
|
66
|
+
inCls(new (class X extends Array {})(), Array); // true
|
|
67
|
+
inCls({}, Object); // true
|
|
68
|
+
inCls(5, Number); // false (primitives are not instanceof)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### `getClsName(value)`
|
|
72
|
+
Returns the class/constructor name of a value, or special names for null/undefined/NaN.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
getClsName(5); // 'Number'
|
|
76
|
+
getClsName('abc'); // 'String'
|
|
77
|
+
getClsName(null); // 'Null'
|
|
78
|
+
getClsName(undefined); // 'Undef'
|
|
79
|
+
getClsName(NaN); // 'Nan'
|
|
80
|
+
getClsName({}); // 'Object'
|
|
81
|
+
getClsName(Object.create(null)); // 'Prototypeless'
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `getCls(value)`
|
|
85
|
+
Returns the constructor function of a value, or null if not available.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
getCls(5); // Number
|
|
89
|
+
getCls('abc'); // String
|
|
90
|
+
getCls(null); // null
|
|
91
|
+
getCls(undefined); // null
|
|
92
|
+
getCls({}); // Object
|
|
93
|
+
getCls(Object.create(null)); // null
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `skip`
|
|
97
|
+
Special exported value (undefined) used to signal omission in mapping helpers.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { skip } from '@gershy/clearing';
|
|
101
|
+
[1, 2, 3].map(v => v > 1 ? v : skip); // [skip, 2, 3]
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### `then(value, onFulfilled?, onRejected?)`
|
|
105
|
+
Unified handler for both Promise and non-Promise values. Applies `onFulfilled`/`onRejected` as appropriate.
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
then(Promise.resolve(5), v => v * 2); // Promise resolving to 10
|
|
109
|
+
then(5, v => v * 2); // 10
|
|
110
|
+
then(Promise.reject('fail'), undefined, e => 'fallback'); // Promise resolving to 'fallback'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### `safe(fn, onRejected?)`
|
|
114
|
+
Runs a function (sync or async), catching errors and returning a Promise. Optionally handles errors with `onRejected`.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
await safe(() => JSON.parse('{ bad }'), e => 'fallback'); // 'fallback'
|
|
118
|
+
await safe(async () => await fetch('bad-url'), e => null); // null if fetch throws
|
|
119
|
+
```
|
|
120
|
+
|
|
35
121
|
## `Object` extensions
|
|
36
122
|
|
|
37
123
|
There are currently no extensions on the `Object` class.
|