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