@vicin/sigil 1.2.6 → 1.3.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.mjs CHANGED
@@ -130,8 +130,7 @@ var SigilRegistry = class _SigilRegistry {
130
130
  * @param newRegistry - New Set<string> instance to use as the active registry, or `null` to disable checks.
131
131
  */
132
132
  replaceRegistry(newRegistry) {
133
- if (newRegistry)
134
- updateOptions({ registry: new _SigilRegistry(newRegistry) });
133
+ if (newRegistry) updateOptions({ registry: new _SigilRegistry(newRegistry) });
135
134
  else updateOptions({ registry: newRegistry });
136
135
  }
137
136
  /**
@@ -188,9 +187,7 @@ var SigilRegistry = class _SigilRegistry {
188
187
  * @param thisArg - Optional `this` context for the callback.
189
188
  */
190
189
  forEach(callback, thisArg) {
191
- this._registry.forEach(
192
- (ctor, label) => callback.call(thisArg, ctor, label)
193
- );
190
+ this._registry.forEach((ctor, label) => callback.call(thisArg, ctor, label));
194
191
  }
195
192
  /**
196
193
  * Get the size (number of entries) of the active registry.
@@ -251,9 +248,7 @@ updateOptions(DEFAULT_OPTIONS);
251
248
  var __SIGIL__ = /* @__PURE__ */ Symbol.for("@Sigil.__SIGIL__");
252
249
  var __SIGIL_BASE__ = /* @__PURE__ */ Symbol.for("@Sigil.__SIGIL_BASE__");
253
250
  var __DECORATED__ = /* @__PURE__ */ Symbol.for("@Sigil.__DECORATED__");
254
- var __INHERITANCE_CHECKED__ = /* @__PURE__ */ Symbol.for(
255
- "@Sigil.__INHERITANCE_CHECKED__"
256
- );
251
+ var __INHERITANCE_CHECKED__ = /* @__PURE__ */ Symbol.for("@Sigil.__INHERITANCE_CHECKED__");
257
252
  var __LABEL__ = /* @__PURE__ */ Symbol.for("@Sigil.__LABEL__");
258
253
  var __TYPE__ = /* @__PURE__ */ Symbol.for("@Sigil.__TYPE__");
259
254
  var __TYPE_LINEAGE__ = /* @__PURE__ */ Symbol.for("@Sigil.__TYPE_LINEAGE__");
@@ -419,8 +414,7 @@ function generateRandomString(length = 16) {
419
414
 
420
415
  // src/core/mixin.ts
421
416
  function Sigilify(Base, label, opts) {
422
- if (isSigilCtor(Base))
423
- throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
417
+ if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
424
418
  let l;
425
419
  if (label) {
426
420
  verifyLabel(label, opts);
@@ -473,9 +467,208 @@ function Sigilify(Base, label, opts) {
473
467
  const ctor = getConstructor(this);
474
468
  if (!ctor) {
475
469
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
476
- throw new Error(
477
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
478
- );
470
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
471
+ return;
472
+ }
473
+ checkInheritance(ctor);
474
+ }
475
+ /**
476
+ * Runtime predicate indicating whether `obj` is an instance produced by a sigil class.
477
+ *
478
+ * @param obj - The value to test.
479
+ * @returns `true` if `obj` is a sigil instance.
480
+ */
481
+ static isSigilified(obj) {
482
+ return isSigilInstance(obj);
483
+ }
484
+ /**
485
+ * Check whether `other` is (or inherits from) the type represented by the calling constructor.
486
+ *
487
+ * This replaces `instanceof` so that checks remain valid across bundles/realms
488
+ * and when subclassing.
489
+ *
490
+ * @typeParam T - The calling constructor type (narrowing the returned instance type).
491
+ * @param this - The constructor performing the check.
492
+ * @param other - The object to test.
493
+ * @returns `true` if `other` is an instance of this type or a subtype.
494
+ */
495
+ static isOfType(other) {
496
+ var _a;
497
+ if (!isSigilInstance(other)) return false;
498
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
499
+ const thisType = this[__TYPE__];
500
+ return !!otherSet && otherSet.has(thisType);
501
+ }
502
+ /**
503
+ * Strict lineage check: compares the type symbol lineage arrays element-by-element.
504
+ *
505
+ * @typeParam T - The calling constructor type.
506
+ * @param this - The constructor performing the check.
507
+ * @param other - The object to test.
508
+ * @returns `true` if `other` has an identical lineage up to the length of this constructor's lineage.
509
+ */
510
+ static isOfTypeStrict(other) {
511
+ var _a;
512
+ if (!isSigilInstance(other)) return false;
513
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
514
+ const thisLineage = this[__TYPE_LINEAGE__];
515
+ return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
516
+ }
517
+ /**
518
+ * Check whether `other` is (or inherits from) the type instance.
519
+ *
520
+ * Allows 'instanceof' like checks but in instances.
521
+ *
522
+ * @typeParam T - The instance type.
523
+ * @param this - The instance performing the check.
524
+ * @param other - The object to test.
525
+ * @returns `true` if `other` is the same instance of this type or a subtype.
526
+ */
527
+ isOfType(other) {
528
+ var _a;
529
+ if (!isSigilInstance(other)) return false;
530
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
531
+ const thisType = getConstructor(this)[__TYPE__];
532
+ return !!otherSet && otherSet.has(thisType);
533
+ }
534
+ /**
535
+ * Strict lineage check: compares the type symbol lineage arrays element-by-element.
536
+ *
537
+ * Allows 'instanceof' like checks but in instances.
538
+ *
539
+ * @typeParam T - The instance type.
540
+ * @param this - The instance performing the check.
541
+ * @param other - The object to test.
542
+ * @returns `true` if `other` has an identical lineage up to the length of this instance's lineage.
543
+ */
544
+ isOfTypeStrict(other) {
545
+ var _a, _b;
546
+ if (!isSigilInstance(other)) return false;
547
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
548
+ const thisLineage = (_b = getConstructor(this)) == null ? void 0 : _b[__TYPE_LINEAGE__];
549
+ return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
550
+ }
551
+ /**
552
+ * Returns the human-readable sigil label of this instance's constructor.
553
+ *
554
+ * @returns The label string (e.g. '@scope/pkg.ClassName') or '@Sigil.unknown' in DEV when constructor is missing.
555
+ */
556
+ getSigilLabel() {
557
+ var _a;
558
+ const ctor = getConstructor(this);
559
+ if (!ctor) {
560
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
561
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
562
+ return "@Sigil.unknown";
563
+ }
564
+ return ctor.SigilLabel;
565
+ }
566
+ /**
567
+ * Returns the runtime sigil type symbol of this instance's constructor.
568
+ *
569
+ * @returns The symbol that identifies this type at runtime.
570
+ */
571
+ getSigilType() {
572
+ var _a;
573
+ const ctor = getConstructor(this);
574
+ if (!ctor) {
575
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
576
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
577
+ return /* @__PURE__ */ Symbol.for("@Sigil.unknown");
578
+ }
579
+ return ctor.SigilType;
580
+ }
581
+ /**
582
+ * Returns a copy of the sigil type symbol lineage for this instance's constructor.
583
+ *
584
+ * @returns readonly array of symbols representing the type lineage.
585
+ */
586
+ getSigilTypeLineage() {
587
+ var _a;
588
+ const ctor = getConstructor(this);
589
+ if (!ctor) {
590
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
591
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
592
+ return [/* @__PURE__ */ Symbol.for("@Sigil.unknown")];
593
+ }
594
+ return ctor.SigilTypeLineage;
595
+ }
596
+ /**
597
+ * Returns a readonly copy of the sigil type symbol set for this instance's constructor.
598
+ *
599
+ * @returns A Readonly Set of symbols representing the type lineage for O(1) membership tests.
600
+ */
601
+ getSigilTypeSet() {
602
+ var _a;
603
+ const ctor = getConstructor(this);
604
+ if (!ctor) {
605
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
606
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
607
+ return /* @__PURE__ */ new Set([/* @__PURE__ */ Symbol.for("@Sigil.unknown")]);
608
+ }
609
+ return ctor.SigilTypeSet;
610
+ }
611
+ }
612
+ decorateCtor(Sigilified, l, opts, true);
613
+ markSigil(Sigilified);
614
+ markSigilBase(Sigilified);
615
+ return Sigilified;
616
+ }
617
+ function SigilifyAbstract(Base, label, opts) {
618
+ if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
619
+ let l;
620
+ if (label) {
621
+ verifyLabel(label, opts);
622
+ l = label;
623
+ } else l = generateRandomLabel();
624
+ class Sigilified extends Base {
625
+ /**
626
+ * Class-level human-readable label constant for this sigil constructor.
627
+ */
628
+ static get SigilLabel() {
629
+ return this[__LABEL__];
630
+ }
631
+ /**
632
+ * Class-level unique runtime symbol used as the type identifier.
633
+ *
634
+ * This symbol is created with `Symbol.for(label)` during decoration so it is
635
+ * stable across realms that share the same global symbol registry.
636
+ */
637
+ static get SigilType() {
638
+ return this[__TYPE__];
639
+ }
640
+ /**
641
+ * Copy of the linearized sigil type symbol chain for the current constructor.
642
+ *
643
+ * Useful for debugging and performing strict lineage comparisons.
644
+ *
645
+ * @returns An array of symbols representing parent → child type symbols.
646
+ */
647
+ static get SigilTypeLineage() {
648
+ var _a;
649
+ return [...(_a = this[__TYPE_LINEAGE__]) != null ? _a : []];
650
+ }
651
+ /**
652
+ * Copy of the sigil type symbol set for the current constructor.
653
+ *
654
+ * Useful for quick membership checks (O(1) lookups) and debugging.
655
+ *
656
+ * @returns A Readonly Set of symbols that represent the type lineage.
657
+ */
658
+ static get SigilTypeSet() {
659
+ const set = /* @__PURE__ */ new Set();
660
+ for (const s of this[__TYPE_SET__]) set.add(s);
661
+ return set;
662
+ }
663
+ constructor(...args) {
664
+ var _a;
665
+ super(...args);
666
+ if (Object.getPrototypeOf(this) !== new.target.prototype)
667
+ Object.setPrototypeOf(this, new.target.prototype);
668
+ const ctor = getConstructor(this);
669
+ if (!ctor) {
670
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
671
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
479
672
  return;
480
673
  }
481
674
  checkInheritance(ctor);
@@ -505,11 +698,11 @@ function Sigilify(Base, label, opts) {
505
698
  * @returns `true` if `other` is an instance of this type or a subtype.
506
699
  */
507
700
  static isOfType(other) {
508
- if (!isSigilInstance(other)) return false;
509
- const otherCtor = getConstructor(other);
510
- if (!otherCtor) return false;
511
- const otherSet = otherCtor[__TYPE_SET__];
512
- return !!otherSet && otherSet.has(this.SigilType);
701
+ var _a;
702
+ if (!isSigilInstance(other) || !isSigilCtor(this)) return false;
703
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
704
+ const thisType = this[__TYPE__];
705
+ return !!otherSet && otherSet.has(thisType);
513
706
  }
514
707
  /**
515
708
  * Strict lineage check: compares the type symbol lineage arrays element-by-element.
@@ -524,13 +717,46 @@ function Sigilify(Base, label, opts) {
524
717
  * @returns `true` if `other` has an identical lineage up to the length of this constructor's lineage.
525
718
  */
526
719
  static isOfTypeStrict(other) {
527
- if (!isSigilInstance(other)) return false;
528
- const otherCtor = getConstructor(other);
529
- if (!otherCtor) return false;
530
- const otherLineage = otherCtor[__TYPE_LINEAGE__];
720
+ var _a;
721
+ if (!isSigilInstance(other) || !isSigilCtor(this)) return false;
722
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
531
723
  const thisLineage = this[__TYPE_LINEAGE__];
532
724
  return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
533
725
  }
726
+ /**
727
+ * Check whether `other` is (or inherits from) the type instance.
728
+ *
729
+ * Allows 'instanceof' like checks but in instances.
730
+ *
731
+ * @typeParam T - The instance type.
732
+ * @param this - The instance performing the check.
733
+ * @param other - The object to test.
734
+ * @returns `true` if `other` is the same instance of this type or a subtype.
735
+ */
736
+ isOfType(other) {
737
+ var _a;
738
+ if (!isSigilInstance(other) || !isSigilInstance(this)) return false;
739
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
740
+ const thisType = getConstructor(this)[__TYPE__];
741
+ return !!otherSet && otherSet.has(thisType);
742
+ }
743
+ /**
744
+ * Strict lineage check: compares the type symbol lineage arrays element-by-element.
745
+ *
746
+ * Allows 'instanceof' like checks but in instances.
747
+ *
748
+ * @typeParam T - The instance type.
749
+ * @param this - The instance performing the check.
750
+ * @param other - The object to test.
751
+ * @returns `true` if `other` has an identical lineage up to the length of this instance's lineage.
752
+ */
753
+ isOfTypeStrict(other) {
754
+ var _a, _b;
755
+ if (!isSigilInstance(other) || !isSigilInstance(this)) return false;
756
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
757
+ const thisLineage = (_b = getConstructor(this)) == null ? void 0 : _b[__TYPE_LINEAGE__];
758
+ return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
759
+ }
534
760
  /**
535
761
  * Returns the human-readable sigil label of this instance's constructor.
536
762
  *
@@ -541,9 +767,7 @@ function Sigilify(Base, label, opts) {
541
767
  const ctor = getConstructor(this);
542
768
  if (!ctor) {
543
769
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
544
- throw new Error(
545
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
546
- );
770
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
547
771
  return "@Sigil.unknown";
548
772
  }
549
773
  return ctor.SigilLabel;
@@ -558,9 +782,7 @@ function Sigilify(Base, label, opts) {
558
782
  const ctor = getConstructor(this);
559
783
  if (!ctor) {
560
784
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
561
- throw new Error(
562
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
563
- );
785
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
564
786
  return /* @__PURE__ */ Symbol.for("@Sigil.unknown");
565
787
  }
566
788
  return ctor.SigilType;
@@ -575,9 +797,7 @@ function Sigilify(Base, label, opts) {
575
797
  const ctor = getConstructor(this);
576
798
  if (!ctor) {
577
799
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
578
- throw new Error(
579
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
580
- );
800
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
581
801
  return [/* @__PURE__ */ Symbol.for("@Sigil.unknown")];
582
802
  }
583
803
  return ctor.SigilTypeLineage;
@@ -592,9 +812,7 @@ function Sigilify(Base, label, opts) {
592
812
  const ctor = getConstructor(this);
593
813
  if (!ctor) {
594
814
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
595
- throw new Error(
596
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
597
- );
815
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
598
816
  return /* @__PURE__ */ new Set([/* @__PURE__ */ Symbol.for("@Sigil.unknown")]);
599
817
  }
600
818
  return ctor.SigilTypeSet;
@@ -679,6 +897,6 @@ function withSigilTyped(Class, label, opts) {
679
897
  return Class;
680
898
  }
681
899
 
682
- export { DEFAULT_LABEL_REGEX, REGISTRY, Sigil, SigilError, SigilRegistry, Sigilify, WithSigil, getActiveRegistry, isDecorated, isInheritanceChecked, isSigilBaseCtor, isSigilBaseInstance, isSigilCtor, isSigilInstance, typed, updateOptions, withSigil, withSigilTyped };
900
+ export { DEFAULT_LABEL_REGEX, REGISTRY, Sigil, SigilError, SigilRegistry, Sigilify, SigilifyAbstract, WithSigil, getActiveRegistry, isDecorated, isInheritanceChecked, isSigilBaseCtor, isSigilBaseInstance, isSigilCtor, isSigilInstance, typed, updateOptions, withSigil, withSigilTyped };
683
901
  //# sourceMappingURL=index.mjs.map
684
902
  //# sourceMappingURL=index.mjs.map