@gershy/clearing 0.0.29 → 0.0.31

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/mjs/main.js CHANGED
@@ -1,58 +1,55 @@
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
- };
45
1
  const applyClearing = (() => {
46
- const global = globalThis;
47
2
  // Prevent multiple installations...
48
- const pfx = '@gershy/clearing';
49
- const memSym = Symbol.for(`${pfx}:mem`);
50
- if (global[memSym])
3
+ const global = globalThis;
4
+ if (global[Symbol.for(`@gershy/clearing:mem`)])
51
5
  return;
52
- global[memSym] = true;
53
- const [enc, dec] = [new TextEncoder(), new TextDecoder()];
6
+ global[Symbol.for(`@gershy/clearing:mem`)] = true;
7
+ const getClsName = i => {
8
+ if (i === null)
9
+ return 'Null';
10
+ if (i === undefined)
11
+ return 'Undef';
12
+ if (i !== i)
13
+ return 'Nan';
14
+ return Object.getPrototypeOf(i)?.constructor.name ?? 'Prototypeless';
15
+ };
16
+ const getCls = i => Object.getPrototypeOf(i)?.constructor ?? null;
17
+ const isCls = (i, C) => {
18
+ // NaN only matches against the NaN primitive (not the Number Form)
19
+ if (i !== i)
20
+ return C !== C;
21
+ // `null` and `undefined` only match to themselves
22
+ if (i == null)
23
+ return i === C;
24
+ // Otherwise strictly check the constructor
25
+ return Object.getPrototypeOf(i).constructor === C;
26
+ };
27
+ const inCls = (i, C) => i instanceof C;
28
+ const skip = undefined;
29
+ const then = (val, rsv = (v => v), rjc = ((e) => { throw e; })) => {
30
+ // Act on `val` regardless of whether it's a Promise or immediate value; return `rsv(val)`
31
+ // either immediately or as a Promise
32
+ if (inCls(val, Promise))
33
+ return val.then(rsv).catch(rjc);
34
+ try {
35
+ return rsv(val);
36
+ }
37
+ catch (err) {
38
+ return rjc(err);
39
+ }
40
+ };
41
+ const safe = (fn, rjc = e => { throw e; }) => {
42
+ // Execute a function which returns a value either synchronously or asynchronously; in both cases
43
+ // allows errors occurring from function execution to be handled
44
+ try {
45
+ return then(fn(), v => v, rjc);
46
+ }
47
+ catch (err) {
48
+ return rjc(err);
49
+ }
50
+ };
54
51
  const symNames = [
55
- // <SYMBOLS> :: definitions :: /[']([a-zA-Z0-9]+)[']/
52
+ // <SYMBOLS> :: runtimeNames :: /[']([a-zA-Z0-9]+)[']/
56
53
  'add',
57
54
  'allArr',
58
55
  'allObj',
@@ -64,13 +61,11 @@ const applyClearing = (() => {
64
61
  'base64Std',
65
62
  'base64Url',
66
63
  'baseline',
67
- 'bind',
68
64
  'char',
69
65
  'charset',
70
66
  'code',
71
67
  'count',
72
68
  'cut',
73
- 'dive',
74
69
  'empty',
75
70
  'find',
76
71
  'fire',
@@ -100,18 +95,71 @@ const applyClearing = (() => {
100
95
  'toNum',
101
96
  'toObj',
102
97
  'toStr',
103
- 'upper'
98
+ 'upper',
99
+ 'walk'
104
100
  // </SYMBOLS>
105
101
  ];
106
- const syms = Object.fromEntries(symNames.map(term => [term, Symbol(`${pfx}:${term}`)]));
107
- Object.assign(global, { ...syms });
102
+ const cl = {
103
+ getClsName, getCls, isCls, inCls, then, safe, skip,
104
+ ...Object.fromEntries(symNames.map(term => [term, Symbol(`@gershy/clearing:${term}`)]))
105
+ };
106
+ Object.assign(global, { cl, clearing: cl });
107
+ // <SYMBOLS> :: runtimeRefs :: /^[ ]*const[ ]([a-zA-Z0-9]+)[:]/
108
+ const add = clearing.add;
109
+ const allArr = clearing.allArr;
110
+ const allObj = clearing.allObj;
111
+ const at = clearing.at;
112
+ const assert = clearing.assert;
113
+ const base32 = clearing.base32;
114
+ const base36 = clearing.base36;
115
+ const base62 = clearing.base62;
116
+ const base64Std = clearing.base64Std;
117
+ const base64Url = clearing.base64Url;
118
+ const baseline = clearing.baseline;
119
+ const char = clearing.char;
120
+ const charset = clearing.charset;
121
+ const code = clearing.code;
122
+ const count = clearing.count;
123
+ const cut = clearing.cut;
124
+ const empty = clearing.empty;
125
+ const find = clearing.find;
126
+ const fire = clearing.fire;
127
+ const group = clearing.group;
128
+ const has = clearing.has;
129
+ const hasHead = clearing.hasHead;
130
+ const hasTail = clearing.hasTail;
131
+ const indent = clearing.indent;
132
+ const int32 = clearing.int32;
133
+ const int64 = clearing.int64;
134
+ const isInt = clearing.isInt;
135
+ const later = clearing.later;
136
+ const limn = clearing.limn;
137
+ const lower = clearing.lower;
138
+ const map = clearing.map;
139
+ const mapk = clearing.mapk;
140
+ const merge = clearing.merge;
141
+ const mod = clearing.mod;
142
+ const padHead = clearing.padHead;
143
+ const padTail = clearing.padTail;
144
+ const rem = clearing.rem;
145
+ const slash = clearing.slash;
146
+ const slice = clearing.slice;
147
+ const suppress = clearing.suppress;
148
+ const toArr = clearing.toArr;
149
+ const toBin = clearing.toBin;
150
+ const toNum = clearing.toNum;
151
+ const toObj = clearing.toObj;
152
+ const toStr = clearing.toStr;
153
+ const upper = clearing.upper;
154
+ const walk = clearing.walk;
155
+ // </SYMBOLS>
108
156
  const assignSyms = (Cls, def) => {
109
157
  const protoVals = [];
110
- for (const key of Reflect.ownKeys(def)) {
158
+ for (const key of Reflect.ownKeys(def))
111
159
  if (!isCls(key, Symbol))
112
160
  throw Object.assign(Error('invalid proto key'), { Cls, keyClsName: getClsName(key), key });
113
- protoVals.push([key, def[key]]);
114
- }
161
+ else
162
+ protoVals.push([key, def[key]]);
115
163
  // Assign class properties
116
164
  for (const [sym, value] of protoVals)
117
165
  Object.defineProperty(Cls, sym, { enumerable: false, value });
@@ -142,7 +190,7 @@ const applyClearing = (() => {
142
190
  // });
143
191
  // >> { small: { a: 1, b: 2, c: 3 }, medium: { d: 4, e: 5, f: 6, g: 7 }, big: { h: 8, i: 9, j: 10 } }
144
192
  const ret = {};
145
- for (const [k, v] of this) {
193
+ for (const [k, v] of this[walk]()) {
146
194
  const g = fn(v, k);
147
195
  if (g === skip)
148
196
  continue;
@@ -173,8 +221,8 @@ const applyClearing = (() => {
173
221
  }
174
222
  return Object.fromEntries(arr);
175
223
  },
176
- [merge](o) {
177
- for (const [k, v] of o) {
224
+ [merge](obj) {
225
+ for (const [k, v] of obj[walk]()) {
178
226
  // `skip` can be passed to remove properties
179
227
  if (v === skip) {
180
228
  delete this[k];
@@ -213,7 +261,7 @@ const applyClearing = (() => {
213
261
  }
214
262
  return ret;
215
263
  },
216
- *[Symbol.iterator]() { for (const k in this)
264
+ *[walk]() { for (const k in this)
217
265
  yield [k, this[k]]; }
218
266
  });
219
267
  assignSyms(Array, {});
@@ -270,6 +318,7 @@ const applyClearing = (() => {
270
318
  return Object.fromEntries(ret);
271
319
  }
272
320
  });
321
+ const [enc, dec] = [new TextEncoder(), new TextDecoder()];
273
322
  assignSyms(String, {
274
323
  [baseline]: (str, seq = '| ') => {
275
324
  return str.split('\n')[map](ln => {
@@ -414,6 +463,11 @@ const applyClearing = (() => {
414
463
  [toStr]() { return dec.decode(this); },
415
464
  [toNum]() { return (new Uint8Array(this))[toNum](); }
416
465
  });
466
+ assignSyms(SharedArrayBuffer, {});
467
+ assignSyms(SharedArrayBuffer.prototype, {
468
+ [toStr]() { return dec.decode(new Uint8Array(this)); },
469
+ [toNum]() { return (new Uint8Array(this))[toNum](); }
470
+ });
417
471
  assignSyms(Uint8Array, {});
418
472
  assignSyms(Uint8Array.prototype, {
419
473
  [toStr]() { return dec.decode(this); },
@@ -448,7 +502,7 @@ const applyClearing = (() => {
448
502
  return {
449
503
  form: getClsName(this),
450
504
  msg: message,
451
- trace: stack?.split('\n').slice(1)[map](v => v.trim() ?? skip) ?? [],
505
+ trace: (stack ?? '<no stack>').split('\n')[map](v => v.trim() ?? skip),
452
506
  ...props,
453
507
  cause: !cause ? null : cause[limn](seen)
454
508
  };