@vicin/sigil 3.3.0 → 3.4.0

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/index.js CHANGED
@@ -31,16 +31,29 @@ var DEFAULT_LABEL_REGEX = RECOMMENDED_LABEL_REGEX;
31
31
  var __SIGIL__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__SIGIL__");
32
32
  var __LABEL__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__LABEL__");
33
33
  var __EFFECTIVE_LABEL__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__EFFECTIVE_LABEL__");
34
- var __LINEAGE__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__LINEAGE__");
34
+ var __DEPTH__ = /* @__PURE__ */ Symbol.for("@vicin/sigil.__DEPTH__");
35
35
 
36
36
  // src/helpers.ts
37
37
  var AUTO_LABEL_PREFEX = "@Sigil-auto";
38
38
  var handledCtors = /* @__PURE__ */ new WeakSet();
39
- function handleSigil(ctor, label, opts) {
40
- if (handledCtors.has(ctor)) return;
39
+ var handledCtorsExplicit = /* @__PURE__ */ new WeakSet();
40
+ function handleSigilExplicit(ctor, label, opts) {
41
+ if (handledCtorsExplicit.has(ctor))
42
+ throw new Error(
43
+ `[Sigil Error] Class '${ctor.name}' with label '${ctor.SigilLabel}' is already sigilified`
44
+ );
41
45
  verifyLabel(ctor, label, opts);
42
46
  handleAncestors(ctor, opts);
43
- sigilify(ctor, label != null ? label : generateRandomLabel(ctor));
47
+ sigilify(ctor, label, true);
48
+ }
49
+ function handleSigilLazy(ctor) {
50
+ if (handledCtors.has(ctor)) return;
51
+ if (!OPTIONS.autofillLabels)
52
+ throw new Error(
53
+ `[Sigil Error] Class '${ctor == null ? void 0 : ctor.name}' is not sigilified, Make sure to sigilify all Sigil classes or set 'autofillLabels' to 'true'`
54
+ );
55
+ handleAncestors(ctor);
56
+ sigilify(ctor, generateRandomLabel(ctor), false);
44
57
  }
45
58
  function handleAncestors(ctor, opts) {
46
59
  var _a;
@@ -59,54 +72,48 @@ function handleAncestors(ctor, opts) {
59
72
  throw new Error(
60
73
  `[Sigil Error] Class '${a2.name}' is not sigilified, Make sure to sigilify all Sigil classes or set 'autofillLabels' to 'true'`
61
74
  );
62
- sigilify(a2, generateRandomLabel(a2));
75
+ sigilify(a2, generateRandomLabel(a2), false);
63
76
  }
64
- labelOwner.set(labelOf(a2), a2.name);
77
+ labelOwner.set(a2.prototype[__LABEL__], a2.name);
65
78
  }
66
79
  }
67
- function sigilify(ctor, label) {
80
+ function sigilify(ctor, label, explicit) {
68
81
  var _a;
69
82
  const sym = Symbol.for(label);
70
83
  Object.defineProperty(ctor.prototype, __SIGIL__, {
71
84
  value: sym,
72
- configurable: false,
73
- enumerable: false,
74
- writable: false
75
- });
76
- Object.defineProperty(ctor.prototype, sym, {
77
- value: true,
78
- configurable: false,
85
+ configurable: !explicit,
79
86
  enumerable: false,
80
87
  writable: false
81
88
  });
82
89
  Object.defineProperty(ctor.prototype, __LABEL__, {
83
90
  value: label,
84
- configurable: false,
91
+ configurable: !explicit,
85
92
  enumerable: false,
86
93
  writable: false
87
94
  });
88
- if (!label.startsWith(AUTO_LABEL_PREFEX))
95
+ if (explicit)
89
96
  Object.defineProperty(ctor.prototype, __EFFECTIVE_LABEL__, {
90
97
  value: label,
91
98
  configurable: false,
92
99
  enumerable: false,
93
100
  writable: false
94
101
  });
95
- Object.defineProperty(ctor.prototype, __LINEAGE__, {
96
- value: /* @__PURE__ */ new Set(["Sigil", ...(_a = lineageOf(ctor)) != null ? _a : [], label]),
97
- configurable: false,
98
- enumerable: false,
99
- writable: false
100
- });
101
- const sigilSym = /* @__PURE__ */ Symbol.for("Sigil");
102
- if (ctor.prototype[sigilSym] !== true)
103
- Object.defineProperty(ctor.prototype, sigilSym, {
104
- value: true,
102
+ if (!handledCtors.has(ctor))
103
+ Object.defineProperty(ctor.prototype, __DEPTH__, {
104
+ value: ((_a = ctor.prototype[__DEPTH__]) != null ? _a : -1) + 1,
105
105
  configurable: false,
106
106
  enumerable: false,
107
107
  writable: false
108
108
  });
109
+ Object.defineProperty(ctor.prototype, sym, {
110
+ value: true,
111
+ configurable: false,
112
+ enumerable: false,
113
+ writable: false
114
+ });
109
115
  handledCtors.add(ctor);
116
+ if (explicit) handledCtorsExplicit.add(ctor);
110
117
  }
111
118
  function isSigilCtor(ctor) {
112
119
  return typeof ctor === "function" && ctor.prototype && __SIGIL__ in ctor.prototype;
@@ -114,15 +121,6 @@ function isSigilCtor(ctor) {
114
121
  function isSigilInstance(inst) {
115
122
  return !!inst && typeof inst === "object" && __SIGIL__ in inst;
116
123
  }
117
- function hasOwnSigil(ctor) {
118
- return typeof ctor === "function" && Object.hasOwn(ctor.prototype, __SIGIL__);
119
- }
120
- function labelOf(ctor) {
121
- return ctor.prototype[__LABEL__];
122
- }
123
- function lineageOf(ctor) {
124
- return ctor.prototype[__LINEAGE__];
125
- }
126
124
  function getSigilLabels() {
127
125
  return getLabelRegistry().labels();
128
126
  }
@@ -146,22 +144,15 @@ function getLabelRegistry() {
146
144
  return labelRegistry;
147
145
  }
148
146
  function verifyLabel(ctor, label, opts) {
149
- var _a, _b, _c;
147
+ var _a, _b;
150
148
  const reg = getLabelRegistry();
151
- if (!label) {
152
- if (!((_a = opts == null ? void 0 : opts.autofillLabels) != null ? _a : OPTIONS.autofillLabels))
153
- throw new Error(
154
- `[Sigil Error] Class '${ctor == null ? void 0 : ctor.name}' is not sigilified, Make sure to sigilify all Sigil classes or set 'autofillLabels' to 'true'`
155
- );
156
- return;
157
- }
158
149
  if (label.startsWith(AUTO_LABEL_PREFEX))
159
150
  throw new Error(`'${AUTO_LABEL_PREFEX}' is a prefex reserved by the library`);
160
- if (!((_b = opts == null ? void 0 : opts.skipLabelUniquenessCheck) != null ? _b : OPTIONS.skipLabelUniquenessCheck) && reg.has(label))
151
+ if (!((_a = opts == null ? void 0 : opts.skipLabelUniquenessCheck) != null ? _a : OPTIONS.skipLabelUniquenessCheck) && reg.has(label))
161
152
  throw new Error(
162
153
  `[Sigil Error] Passed label '${label}' to class '${ctor == null ? void 0 : ctor.name}' is re-used, passed labels must be unique`
163
154
  );
164
- const labelValidation = (_c = opts == null ? void 0 : opts.labelValidation) != null ? _c : OPTIONS.labelValidation;
155
+ const labelValidation = (_b = opts == null ? void 0 : opts.labelValidation) != null ? _b : OPTIONS.labelValidation;
165
156
  if (labelValidation) {
166
157
  let valid;
167
158
  if (labelValidation instanceof RegExp) valid = labelValidation.test(label);
@@ -178,24 +169,20 @@ function generateRandomLabel(ctor) {
178
169
  }
179
170
 
180
171
  // src/mixin.ts
181
- function Sigilify(Base, label, opts) {
182
- if (hasOwnSigil(Base))
183
- throw new Error(
184
- `[Sigil Error] Class '${Base.name}' with label '${Base.SigilLabel}' is already sigilified`
185
- );
186
- class Sigilified extends Base {
172
+ function BaseSigilify(Base) {
173
+ class Sigil2 extends Base {
187
174
  /**
188
175
  * Class-level identity label constant for this sigil constructor.
189
176
  */
190
177
  static get SigilLabel() {
191
- handleSigil(this);
178
+ handleSigilLazy(this);
192
179
  return this.prototype[__LABEL__];
193
180
  }
194
181
  /**
195
182
  * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
196
183
  */
197
184
  static get SigilEffectiveLabel() {
198
- handleSigil(this);
185
+ handleSigilLazy(this);
199
186
  return this.prototype[__EFFECTIVE_LABEL__];
200
187
  }
201
188
  /**
@@ -206,23 +193,29 @@ function Sigilify(Base, label, opts) {
206
193
  * @returns An array of labels representing parent → child type labels.
207
194
  */
208
195
  static get SigilLabelLineage() {
209
- handleSigil(this);
210
- return [...this.prototype[__LINEAGE__]];
196
+ handleSigilLazy(this);
197
+ const lineage = [];
198
+ let c = this;
199
+ while (c && typeof c === "function" && c.prototype[__SIGIL__]) {
200
+ lineage.unshift(c.SigilLabel);
201
+ c = Object.getPrototypeOf(c);
202
+ }
203
+ return lineage;
211
204
  }
212
205
  /**
213
206
  * Sigil type label set for the current constructor.
214
207
  * Useful for debugging.
215
208
  *
209
+ * @deprecated To minize API and bundle size, internally this method is 'new Set(this.SigilLabelLineage)' only. will be removed in v4
216
210
  * @returns A Readonly Set of labels that represent the type lineage.
217
211
  */
218
212
  static get SigilLabelSet() {
219
- handleSigil(this);
220
- return this.prototype[__LINEAGE__];
213
+ return new Set(this.SigilLabelLineage);
221
214
  }
222
215
  constructor(...args) {
223
216
  super(...args);
224
217
  const ctor = new.target;
225
- handleSigil(ctor);
218
+ handleSigilLazy(ctor);
226
219
  }
227
220
  /**
228
221
  * Check whether `other` is (or inherits from) the instance represented by the
@@ -237,7 +230,7 @@ function Sigilify(Base, label, opts) {
237
230
  * @returns A type guard asserting `other` is an instance of the constructor.
238
231
  */
239
232
  static isOfType(other) {
240
- handleSigil(this);
233
+ handleSigilLazy(this);
241
234
  if (other == null || typeof other !== "object") return false;
242
235
  return other[this.prototype[__SIGIL__]] === true;
243
236
  }
@@ -251,11 +244,9 @@ function Sigilify(Base, label, opts) {
251
244
  * @returns A type guard asserting `other` is an instance of the constructor.
252
245
  */
253
246
  static isExactType(other) {
254
- var _a, _b;
255
- handleSigil(this);
247
+ handleSigilLazy(this);
256
248
  if (other == null || typeof other !== "object") return false;
257
- if (((_a = this.prototype) == null ? void 0 : _a[__LINEAGE__].size) !== ((_b = other[__LINEAGE__]) == null ? void 0 : _b.size))
258
- return false;
249
+ if (this.prototype[__DEPTH__] !== other[__DEPTH__]) return false;
259
250
  return other[this.prototype[__SIGIL__]] === true;
260
251
  }
261
252
  /**
@@ -284,9 +275,8 @@ function Sigilify(Base, label, opts) {
284
275
  * @returns A type guard asserting `other` is an instance of the constructor.
285
276
  */
286
277
  isExactType(other) {
287
- var _a;
288
278
  if (other == null || typeof other !== "object") return false;
289
- if (this[__LINEAGE__].size !== ((_a = other[__LINEAGE__]) == null ? void 0 : _a.size)) return false;
279
+ if (this[__DEPTH__] !== other[__DEPTH__]) return false;
290
280
  return other[this[__SIGIL__]] === true;
291
281
  }
292
282
  /**
@@ -311,202 +301,77 @@ function Sigilify(Base, label, opts) {
311
301
  * @returns readonly array of labels representing the type lineage.
312
302
  */
313
303
  getSigilLabelLineage() {
314
- return [...this[__LINEAGE__]];
304
+ const lineage = [];
305
+ let proto = Object.getPrototypeOf(this);
306
+ while (proto && proto[__SIGIL__]) {
307
+ lineage.unshift(proto[__LABEL__]);
308
+ proto = Object.getPrototypeOf(proto);
309
+ }
310
+ return lineage;
315
311
  }
316
312
  /**
317
313
  * Returns a copy of the sigil type label lineage set for this instance's constructor.
318
314
  *
315
+ * @deprecated To minize API and bundle size, internally this method is 'new Set(this.SigilLabelLineage)' only. will be removed in v4
319
316
  * @returns readonly array of labels representing the type lineage.
320
317
  */
321
318
  getSigilLabelSet() {
322
- return this[__LINEAGE__];
319
+ return new Set(this.getSigilLabelLineage());
323
320
  }
324
321
  }
325
- handleSigil(Sigilified, label, opts);
322
+ handleSigilExplicit(Sigil2, "Sigil", { skipLabelUniquenessCheck: true });
323
+ return Sigil2;
324
+ }
325
+ function Sigilify(Base, label, opts) {
326
+ if (isSigilCtor(Base))
327
+ throw new Error(
328
+ `[Sigil Error] Class '${Base.name}' with label '${Base.SigilLabel}' is already sigilified`
329
+ );
330
+ const BaseSigil = BaseSigilify(Base);
331
+ class Sigilified extends BaseSigil {
332
+ }
333
+ handleSigilExplicit(Sigilified, label, opts);
326
334
  return Sigilified;
327
335
  }
328
336
  function SigilifyAbstract(Base, label, opts) {
329
- if (hasOwnSigil(Base))
337
+ if (isSigilCtor(Base))
330
338
  throw new Error(
331
339
  `[Sigil Error] Class '${Base.name}' with label '${Base.SigilLabel}' is already sigilified`
332
340
  );
333
- class Sigilified extends Base {
334
- /**
335
- * Class-level identity label constant for this sigil constructor.
336
- */
337
- static get SigilLabel() {
338
- handleSigil(this);
339
- return this.prototype[__LABEL__];
340
- }
341
- /**
342
- * Class-level human-readable label constant for this sigil constructor, last passed label in 'Sigil' chain by developer.
343
- */
344
- static get SigilEffectiveLabel() {
345
- handleSigil(this);
346
- return this.prototype[__EFFECTIVE_LABEL__];
347
- }
348
- /**
349
- * Linearized sigil type label chain for the current constructor.
350
- *
351
- * Useful for debugging and performing strict lineage comparisons.
352
- *
353
- * @returns An array of labels representing parent → child type labels.
354
- */
355
- static get SigilLabelLineage() {
356
- handleSigil(this);
357
- return [...this.prototype[__LINEAGE__]];
358
- }
359
- /**
360
- * Sigil type label set for the current constructor.
361
- * Useful for debugging.
362
- *
363
- * @returns A Readonly Set of labels that represent the type lineage.
364
- */
365
- static get SigilLabelSet() {
366
- handleSigil(this);
367
- return this.prototype[__LINEAGE__];
368
- }
369
- constructor(...args) {
370
- super(...args);
371
- const ctor = new.target;
372
- handleSigil(ctor);
373
- }
374
- /**
375
- * Check whether `other` is (or inherits from) the instance represented by the
376
- * calling constructor.
377
- *
378
- * This replaces `instanceof` so that checks remain valid across bundles/realms
379
- * and when subclassing.
380
- *
381
- * @typeParam T - The specific sigil constructor (`this`).
382
- * @param this - The constructor performing the type check.
383
- * @param other - The object to test.
384
- * @returns A type guard asserting `other` is an instance of the constructor.
385
- */
386
- static isOfType(other) {
387
- handleSigil(this);
388
- if (other == null || typeof other !== "object") return false;
389
- return other[this.prototype[__SIGIL__]] === true;
390
- }
391
- /**
392
- * Check whether `other` is exactly the same instance represented by the
393
- * calling constructor.
394
- *
395
- * @typeParam T - The specific sigil constructor (`this`).
396
- * @param this - The constructor performing the type check.
397
- * @param other - The object to test.
398
- * @returns A type guard asserting `other` is an instance of the constructor.
399
- */
400
- static isExactType(other) {
401
- var _a, _b;
402
- handleSigil(this);
403
- if (other == null || typeof other !== "object") return false;
404
- if (((_a = this.prototype) == null ? void 0 : _a[__LINEAGE__].size) !== ((_b = other[__LINEAGE__]) == null ? void 0 : _b.size))
405
- return false;
406
- return other[this.prototype[__SIGIL__]] === true;
407
- }
408
- /**
409
- * Check whether `other` is (or inherits from) the instance represented by the
410
- * calling constructor.
411
- *
412
- * This replaces `instanceof` so that checks remain valid across bundles/realms
413
- * and when subclassing.
414
- *
415
- * @typeParam T - The specific sigil constructor (`this`).
416
- * @param this - The constructor performing the type check.
417
- * @param other - The object to test.
418
- * @returns A type guard asserting `other` is an instance of the constructor.
419
- */
420
- isOfType(other) {
421
- if (other == null || typeof other !== "object") return false;
422
- return other[this[__SIGIL__]] === true;
423
- }
424
- /**
425
- * Check whether `other` is exactly the same instance represented by the
426
- * calling constructor.
427
- *
428
- * @typeParam T - The specific sigil constructor (`this`).
429
- * @param this - The constructor performing the type check.
430
- * @param other - The object to test.
431
- * @returns A type guard asserting `other` is an instance of the constructor.
432
- */
433
- isExactType(other) {
434
- var _a;
435
- if (other == null || typeof other !== "object") return false;
436
- if (this[__LINEAGE__].size !== ((_a = other[__LINEAGE__]) == null ? void 0 : _a.size)) return false;
437
- return other[this[__SIGIL__]] === true;
438
- }
439
- /**
440
- * Returns the identity sigil label of this instance's constructor.
441
- *
442
- * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil-auto:ClassName:1:pnf11bgl').
443
- */
444
- getSigilLabel() {
445
- return this[__LABEL__];
446
- }
447
- /**
448
- * Returns the human-readable sigil label of this instance's constructor.
449
- *
450
- * @returns The last passed label string (e.g. '@scope/pkg.ClassName').
451
- */
452
- getSigilEffectiveLabel() {
453
- return this[__EFFECTIVE_LABEL__];
454
- }
455
- /**
456
- * Returns a copy of the sigil type label lineage for this instance's constructor.
457
- *
458
- * @returns readonly array of labels representing the type lineage.
459
- */
460
- getSigilLabelLineage() {
461
- return [...this[__LINEAGE__]];
462
- }
463
- /**
464
- * Returns a copy of the sigil type label lineage set for this instance's constructor.
465
- *
466
- * @returns readonly array of labels representing the type lineage.
467
- */
468
- getSigilLabelSet() {
469
- return this[__LINEAGE__];
470
- }
341
+ const BaseSigil = BaseSigilify(Base);
342
+ class Sigilified extends BaseSigil {
471
343
  }
472
- handleSigil(Sigilified, label, opts);
344
+ handleSigilExplicit(Sigilified, label, opts);
473
345
  return Sigilified;
474
346
  }
475
347
 
476
348
  // src/classes.ts
477
- var Sigil = Sigilify(class {
478
- }, "Sigil");
349
+ var Sigil = BaseSigilify(class {
350
+ });
479
351
  var SigilError = Sigilify(Error, "SigilError");
480
352
 
481
- // src/decorator.ts
482
- function WithSigil(label, opts) {
353
+ // src/attach.ts
354
+ function AttachSigil(label, opts) {
483
355
  return function(value, context) {
484
356
  if (!isSigilCtor(value))
485
357
  throw new Error(
486
- `[Sigil Error] 'WithSigil' decorator accept only Sigil classes but used on class '${value.name}'`
358
+ `[Sigil Error] 'AttachSigil' decorator accept only Sigil classes but used on class '${value.name}'`
487
359
  );
488
- if (hasOwnSigil(value))
489
- throw new Error(
490
- `[Sigil Error] Class '${value.name}' with label '${value.SigilLabel}' is already sigilified`
491
- );
492
- handleSigil(value, label, opts);
360
+ handleSigilExplicit(value, label, opts);
493
361
  };
494
362
  }
495
-
496
- // src/hof.ts
497
- function withSigil(Class, label, opts) {
363
+ var WithSigil = AttachSigil;
364
+ function attachSigil(Class, label, opts) {
498
365
  if (!isSigilCtor(Class))
499
366
  throw new Error(
500
- `[Sigil Error] 'withSigil' HOF accept only Sigil classes but used on class '${Class.name}'`
501
- );
502
- if (hasOwnSigil(Class))
503
- throw new Error(
504
- `[Sigil Error] Class '${Class.name}' with label '${Class.SigilLabel}' is already sigilified`
367
+ `[Sigil Error] 'attachSigil' function accept only Sigil classes but used on class '${Class.name}'`
505
368
  );
506
- handleSigil(Class, label, opts);
369
+ handleSigilExplicit(Class, label, opts);
507
370
  return Class;
508
371
  }
372
+ var withSigil = attachSigil;
509
373
 
374
+ exports.AttachSigil = AttachSigil;
510
375
  exports.DEFAULT_LABEL_REGEX = DEFAULT_LABEL_REGEX;
511
376
  exports.RECOMMENDED_LABEL_REGEX = RECOMMENDED_LABEL_REGEX;
512
377
  exports.Sigil = Sigil;
@@ -514,6 +379,7 @@ exports.SigilError = SigilError;
514
379
  exports.Sigilify = Sigilify;
515
380
  exports.SigilifyAbstract = SigilifyAbstract;
516
381
  exports.WithSigil = WithSigil;
382
+ exports.attachSigil = attachSigil;
517
383
  exports.getSigilLabels = getSigilLabels;
518
384
  exports.isSigilCtor = isSigilCtor;
519
385
  exports.isSigilInstance = isSigilInstance;