@vicin/sigil 3.3.0 → 4.0.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.
@@ -0,0 +1,374 @@
1
+ /** -----------------------------------------
2
+ * Public
3
+ * ----------------------------------------- */
4
+ /**
5
+ * Symbol used for nominal typing
6
+ */
7
+ declare const sigil: unique symbol;
8
+ /** Update '[sigil]' field for nominal typing */
9
+ type ExtendSigil<L extends string, P extends Sigil> = Prettify<IfNever<SigilOf<P>, {}> & {
10
+ [K in L]: true;
11
+ }>;
12
+ /**
13
+ * Extract the compile-time label map from a sigil instance `S`.
14
+ *
15
+ * @typeParam S - A sigil instance type.
16
+ * @returns The sigil label record (e.g. `{ User: true, Admin: true }`) or never if not Sigil class instance.
17
+ */
18
+ type SigilOf<S> = S extends {
19
+ readonly [sigil]: infer Sigil;
20
+ } ? Sigil : never;
21
+ /**
22
+ * Helper type to get prototype of class
23
+ *
24
+ * @template T - Class constructor.
25
+ */
26
+ type GetPrototype<T> = T extends {
27
+ prototype: infer P;
28
+ } ? P : never;
29
+ /** -----------------------------------------
30
+ * Internal
31
+ * ----------------------------------------- */
32
+ /**
33
+ * Generic type for class constructors used by the Sigil utilities.
34
+ *
35
+ * - `T` is the instance type produced by the constructor.
36
+ * - `P` is the tuple of parameter types accepted by the constructor.
37
+ *
38
+ * @template T - Instance type produced by the constructor (defaults to `object`).
39
+ * @template P - Parameter tuple type for the constructor.
40
+ */
41
+ type Constructor<T = object, P extends any[] = any[]> = new (...args: P) => T;
42
+ /**
43
+ * Generic type for class constructors used by the Sigil utilities. for 'abstract classes'.
44
+ *
45
+ * - `T` is the instance type produced by the constructor.
46
+ * - `P` is the tuple of parameter types accepted by the constructor.
47
+ *
48
+ * @template T - Instance type produced by the constructor (defaults to `object`).
49
+ * @template P - Parameter tuple type for the constructor.
50
+ */
51
+ type ConstructorAbstract<T = object, P extends any[] = any[]> = abstract new (...args: P) => T;
52
+ /** Helper type to prettify value */
53
+ type Prettify<T> = {
54
+ [K in keyof T]: T[K];
55
+ } & {};
56
+ /** Helper type to replace 'never' with another type */
57
+ type IfNever<T, R = {}> = [T] extends [never] ? R : T;
58
+
59
+ /**
60
+ * A minimal root Sigil class used by the library as a base identity.
61
+ *
62
+ * This is produced by `Sigilify` and can serve as a basic sentinel/base
63
+ * class for other sigil classes or for debugging/inspection.
64
+ */
65
+ declare const Sigil: {
66
+ new (...args: any[]): {
67
+ isInstance<T>(this: T, other: any): other is T;
68
+ isExactInstance<T>(this: T, other: any): other is T;
69
+ get SigilLabel(): string;
70
+ get SigilLabelLineage(): readonly string[];
71
+ get hasOwnSigil(): boolean;
72
+ readonly [sigil]: {
73
+ Sigil: true;
74
+ };
75
+ };
76
+ get SigilLabel(): string;
77
+ get SigilLabelLineage(): readonly string[];
78
+ get hasOwnSigil(): boolean;
79
+ isInstance<T>(this: T, other: any): other is GetPrototype<T>;
80
+ isExactInstance<T>(this: T, other: any): other is GetPrototype<T>;
81
+ };
82
+ type Sigil = InstanceType<typeof Sigil>;
83
+ /**
84
+ * A sigil variant of the built-in `Error` constructor used by the library
85
+ * to represent Sigil-specific errors.
86
+ *
87
+ * Use `SigilError` when you want an Error type that is identifiable via sigil
88
+ * runtime checks (e.g. `SigilError.isInstance(someError)`).
89
+ */
90
+ declare const SigilError: {
91
+ new (...args: any[]): {
92
+ [sigil]: {
93
+ Sigil: true;
94
+ SigilError: true;
95
+ };
96
+ isInstance<T>(this: T, other: any): other is T;
97
+ isExactInstance<T>(this: T, other: any): other is T;
98
+ get SigilLabel(): string;
99
+ get SigilLabelLineage(): readonly string[];
100
+ get hasOwnSigil(): boolean;
101
+ };
102
+ get SigilLabel(): string;
103
+ get SigilLabelLineage(): readonly string[];
104
+ get hasOwnSigil(): boolean;
105
+ isInstance<T>(this: T, other: any): other is GetPrototype<T>;
106
+ isExactInstance<T>(this: T, other: any): other is GetPrototype<T>;
107
+ };
108
+ type SigilError = InstanceType<typeof SigilError>;
109
+
110
+ /** -----------------------------------------
111
+ * Types
112
+ * ----------------------------------------- */
113
+ /**
114
+ * Configuration options for the Sigil library.
115
+ *
116
+ * These options control runtime validation, inheritance checks, label autofill behavior.
117
+ */
118
+ interface SigilOptions {
119
+ /**
120
+ * Validation rule applied to sigil labels before registration.
121
+ *
122
+ * - A function receives the label and must return `true` if valid.
123
+ * - A `RegExp` must match the label.
124
+ * - `null` disables validation entirely.
125
+ *
126
+ * Defaults to `null`.
127
+ */
128
+ labelValidation?: ((label: string) => boolean) | RegExp | null;
129
+ }
130
+ /** -----------------------------------------
131
+ * Update options
132
+ * ----------------------------------------- */
133
+ /**
134
+ * Update runtime options for the Sigil library.
135
+ * Call this early during application startup if you want non-default behavior.
136
+ *
137
+ * @param opts - Partial options to merge into the global `OPTIONS` object.
138
+ */
139
+ declare const updateSigilOptions: ({ labelValidation }?: SigilOptions) => void;
140
+ /** -----------------------------------------
141
+ * Label validation
142
+ * ----------------------------------------- */
143
+ /**
144
+ * Label validation regex. Labels must follow the pattern
145
+ * `@scope/package.ClassName` where `ClassName` begins with an uppercase
146
+ * letter. This avoids collisions across packages and helps debugging.
147
+ *
148
+ * It's advised to use this regex in 'SigilOptions.labelValidation'.
149
+ */
150
+ declare const RECOMMENDED_LABEL_REGEX: RegExp;
151
+
152
+ /**
153
+ * Class decorator factory that attaches sigil statics to a class constructor.
154
+ *
155
+ * @param label - Sigil label string to assign to the decorated class (e.g. `@scope/pkg.ClassName`).
156
+ * @param opts - Options object to override any global options if needed.
157
+ * @returns A class decorator compatible with the ECMAScript decorator context.
158
+ */
159
+ declare function AttachSigil(label: string, opts?: SigilOptions): (target: Function, ctx: any) => void;
160
+ /**
161
+ * Function that attaches runtime sigil metadata to Sigil class.
162
+ * Alternative to '@AttachSigil' if you prefer normal functions.
163
+ *
164
+ * @typeParam S - Constructor type (should be an instance of sigil class).
165
+ * @param Class - The constructor (class) to enhance.
166
+ * @param label - Sigil label string to assign to the decorated class (e.g. `@scope/pkg.ClassName`).
167
+ * @param opts - Options object to override any global options if needed.
168
+ * @returns The same constructor value, with runtime metadata ensured.
169
+ */
170
+ declare function attachSigil<S extends Function>(Class: S, label: string, opts?: SigilOptions): S;
171
+
172
+ /**
173
+ * Runtime predicate that checks whether the provided value is a sigil constructor.
174
+ *
175
+ * @param ctor - Constructor to test.
176
+ * @returns `true` if `value` is a sigil constructor, otherwise `false`.
177
+ */
178
+ declare function isSigilCtor(ctor: unknown): ctor is typeof Sigil;
179
+ /**
180
+ * Runtime predicate that checks whether the provided object is an instance
181
+ * of a sigil class.
182
+ *
183
+ * @param inst - The instanca to test.
184
+ * @returns `true` if `obj` is an instance produced by a sigil constructor.
185
+ */
186
+ declare function isSigilInstance(inst: unknown): inst is Sigil;
187
+
188
+ /**
189
+ * Mixin factory that augments an existing class with Sigil runtime metadata and helpers.
190
+ *
191
+ * @param Base - The base constructor to extend.
192
+ * @param label - Identity label to attach to the resulting class (e.g. '@scope/pkg.ClassName').
193
+ * @param opts - Options object to override any global options if needed.
194
+ * @returns A new constructor that extends `Base` and includes Sigil statics/instance methods.
195
+ * @throws Error if `Base` is already sigilified.
196
+ */
197
+ declare function Sigilify<L extends string>(Base: Constructor, label: L, opts?: SigilOptions): {
198
+ new (...args: any[]): {
199
+ [sigil]: {
200
+ Sigil: true;
201
+ } & { [K_1 in L]: true; } extends infer T ? { [K in keyof T]: T[K]; } : never;
202
+ /**
203
+ * Check whether `other` is (or inherits from) the instance represented by the
204
+ * calling constructor.
205
+ *
206
+ * This replaces `instanceof` so that checks remain valid across bundles/realms
207
+ * and when subclassing.
208
+ *
209
+ * @typeParam T - The specific sigil constructor (`this`).
210
+ * @param this - The constructor performing the type check.
211
+ * @param other - The object to test.
212
+ * @returns A type guard asserting `other` is an instance of the constructor.
213
+ */
214
+ isInstance<T_1>(this: T_1, other: any): other is T_1;
215
+ /**
216
+ * Check whether `other` is exactly the same instance represented by the
217
+ * calling constructor.
218
+ *
219
+ * @typeParam T - The specific sigil constructor (`this`).
220
+ * @param this - The constructor performing the type check.
221
+ * @param other - The object to test.
222
+ * @returns A type guard asserting `other` is an instance of the constructor.
223
+ */
224
+ isExactInstance<T_1>(this: T_1, other: any): other is T_1;
225
+ /**
226
+ * Returns the identity sigil label of this instance's constructor.
227
+ *
228
+ * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil-auto:ClassName:1:pnf11bgl').
229
+ */
230
+ get SigilLabel(): string;
231
+ /**
232
+ * Copy of the sigil label lineage for this instance's constructor.
233
+ *
234
+ * Useful for debugging and logging.
235
+ *
236
+ * @returns An array of sigil labels representing parent → child labels.
237
+ */
238
+ get SigilLabelLineage(): readonly string[];
239
+ /** Check if sigil label has been attached to this class */
240
+ get hasOwnSigil(): boolean;
241
+ };
242
+ /**
243
+ * Class-level identity label constant for this sigil constructor.
244
+ */
245
+ get SigilLabel(): string;
246
+ /**
247
+ * Copy of the sigil label lineage for this instance's constructor.
248
+ *
249
+ * Useful for debugging and logging.
250
+ *
251
+ * @returns An array of sigil labels representing parent → child labels.
252
+ */
253
+ get SigilLabelLineage(): readonly string[];
254
+ /** Check if sigil label has been attached to this class */
255
+ get hasOwnSigil(): boolean;
256
+ /**
257
+ * Check whether `other` is (or inherits from) the instance represented by the
258
+ * calling constructor.
259
+ *
260
+ * This replaces `instanceof` so that checks remain valid across bundles/realms
261
+ * and when subclassing.
262
+ *
263
+ * @typeParam T - The specific sigil constructor (`this`).
264
+ * @param this - The constructor performing the type check.
265
+ * @param other - The object to test.
266
+ * @returns A type guard asserting `other` is an instance of the constructor.
267
+ */
268
+ isInstance<T>(this: T, other: any): other is GetPrototype<T>;
269
+ /**
270
+ * Check whether `other` is exactly the same instance represented by the
271
+ * calling constructor.
272
+ *
273
+ * @typeParam T - The specific sigil constructor (`this`).
274
+ * @param this - The constructor performing the type check.
275
+ * @param other - The object to test.
276
+ * @returns A type guard asserting `other` is an instance of the constructor.
277
+ */
278
+ isExactInstance<T>(this: T, other: any): other is GetPrototype<T>;
279
+ };
280
+ /**
281
+ * Mixin factory that augments an existing class with Sigil runtime metadata and helpers. Accept and return 'abstract' class.
282
+ *
283
+ * @param Base - The base constructor to extend.
284
+ * @param label - Identity label to attach to the resulting class (e.g. '@scope/pkg.ClassName').
285
+ * @param opts - Options object to override any global options if needed.
286
+ * @returns A new abstract constructor that extends `Base` and includes Sigil statics/instance methods.
287
+ * @throws Error if `Base` is already sigilified.
288
+ */
289
+ declare function SigilifyAbstract<L extends string>(Base: ConstructorAbstract, label: L, opts?: SigilOptions): (abstract new (...args: any[]) => {
290
+ [sigil]: {
291
+ Sigil: true;
292
+ } & { [K_1 in L]: true; } extends infer T ? { [K in keyof T]: T[K]; } : never;
293
+ /**
294
+ * Check whether `other` is (or inherits from) the instance represented by the
295
+ * calling constructor.
296
+ *
297
+ * This replaces `instanceof` so that checks remain valid across bundles/realms
298
+ * and when subclassing.
299
+ *
300
+ * @typeParam T - The specific sigil constructor (`this`).
301
+ * @param this - The constructor performing the type check.
302
+ * @param other - The object to test.
303
+ * @returns A type guard asserting `other` is an instance of the constructor.
304
+ */
305
+ isInstance<T_1>(this: T_1, other: any): other is T_1;
306
+ /**
307
+ * Check whether `other` is exactly the same instance represented by the
308
+ * calling constructor.
309
+ *
310
+ * @typeParam T - The specific sigil constructor (`this`).
311
+ * @param this - The constructor performing the type check.
312
+ * @param other - The object to test.
313
+ * @returns A type guard asserting `other` is an instance of the constructor.
314
+ */
315
+ isExactInstance<T_1>(this: T_1, other: any): other is T_1;
316
+ /**
317
+ * Returns the identity sigil label of this instance's constructor.
318
+ *
319
+ * @returns The label string if passed (e.g. '@scope/pkg.ClassName'), random label if not passed (e.g. '@Sigil-auto:ClassName:1:pnf11bgl').
320
+ */
321
+ get SigilLabel(): string;
322
+ /**
323
+ * Copy of the sigil label lineage for this instance's constructor.
324
+ *
325
+ * Useful for debugging and logging.
326
+ *
327
+ * @returns An array of sigil labels representing parent → child labels.
328
+ */
329
+ get SigilLabelLineage(): readonly string[];
330
+ /** Check if sigil label has been attached to this class */
331
+ get hasOwnSigil(): boolean;
332
+ }) & {
333
+ /**
334
+ * Class-level identity label constant for this sigil constructor.
335
+ */
336
+ get SigilLabel(): string;
337
+ /**
338
+ * Copy of the sigil label lineage for this instance's constructor.
339
+ *
340
+ * Useful for debugging and logging.
341
+ *
342
+ * @returns An array of sigil labels representing parent → child labels.
343
+ */
344
+ get SigilLabelLineage(): readonly string[];
345
+ /** Check if sigil label has been attached to this class */
346
+ get hasOwnSigil(): boolean;
347
+ /**
348
+ * Check whether `other` is (or inherits from) the instance represented by the
349
+ * calling constructor.
350
+ *
351
+ * This replaces `instanceof` so that checks remain valid across bundles/realms
352
+ * and when subclassing.
353
+ *
354
+ * @typeParam T - The specific sigil constructor (`this`).
355
+ * @param this - The constructor performing the type check.
356
+ * @param other - The object to test.
357
+ * @returns A type guard asserting `other` is an instance of the constructor.
358
+ */
359
+ isInstance<T>(this: T, other: any): other is GetPrototype<T>;
360
+ /**
361
+ * Check whether `other` is exactly the same instance represented by the
362
+ * calling constructor.
363
+ *
364
+ * @typeParam T - The specific sigil constructor (`this`).
365
+ * @param this - The constructor performing the type check.
366
+ * @param other - The object to test.
367
+ * @returns A type guard asserting `other` is an instance of the constructor.
368
+ */
369
+ isExactInstance<T>(this: T, other: any): other is GetPrototype<T>;
370
+ };
371
+
372
+ declare function hasOwnSigil(ctor: Function): boolean;
373
+
374
+ export { AttachSigil, type ExtendSigil, type GetPrototype, RECOMMENDED_LABEL_REGEX, Sigil, SigilError, type SigilOf, type SigilOptions, Sigilify, SigilifyAbstract, attachSigil, hasOwnSigil, isSigilCtor, isSigilInstance, sigil, updateSigilOptions };