@gershy/clearing 0.0.15 → 0.0.17
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.d.ts +27 -0
- package/cmp/cjs/main.js +88 -59
- package/cmp/mjs/main.d.ts +27 -0
- package/cmp/mjs/main.js +45 -23
- package/cmp/sideEffects.d.ts +0 -31
- package/package.json +7 -4
package/cmp/cjs/main.d.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
1
|
import '../sideEffects.js';
|
|
2
|
+
type ClsCheck = {
|
|
3
|
+
(i: unknown, num: BooleanConstructor): i is boolean;
|
|
4
|
+
(i: unknown, num: NumberConstructor): i is number;
|
|
5
|
+
(i: unknown, str: StringConstructor): i is string;
|
|
6
|
+
(i: unknown, buff: BufferConstructor): i is Buffer;
|
|
7
|
+
(i: unknown, arr: ArrayConstructor): i is any[];
|
|
8
|
+
(i: unknown, obj: ObjectConstructor): i is Obj<unknown>;
|
|
9
|
+
(i: unknown, fn: FunctionConstructor): i is Fn;
|
|
10
|
+
(i: unknown, fn: SymbolConstructor): i is symbol;
|
|
11
|
+
<T>(i: unknown, prm: PromiseConstructor): i is Promise<T>;
|
|
12
|
+
<C extends abstract new (...args: any) => any>(i: unknown, cls: C): i is InstanceType<C>;
|
|
13
|
+
};
|
|
14
|
+
export declare const getClsName: (i: any) => any;
|
|
15
|
+
export declare const getCls: (i: any) => any;
|
|
16
|
+
export declare const isCls: ClsCheck;
|
|
17
|
+
export declare const inCls: ClsCheck;
|
|
18
|
+
export declare const skip: undefined;
|
|
19
|
+
type Then = {
|
|
20
|
+
<V, R0 = V, R1 = never>(val: Promise<V>, rsv?: (v: V) => R0, rjc?: (e: any) => R1): Promise<R0 | R1>;
|
|
21
|
+
<V, R0 = V, R1 = never>(val: V, rsv?: (v: V) => R0, rjc?: (e: any) => R1): R0 | R1;
|
|
22
|
+
};
|
|
23
|
+
export declare const then: Then;
|
|
24
|
+
type Safe = {
|
|
25
|
+
<V, R0 = never>(fn: () => Promise<V>, rjc?: (e: any) => R0): Promise<V | R0>;
|
|
26
|
+
<V, R0 = never>(fn: () => V, rjc?: (e: any) => R0): Promise<V | R0>;
|
|
27
|
+
};
|
|
28
|
+
export declare const safe: Safe;
|
|
2
29
|
declare const applyClearing: () => void;
|
|
3
30
|
export default applyClearing;
|
package/cmp/cjs/main.js
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.safe = exports.then = exports.skip = exports.inCls = exports.isCls = exports.getCls = exports.getClsName = void 0;
|
|
4
|
+
const getClsName = i => {
|
|
5
|
+
if (i === null)
|
|
6
|
+
return 'Null';
|
|
7
|
+
if (i === undefined)
|
|
8
|
+
return 'Undef';
|
|
9
|
+
if (i !== i)
|
|
10
|
+
return 'Nan';
|
|
11
|
+
return Object.getPrototypeOf(i)?.constructor.name ?? 'Prototypeless';
|
|
12
|
+
};
|
|
13
|
+
exports.getClsName = getClsName;
|
|
14
|
+
const getCls = i => Object.getPrototypeOf(i)?.constructor ?? null;
|
|
15
|
+
exports.getCls = getCls;
|
|
16
|
+
const isCls = (i, C) => {
|
|
17
|
+
// NaN only matches against the NaN primitive (not the Number Form)
|
|
18
|
+
if (i !== i)
|
|
19
|
+
return C !== C;
|
|
20
|
+
// `null` and `undefined` only match to themselves
|
|
21
|
+
if (i == null)
|
|
22
|
+
return i === C;
|
|
23
|
+
// Otherwise strictly check the constructor
|
|
24
|
+
return Object.getPrototypeOf(i).constructor === C;
|
|
25
|
+
};
|
|
26
|
+
exports.isCls = isCls;
|
|
27
|
+
const inCls = (i, C) => i instanceof C;
|
|
28
|
+
exports.inCls = inCls;
|
|
29
|
+
exports.skip = undefined;
|
|
30
|
+
const then = (val, rsv = (v => v), rjc = ((e) => { throw e; })) => {
|
|
31
|
+
// Act on `val` regardless of whether it's a Promise or immediate value; return `rsv(val)`
|
|
32
|
+
// either immediately or as a Promise
|
|
33
|
+
if ((0, exports.inCls)(val, Promise))
|
|
34
|
+
return val.then(rsv).catch(rjc);
|
|
35
|
+
try {
|
|
36
|
+
return rsv(val);
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
return rjc(err);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.then = then;
|
|
43
|
+
const safe = (fn, rjc = e => { throw e; }) => {
|
|
44
|
+
// Execute a function which returns a value either synchronously or asynchronously; in both cases
|
|
45
|
+
// allows errors occurring from function execution to be handled
|
|
46
|
+
try {
|
|
47
|
+
return (0, exports.then)(fn(), v => v, rjc);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
return rjc(err);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
exports.safe = safe;
|
|
3
54
|
const applyClearing = (() => {
|
|
4
55
|
const global = globalThis;
|
|
5
56
|
// Prevent multiple installations...
|
|
@@ -61,42 +112,20 @@ const applyClearing = (() => {
|
|
|
61
112
|
// </SYMBOLS>
|
|
62
113
|
];
|
|
63
114
|
const syms = Object.fromEntries(symNames.map(term => [term, Symbol(`${pfx}:${term}`)]));
|
|
64
|
-
|
|
65
|
-
if (i === null)
|
|
66
|
-
return 'Null';
|
|
67
|
-
if (i === undefined)
|
|
68
|
-
return 'Undef';
|
|
69
|
-
if (i !== i)
|
|
70
|
-
return 'Nan';
|
|
71
|
-
return Object.getPrototypeOf(i)?.constructor.name ?? 'Prototypeless';
|
|
72
|
-
};
|
|
73
|
-
const getCls = i => Object.getPrototypeOf(i)?.constructor ?? null;
|
|
74
|
-
const isCls = (i, C) => {
|
|
75
|
-
// NaN only matches against the NaN primitive (not the Number Form)
|
|
76
|
-
if (i !== i)
|
|
77
|
-
return C !== C;
|
|
78
|
-
// `null` and `undefined` only match to themselves
|
|
79
|
-
if (i == null)
|
|
80
|
-
return i === C;
|
|
81
|
-
// Otherwise strictly check the constructor
|
|
82
|
-
return Object.getPrototypeOf(i).constructor === C;
|
|
83
|
-
};
|
|
84
|
-
const inCls = (i, C) => i instanceof C;
|
|
85
|
-
const skip = undefined;
|
|
86
|
-
Object.assign(global, { ...syms, clearing: { getClsName, getCls, isCls, inCls } });
|
|
115
|
+
Object.assign(global, { ...syms });
|
|
87
116
|
const protoDefs = (Cls, def) => {
|
|
88
117
|
let protoVals = [];
|
|
89
118
|
let classVals = [];
|
|
90
119
|
for (const key of Reflect.ownKeys(def)) {
|
|
91
|
-
if (key !== '$' && !isCls(key, Symbol))
|
|
92
|
-
throw Object.assign(Error('invalid proto key'), { Cls, keyClsName: getClsName(key), key });
|
|
120
|
+
if (key !== '$' && !(0, exports.isCls)(key, Symbol))
|
|
121
|
+
throw Object.assign(Error('invalid proto key'), { Cls, keyClsName: (0, exports.getClsName)(key), key });
|
|
93
122
|
if (key !== '$') {
|
|
94
123
|
protoVals.push([key, def[key]]);
|
|
95
124
|
}
|
|
96
125
|
else {
|
|
97
126
|
for (const k of Reflect.ownKeys(def[key])) {
|
|
98
|
-
if (!isCls(k, Symbol))
|
|
99
|
-
throw Object.assign(Error('invalid class key'), { Cls, keyClsName: getClsName(k), key: k });
|
|
127
|
+
if (!(0, exports.isCls)(k, Symbol))
|
|
128
|
+
throw Object.assign(Error('invalid class key'), { Cls, keyClsName: (0, exports.getClsName)(k), key: k });
|
|
100
129
|
classVals.push([k, def[key][k]]);
|
|
101
130
|
}
|
|
102
131
|
}
|
|
@@ -107,9 +136,9 @@ const applyClearing = (() => {
|
|
|
107
136
|
Object.defineProperty(target, sym, { enumerable: false, value });
|
|
108
137
|
};
|
|
109
138
|
protoDefs(Object, {
|
|
110
|
-
[at](cmps, def = skip) {
|
|
139
|
+
[at](cmps, def = exports.skip) {
|
|
111
140
|
let ptr = this;
|
|
112
|
-
if (!isCls(cmps, Array))
|
|
141
|
+
if (!(0, exports.isCls)(cmps, Array))
|
|
113
142
|
cmps = [cmps];
|
|
114
143
|
for (const c of cmps) {
|
|
115
144
|
if (ptr[has](c))
|
|
@@ -133,7 +162,7 @@ const applyClearing = (() => {
|
|
|
133
162
|
const ret = {};
|
|
134
163
|
for (const [k, v] of this) {
|
|
135
164
|
const g = fn(v, k);
|
|
136
|
-
if (g === skip)
|
|
165
|
+
if (g === exports.skip)
|
|
137
166
|
continue;
|
|
138
167
|
if (!ret[has](g))
|
|
139
168
|
ret[g] = {};
|
|
@@ -146,7 +175,7 @@ const applyClearing = (() => {
|
|
|
146
175
|
const ret = Object.assign({}, this);
|
|
147
176
|
for (const k in ret) {
|
|
148
177
|
const v = fn(ret[k], k);
|
|
149
|
-
if (v !== skip)
|
|
178
|
+
if (v !== exports.skip)
|
|
150
179
|
ret[k] = v;
|
|
151
180
|
else
|
|
152
181
|
delete ret[k];
|
|
@@ -157,7 +186,7 @@ const applyClearing = (() => {
|
|
|
157
186
|
const arr = [];
|
|
158
187
|
for (const k in this) {
|
|
159
188
|
const r = fn(this[k], k);
|
|
160
|
-
if (r !== skip)
|
|
189
|
+
if (r !== exports.skip)
|
|
161
190
|
arr.push(r);
|
|
162
191
|
}
|
|
163
192
|
return Object.fromEntries(arr);
|
|
@@ -165,17 +194,17 @@ const applyClearing = (() => {
|
|
|
165
194
|
[merge](o) {
|
|
166
195
|
for (const [k, v] of o) {
|
|
167
196
|
// `skip` can be passed to remove properties
|
|
168
|
-
if (v === skip) {
|
|
197
|
+
if (v === exports.skip) {
|
|
169
198
|
delete this[k];
|
|
170
199
|
continue;
|
|
171
200
|
}
|
|
172
201
|
// Incoming non-Object properties are simple
|
|
173
|
-
if (!isCls(v, Object)) {
|
|
202
|
+
if (!(0, exports.isCls)(v, Object)) {
|
|
174
203
|
this[k] = v;
|
|
175
204
|
continue;
|
|
176
205
|
}
|
|
177
206
|
// `v` is an Object; existing non-Object replaced with `{}`
|
|
178
|
-
if (!this[has](k) || !isCls(this[k], Object))
|
|
207
|
+
if (!this[has](k) || !(0, exports.isCls)(this[k], Object))
|
|
179
208
|
this[k] = {};
|
|
180
209
|
// And simply recurse!
|
|
181
210
|
this[k][merge](v);
|
|
@@ -191,13 +220,13 @@ const applyClearing = (() => {
|
|
|
191
220
|
[slice](p) {
|
|
192
221
|
// >> { a: 1, b: 2, c: 3, d: 4 }.slice([ 'b', 'd' ]);
|
|
193
222
|
// { b: 2, d: 4 }
|
|
194
|
-
return p[toObj](p => this[has](p) ? [p, this[p]] : skip);
|
|
223
|
+
return p[toObj](p => this[has](p) ? [p, this[p]] : exports.skip);
|
|
195
224
|
},
|
|
196
225
|
[toArr](fn) {
|
|
197
226
|
const ret = [];
|
|
198
227
|
for (const k in this) {
|
|
199
228
|
const r = fn(this[k], k);
|
|
200
|
-
if (r !== skip)
|
|
229
|
+
if (r !== exports.skip)
|
|
201
230
|
ret.push(r);
|
|
202
231
|
}
|
|
203
232
|
return ret;
|
|
@@ -226,7 +255,7 @@ const applyClearing = (() => {
|
|
|
226
255
|
const ret = {};
|
|
227
256
|
for (const elem of this) {
|
|
228
257
|
const g = fn(elem);
|
|
229
|
-
if (g === skip)
|
|
258
|
+
if (g === exports.skip)
|
|
230
259
|
continue;
|
|
231
260
|
if (!ret[has](g))
|
|
232
261
|
ret[g] = [];
|
|
@@ -240,7 +269,7 @@ const applyClearing = (() => {
|
|
|
240
269
|
const len = this.length;
|
|
241
270
|
for (let i = 0; i < len; i++) {
|
|
242
271
|
const r = it(this[i], i);
|
|
243
|
-
if (r !== skip)
|
|
272
|
+
if (r !== exports.skip)
|
|
244
273
|
ret.push(r);
|
|
245
274
|
}
|
|
246
275
|
return ret;
|
|
@@ -252,7 +281,7 @@ const applyClearing = (() => {
|
|
|
252
281
|
const len = this.length;
|
|
253
282
|
for (let i = 0; i < len; i++) {
|
|
254
283
|
const r = it(this[i], i);
|
|
255
|
-
if (r !== skip)
|
|
284
|
+
if (r !== exports.skip)
|
|
256
285
|
ret.push(r);
|
|
257
286
|
}
|
|
258
287
|
return Object.fromEntries(ret);
|
|
@@ -264,7 +293,7 @@ const applyClearing = (() => {
|
|
|
264
293
|
return str.split('\n')[map](ln => {
|
|
265
294
|
const ind = ln.indexOf(seq);
|
|
266
295
|
if (ind === -1)
|
|
267
|
-
return skip;
|
|
296
|
+
return exports.skip;
|
|
268
297
|
return ln.slice(ind + seq.length);
|
|
269
298
|
}).join('\n');
|
|
270
299
|
},
|
|
@@ -299,7 +328,7 @@ const applyClearing = (() => {
|
|
|
299
328
|
[count]() { return this.length; },
|
|
300
329
|
[cut](delim, cuts = 1) {
|
|
301
330
|
// `cuts` defines # of cuts (resulting array length is `num + 1`)
|
|
302
|
-
const split = this.split(delim, cuts < Infinity ? cuts : skip);
|
|
331
|
+
const split = this.split(delim, cuts < Infinity ? cuts : exports.skip);
|
|
303
332
|
const numDelimsSplit = split.length - 1;
|
|
304
333
|
const lenConsumed = 0
|
|
305
334
|
+ split.reduce((a, s) => a + s.length, 0)
|
|
@@ -315,7 +344,7 @@ const applyClearing = (() => {
|
|
|
315
344
|
if (!this)
|
|
316
345
|
return this; // No-op on empty String (otherwise it would transform a 0-line string to a 1-line string)
|
|
317
346
|
let indentStr;
|
|
318
|
-
if (isCls(args[0], String)) {
|
|
347
|
+
if ((0, exports.isCls)(args[0], String)) {
|
|
319
348
|
indentStr = args[0];
|
|
320
349
|
}
|
|
321
350
|
else {
|
|
@@ -328,7 +357,7 @@ const applyClearing = (() => {
|
|
|
328
357
|
[padHead]: String.prototype.padStart,
|
|
329
358
|
[padTail]: String.prototype.padEnd,
|
|
330
359
|
[toNum](cs = String[base62]) {
|
|
331
|
-
if (isCls(cs, String))
|
|
360
|
+
if ((0, exports.isCls)(cs, String))
|
|
332
361
|
cs = String[charset](cs);
|
|
333
362
|
const base = cs.size;
|
|
334
363
|
if (base === 1n)
|
|
@@ -354,7 +383,7 @@ const applyClearing = (() => {
|
|
|
354
383
|
const ret = [];
|
|
355
384
|
for (let i = 0; i < this; i++) {
|
|
356
385
|
const r = fn(i);
|
|
357
|
-
if (r !== skip)
|
|
386
|
+
if (r !== exports.skip)
|
|
358
387
|
ret.push(r);
|
|
359
388
|
}
|
|
360
389
|
return Object.fromEntries(ret);
|
|
@@ -365,7 +394,7 @@ const applyClearing = (() => {
|
|
|
365
394
|
// | (n).encodeStr(singleChr)
|
|
366
395
|
// is always equivalent to
|
|
367
396
|
// | singleChr.repeat(n - 1)
|
|
368
|
-
if (isCls(cs, String))
|
|
397
|
+
if ((0, exports.isCls)(cs, String))
|
|
369
398
|
cs = String[charset](cs);
|
|
370
399
|
const base = cs.size;
|
|
371
400
|
if (base === 1n && padLen)
|
|
@@ -409,17 +438,17 @@ const applyClearing = (() => {
|
|
|
409
438
|
seen.set(this, 'cycle(Error)');
|
|
410
439
|
const { message, stack, cause, ...props } = this;
|
|
411
440
|
return {
|
|
412
|
-
form: getClsName(this),
|
|
441
|
+
form: (0, exports.getClsName)(this),
|
|
413
442
|
msg: message,
|
|
414
|
-
trace: stack?.split('\n').slice(1)[map](v => v.trim() ?? skip) ?? [],
|
|
443
|
+
trace: stack?.split('\n').slice(1)[map](v => v.trim() ?? exports.skip) ?? [],
|
|
415
444
|
...props,
|
|
416
445
|
cause: !cause ? null : cause[limn](seen)
|
|
417
446
|
};
|
|
418
447
|
},
|
|
419
448
|
[mod](props = {} /* { cause, msg, message, ...more } */) {
|
|
420
|
-
if (isCls(props, Function))
|
|
449
|
+
if ((0, exports.isCls)(props, Function))
|
|
421
450
|
props = props(this.message, this);
|
|
422
|
-
if (isCls(props, String))
|
|
451
|
+
if ((0, exports.isCls)(props, String))
|
|
423
452
|
props = { message: props };
|
|
424
453
|
const { cause = null, msg = null, message = msg ?? this.message, ...moreProps } = props;
|
|
425
454
|
// - Assign `cause` to transfer props like fs "code" props, etc. - watch out, `cause` may be
|
|
@@ -427,12 +456,12 @@ const applyClearing = (() => {
|
|
|
427
456
|
// - Assign `moreProps` to transfer any other properties
|
|
428
457
|
// - Add `message` prop
|
|
429
458
|
// - Only add `cause` prop if `cause` is non-null
|
|
430
|
-
return Object.assign(this, inCls(cause, Error) ? cause : {}, moreProps, cause ? { message, cause } : { message });
|
|
459
|
+
return Object.assign(this, (0, exports.inCls)(cause, Error) ? cause : {}, moreProps, cause ? { message, cause } : { message });
|
|
431
460
|
},
|
|
432
461
|
[suppress]() {
|
|
433
462
|
this[Symbol.for('clearing.err.suppressed')] = true;
|
|
434
463
|
if (this.cause) {
|
|
435
|
-
const causes = inCls(this.cause, Error) ? [this.cause] : this.cause;
|
|
464
|
+
const causes = (0, exports.inCls)(this.cause, Error) ? [this.cause] : this.cause;
|
|
436
465
|
for (const err of causes)
|
|
437
466
|
err[suppress]();
|
|
438
467
|
}
|
|
@@ -448,7 +477,7 @@ const applyClearing = (() => {
|
|
|
448
477
|
return Promise.all(Object.values(obj)).then(vals => {
|
|
449
478
|
const ret = {};
|
|
450
479
|
for (const [i, k] of keys.entries())
|
|
451
|
-
if (vals[i] !== skip)
|
|
480
|
+
if (vals[i] !== exports.skip)
|
|
452
481
|
ret[k] = vals[i];
|
|
453
482
|
return ret;
|
|
454
483
|
});
|
|
@@ -473,7 +502,7 @@ const applyClearing = (() => {
|
|
|
473
502
|
let ind = 0;
|
|
474
503
|
for (const item of this) {
|
|
475
504
|
const r = fn(item, ind++);
|
|
476
|
-
if (r !== skip)
|
|
505
|
+
if (r !== exports.skip)
|
|
477
506
|
ret.push(r);
|
|
478
507
|
}
|
|
479
508
|
return ret;
|
|
@@ -484,7 +513,7 @@ const applyClearing = (() => {
|
|
|
484
513
|
let ind = 0;
|
|
485
514
|
for (const item of this) {
|
|
486
515
|
const r = fn(item, ind++);
|
|
487
|
-
if (r !== skip)
|
|
516
|
+
if (r !== exports.skip)
|
|
488
517
|
ret.push(r);
|
|
489
518
|
}
|
|
490
519
|
return ret;
|
|
@@ -493,7 +522,7 @@ const applyClearing = (() => {
|
|
|
493
522
|
const ret = [];
|
|
494
523
|
for (const item of this) {
|
|
495
524
|
const r = fn(item);
|
|
496
|
-
if (r !== skip)
|
|
525
|
+
if (r !== exports.skip)
|
|
497
526
|
ret.push(r);
|
|
498
527
|
}
|
|
499
528
|
return Object.fromEntries(ret);
|
|
@@ -513,7 +542,7 @@ const applyClearing = (() => {
|
|
|
513
542
|
const ret = [];
|
|
514
543
|
for (const [k, v] of this) {
|
|
515
544
|
const r = fn(v, k);
|
|
516
|
-
if (r !== skip)
|
|
545
|
+
if (r !== exports.skip)
|
|
517
546
|
ret.push(r);
|
|
518
547
|
}
|
|
519
548
|
return Object.fromEntries(ret);
|
|
@@ -523,7 +552,7 @@ const applyClearing = (() => {
|
|
|
523
552
|
const ret = [];
|
|
524
553
|
for (const [k, v] of this) {
|
|
525
554
|
const r = fn(v, k);
|
|
526
|
-
if (r !== skip)
|
|
555
|
+
if (r !== exports.skip)
|
|
527
556
|
ret.push(r);
|
|
528
557
|
}
|
|
529
558
|
return ret;
|
|
@@ -532,7 +561,7 @@ const applyClearing = (() => {
|
|
|
532
561
|
const ret = [];
|
|
533
562
|
for (const [k, v] of this) {
|
|
534
563
|
const r = fn(v, k);
|
|
535
|
-
if (r !== skip)
|
|
564
|
+
if (r !== exports.skip)
|
|
536
565
|
ret.push(r);
|
|
537
566
|
}
|
|
538
567
|
return Object.fromEntries(ret);
|
package/cmp/mjs/main.d.ts
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
1
|
import '../sideEffects.js';
|
|
2
|
+
type ClsCheck = {
|
|
3
|
+
(i: unknown, num: BooleanConstructor): i is boolean;
|
|
4
|
+
(i: unknown, num: NumberConstructor): i is number;
|
|
5
|
+
(i: unknown, str: StringConstructor): i is string;
|
|
6
|
+
(i: unknown, buff: BufferConstructor): i is Buffer;
|
|
7
|
+
(i: unknown, arr: ArrayConstructor): i is any[];
|
|
8
|
+
(i: unknown, obj: ObjectConstructor): i is Obj<unknown>;
|
|
9
|
+
(i: unknown, fn: FunctionConstructor): i is Fn;
|
|
10
|
+
(i: unknown, fn: SymbolConstructor): i is symbol;
|
|
11
|
+
<T>(i: unknown, prm: PromiseConstructor): i is Promise<T>;
|
|
12
|
+
<C extends abstract new (...args: any) => any>(i: unknown, cls: C): i is InstanceType<C>;
|
|
13
|
+
};
|
|
14
|
+
export declare const getClsName: (i: any) => any;
|
|
15
|
+
export declare const getCls: (i: any) => any;
|
|
16
|
+
export declare const isCls: ClsCheck;
|
|
17
|
+
export declare const inCls: ClsCheck;
|
|
18
|
+
export declare const skip: undefined;
|
|
19
|
+
type Then = {
|
|
20
|
+
<V, R0 = V, R1 = never>(val: Promise<V>, rsv?: (v: V) => R0, rjc?: (e: any) => R1): Promise<R0 | R1>;
|
|
21
|
+
<V, R0 = V, R1 = never>(val: V, rsv?: (v: V) => R0, rjc?: (e: any) => R1): R0 | R1;
|
|
22
|
+
};
|
|
23
|
+
export declare const then: Then;
|
|
24
|
+
type Safe = {
|
|
25
|
+
<V, R0 = never>(fn: () => Promise<V>, rjc?: (e: any) => R0): Promise<V | R0>;
|
|
26
|
+
<V, R0 = never>(fn: () => V, rjc?: (e: any) => R0): Promise<V | R0>;
|
|
27
|
+
};
|
|
28
|
+
export declare const safe: Safe;
|
|
2
29
|
declare const applyClearing: () => void;
|
|
3
30
|
export default applyClearing;
|
package/cmp/mjs/main.js
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
+
export const getClsName = i => {
|
|
2
|
+
if (i === null)
|
|
3
|
+
return 'Null';
|
|
4
|
+
if (i === undefined)
|
|
5
|
+
return 'Undef';
|
|
6
|
+
if (i !== i)
|
|
7
|
+
return 'Nan';
|
|
8
|
+
return Object.getPrototypeOf(i)?.constructor.name ?? 'Prototypeless';
|
|
9
|
+
};
|
|
10
|
+
export const getCls = i => Object.getPrototypeOf(i)?.constructor ?? null;
|
|
11
|
+
export const isCls = (i, C) => {
|
|
12
|
+
// NaN only matches against the NaN primitive (not the Number Form)
|
|
13
|
+
if (i !== i)
|
|
14
|
+
return C !== C;
|
|
15
|
+
// `null` and `undefined` only match to themselves
|
|
16
|
+
if (i == null)
|
|
17
|
+
return i === C;
|
|
18
|
+
// Otherwise strictly check the constructor
|
|
19
|
+
return Object.getPrototypeOf(i).constructor === C;
|
|
20
|
+
};
|
|
21
|
+
export const inCls = (i, C) => i instanceof C;
|
|
22
|
+
export const skip = undefined;
|
|
23
|
+
export const then = (val, rsv = (v => v), rjc = ((e) => { throw e; })) => {
|
|
24
|
+
// Act on `val` regardless of whether it's a Promise or immediate value; return `rsv(val)`
|
|
25
|
+
// either immediately or as a Promise
|
|
26
|
+
if (inCls(val, Promise))
|
|
27
|
+
return val.then(rsv).catch(rjc);
|
|
28
|
+
try {
|
|
29
|
+
return rsv(val);
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
return rjc(err);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export const safe = (fn, rjc = e => { throw e; }) => {
|
|
36
|
+
// Execute a function which returns a value either synchronously or asynchronously; in both cases
|
|
37
|
+
// allows errors occurring from function execution to be handled
|
|
38
|
+
try {
|
|
39
|
+
return then(fn(), v => v, rjc);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
return rjc(err);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
1
45
|
const applyClearing = (() => {
|
|
2
46
|
const global = globalThis;
|
|
3
47
|
// Prevent multiple installations...
|
|
@@ -59,29 +103,7 @@ const applyClearing = (() => {
|
|
|
59
103
|
// </SYMBOLS>
|
|
60
104
|
];
|
|
61
105
|
const syms = Object.fromEntries(symNames.map(term => [term, Symbol(`${pfx}:${term}`)]));
|
|
62
|
-
|
|
63
|
-
if (i === null)
|
|
64
|
-
return 'Null';
|
|
65
|
-
if (i === undefined)
|
|
66
|
-
return 'Undef';
|
|
67
|
-
if (i !== i)
|
|
68
|
-
return 'Nan';
|
|
69
|
-
return Object.getPrototypeOf(i)?.constructor.name ?? 'Prototypeless';
|
|
70
|
-
};
|
|
71
|
-
const getCls = i => Object.getPrototypeOf(i)?.constructor ?? null;
|
|
72
|
-
const isCls = (i, C) => {
|
|
73
|
-
// NaN only matches against the NaN primitive (not the Number Form)
|
|
74
|
-
if (i !== i)
|
|
75
|
-
return C !== C;
|
|
76
|
-
// `null` and `undefined` only match to themselves
|
|
77
|
-
if (i == null)
|
|
78
|
-
return i === C;
|
|
79
|
-
// Otherwise strictly check the constructor
|
|
80
|
-
return Object.getPrototypeOf(i).constructor === C;
|
|
81
|
-
};
|
|
82
|
-
const inCls = (i, C) => i instanceof C;
|
|
83
|
-
const skip = undefined;
|
|
84
|
-
Object.assign(global, { ...syms, clearing: { getClsName, getCls, isCls, inCls } });
|
|
106
|
+
Object.assign(global, { ...syms });
|
|
85
107
|
const protoDefs = (Cls, def) => {
|
|
86
108
|
let protoVals = [];
|
|
87
109
|
let classVals = [];
|
package/cmp/sideEffects.d.ts
CHANGED
|
@@ -26,37 +26,6 @@ declare global {
|
|
|
26
26
|
charVal: (c: string) => bigint,
|
|
27
27
|
valChar: (n: bigint) => string
|
|
28
28
|
};
|
|
29
|
-
type ClsCheck = {
|
|
30
|
-
(i: unknown, num: BooleanConstructor): i is boolean,
|
|
31
|
-
(i: unknown, num: NumberConstructor): i is number,
|
|
32
|
-
(i: unknown, str: StringConstructor): i is string,
|
|
33
|
-
(i: unknown, buff: BufferConstructor): i is Buffer,
|
|
34
|
-
(i: unknown, arr: ArrayConstructor): i is any[],
|
|
35
|
-
(i: unknown, obj: ObjectConstructor): i is Obj<unknown>,
|
|
36
|
-
(i: unknown, fn: FunctionConstructor): i is Fn,
|
|
37
|
-
(i: unknown, fn: SymbolConstructor): i is symbol,
|
|
38
|
-
<T>(i: unknown, prm: PromiseConstructor): i is Promise<T>,
|
|
39
|
-
<C extends abstract new (...args: any) => any>(i: unknown, cls: C): i is InstanceType<C>
|
|
40
|
-
};
|
|
41
|
-
const clearing: {
|
|
42
|
-
|
|
43
|
-
getClsName: (i: any) => string,
|
|
44
|
-
getCls: {
|
|
45
|
-
(i: number): NumberConstructor,
|
|
46
|
-
(i: string): StringConstructor,
|
|
47
|
-
(i: Buffer): BufferConstructor,
|
|
48
|
-
(i: any[]): ArrayConstructor,
|
|
49
|
-
(i: { [K: string]: any }): ObjectConstructor,
|
|
50
|
-
(i: (...a: any[]) => any): FunctionConstructor,
|
|
51
|
-
(i: Promise<any>): PromiseConstructor,
|
|
52
|
-
<T>(i: T): { new (...args: any[]): T }
|
|
53
|
-
},
|
|
54
|
-
isCls: ClsCheck,
|
|
55
|
-
inCls: ClsCheck,
|
|
56
|
-
|
|
57
|
-
skip: Skip
|
|
58
|
-
|
|
59
|
-
};
|
|
60
29
|
|
|
61
30
|
// <SYMBOLS> :: declarations :: /const ([a-zA-Z0-9]+)[ ]*[:][ ]*unique symbol;/
|
|
62
31
|
const add: unique symbol;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gershy/clearing",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
4
4
|
"description": "Adds the features you always wish javascript had!",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"clearing",
|
|
@@ -22,22 +22,25 @@
|
|
|
22
22
|
"tsx": "^4.21.0",
|
|
23
23
|
"typescript": "^5.9.3"
|
|
24
24
|
},
|
|
25
|
+
|
|
25
26
|
"type": "module",
|
|
26
27
|
"files": [
|
|
27
28
|
"cmp"
|
|
28
29
|
],
|
|
30
|
+
"types": "./cmp/mjs/main.d.ts",
|
|
29
31
|
"exports": {
|
|
30
32
|
".": {
|
|
31
33
|
"import": "./cmp/mjs/main.js",
|
|
32
34
|
"require": "./cmp/cjs/main.js"
|
|
33
35
|
}
|
|
34
36
|
},
|
|
37
|
+
|
|
35
38
|
"scripts": {
|
|
36
39
|
"test": "npm run ts.check && npx tsx ./src/main.test.ts",
|
|
37
40
|
"ts.check": "npx tsc --noEmit",
|
|
38
|
-
"build.cjs": "tsc -p
|
|
39
|
-
"build.mjs": "tsc -p
|
|
40
|
-
"build": "node ./build.js removeCmp && npm run build.cjs && npm run build.mjs && node ./build.js finalizeExportVariants",
|
|
41
|
+
"build.cjs": "tsc -p build/tsconfig.cjs.json",
|
|
42
|
+
"build.mjs": "tsc -p build/tsconfig.mjs.json",
|
|
43
|
+
"build": "node ./build/act.js removeCmp && npm run build.cjs && npm run build.mjs && node ./build/act.js finalizeExportVariants",
|
|
41
44
|
"git.pub": "npm run test && git add --all && git commit -m \"automated\" && git push",
|
|
42
45
|
"npm.login": "npm login",
|
|
43
46
|
"npm.pub": "npm run test && npm run build && npm publish --access public"
|