@develia/commons 0.4.2 → 0.4.4
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/cjs/browser/index.js +129 -0
- package/dist/cjs/browser/index.js.map +1 -0
- package/dist/cjs/index.js +4 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/type-code.js +18 -0
- package/dist/cjs/type-code.js.map +1 -0
- package/dist/cjs/type.js +212 -12
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/utilities.js +34 -127
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/browser/index.js +122 -0
- package/dist/esm/browser/index.js.map +1 -0
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/type-code.js +16 -0
- package/dist/esm/type-code.js.map +1 -0
- package/dist/esm/type.js +209 -12
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/utilities.js +33 -122
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/browser/index.d.ts +7 -0
- package/dist/types/browser/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/patch.d.ts +3 -3
- package/dist/types/patch.d.ts.map +1 -1
- package/dist/types/type-code.d.ts +15 -0
- package/dist/types/type-code.d.ts.map +1 -0
- package/dist/types/type.d.ts +45 -11
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/types.d.ts +1 -2
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utilities.d.ts +2 -6
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/browser/index.js +123 -0
- package/dist/web/browser/index.js.map +1 -0
- package/dist/web/index.js +2 -1
- package/dist/web/index.js.map +1 -1
- package/dist/web/type-code.js +16 -0
- package/dist/web/type-code.js.map +1 -0
- package/dist/web/type.js +210 -12
- package/dist/web/type.js.map +1 -1
- package/dist/web/utilities.js +33 -122
- package/dist/web/utilities.js.map +1 -1
- package/package.json +1 -1
- package/src/browser/index.ts +172 -0
- package/src/index.ts +15 -13
- package/src/patch.ts +3 -3
- package/src/type-code.ts +16 -0
- package/src/type.ts +370 -17
- package/src/types.ts +1 -5
- package/src/utilities.ts +38 -169
- package/dist/cjs/remote-data.js +0 -115
- package/dist/cjs/remote-data.js.map +0 -1
- package/dist/esm/remote-data.js +0 -112
- package/dist/esm/remote-data.js.map +0 -1
- package/dist/types/remote-data.d.ts +0 -32
- package/dist/types/remote-data.d.ts.map +0 -1
- package/dist/web/remote-data.js +0 -112
- package/dist/web/remote-data.js.map +0 -1
- package/src/remote-data.ts +0 -149
package/src/index.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
// ─── Types ───────────────────────────────────────────
|
|
4
4
|
export * from './types.js';
|
|
5
|
-
export {
|
|
5
|
+
export {default as Type} from './type.js';
|
|
6
|
+
export {default as TypeCode} from './type-code.js';
|
|
6
7
|
|
|
7
8
|
// ─── Utilities ───────────────────────────────────────
|
|
8
9
|
export * from './utilities.js';
|
|
@@ -10,25 +11,26 @@ export * from './string.js';
|
|
|
10
11
|
export * from './math.js';
|
|
11
12
|
|
|
12
13
|
// ─── Data Structures ────────────────────────────────
|
|
13
|
-
export {
|
|
14
|
-
export {
|
|
14
|
+
export {default as KeyValuePair} from './key-value-pair.js';
|
|
15
|
+
export {default as Order} from './order.js';
|
|
15
16
|
|
|
16
17
|
// ─── Async & Timing ─────────────────────────────────
|
|
17
|
-
export {
|
|
18
|
-
export {
|
|
19
|
-
export {
|
|
20
|
-
export {
|
|
18
|
+
export {default as Timer} from './timer.js';
|
|
19
|
+
export {default as TimeSpan} from './timespan.js';
|
|
20
|
+
export {default as Stopwatch} from './stopwatch.js';
|
|
21
|
+
export {default as AsyncPoolExecutor} from './async-pool-executor.js';
|
|
21
22
|
|
|
22
23
|
// ─── Patterns ────────────────────────────────────────
|
|
23
|
-
export {
|
|
24
|
-
export {
|
|
25
|
-
export { default as from, From } from './from.js';
|
|
24
|
+
export {default as Lazy} from './lazy.js';
|
|
25
|
+
export {default as from, From} from './from.js';
|
|
26
26
|
|
|
27
27
|
// ─── Web ─────────────────────────────────────────────
|
|
28
|
-
export {
|
|
28
|
+
export {default as CleanURLSearchParams} from './clean-url-search-params.js';
|
|
29
|
+
/** @deprecated */
|
|
30
|
+
export * from './browser/index.js';
|
|
29
31
|
|
|
30
32
|
// ─── Deprecated ──────────────────────────────────────
|
|
31
|
-
/** @deprecated
|
|
33
|
+
/** @deprecated */
|
|
32
34
|
export * from './array.js';
|
|
33
|
-
/** @deprecated
|
|
35
|
+
/** @deprecated */
|
|
34
36
|
export * from './collections/index.js';
|
package/src/patch.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {Constructor} from "./types.js";
|
|
2
2
|
|
|
3
3
|
export type PureMethod<T extends object, A extends any[] = any[], R = any> =
|
|
4
4
|
(obj: T, ...args: A) => R;
|
|
@@ -8,7 +8,7 @@ export type PatchSpec<T extends object> =
|
|
|
8
8
|
| Map<PropertyKey, PureMethod<T, any[], any>>;
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
export function patch<C extends
|
|
11
|
+
export function patch<C extends Constructor<any>>(
|
|
12
12
|
targetOrCtor: C,
|
|
13
13
|
fns: PatchSpec<InstanceType<C>>
|
|
14
14
|
): Map<PropertyKey, PropertyDescriptor | null>;
|
|
@@ -53,7 +53,7 @@ export function patch(
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// Overloads unpatch
|
|
56
|
-
export function unpatch<C extends
|
|
56
|
+
export function unpatch<C extends Constructor<any>>(
|
|
57
57
|
targetOrCtor: C,
|
|
58
58
|
changes: Map<PropertyKey, PropertyDescriptor | null>
|
|
59
59
|
): void;
|
package/src/type-code.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
enum TypeCode {
|
|
2
|
+
String = 'string',
|
|
3
|
+
Number = 'number',
|
|
4
|
+
Boolean = 'boolean',
|
|
5
|
+
Symbol = 'symbol',
|
|
6
|
+
BigInt = 'bigint',
|
|
7
|
+
Null = 'null',
|
|
8
|
+
Undefined = 'undefined',
|
|
9
|
+
Object = 'object',
|
|
10
|
+
Function = 'function',
|
|
11
|
+
Array = 'array',
|
|
12
|
+
Unknown = 'unknown',
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export default TypeCode;
|
package/src/type.ts
CHANGED
|
@@ -1,19 +1,372 @@
|
|
|
1
|
+
import TypeCode from "./type-code.js";
|
|
2
|
+
import {type Constructor} from "./types.js";
|
|
1
3
|
|
|
2
|
-
|
|
3
|
-
* Represents different types in JavaScript.
|
|
4
|
-
* @enum {string}
|
|
5
|
-
*/
|
|
6
|
-
enum Type {
|
|
7
|
-
Undefined = "undefined",
|
|
8
|
-
Number = "number",
|
|
9
|
-
String = "string",
|
|
10
|
-
Boolean = "boolean",
|
|
11
|
-
Object = "object",
|
|
12
|
-
Function = "function",
|
|
13
|
-
Symbol = "symbol",
|
|
14
|
-
BigInt = "bigint",
|
|
15
|
-
Null = "null"
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export default Type;
|
|
4
|
+
export default class Type<T = unknown> {
|
|
19
5
|
|
|
6
|
+
// ──────────────────────────────────────────────
|
|
7
|
+
// Caché (Flyweight)
|
|
8
|
+
// ──────────────────────────────────────────────
|
|
9
|
+
|
|
10
|
+
private static readonly _cache = new Map<unknown, Type>();
|
|
11
|
+
|
|
12
|
+
// ──────────────────────────────────────────────
|
|
13
|
+
// Tipos predefinidos
|
|
14
|
+
// ──────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
public static readonly String = Type._getOrCreate<string>('string', undefined, TypeCode.String, 'string');
|
|
17
|
+
public static readonly Number = Type._getOrCreate<number>('number', undefined, TypeCode.Number, 'number');
|
|
18
|
+
public static readonly Boolean = Type._getOrCreate<boolean>('boolean', undefined, TypeCode.Boolean, 'boolean');
|
|
19
|
+
public static readonly Symbol = Type._getOrCreate<symbol>('symbol', undefined, TypeCode.Symbol, 'symbol');
|
|
20
|
+
public static readonly BigInt = Type._getOrCreate<bigint>('bigint', undefined, TypeCode.BigInt, 'bigint');
|
|
21
|
+
public static readonly Null = Type._getOrCreate<null>('null', undefined, TypeCode.Null, 'null');
|
|
22
|
+
public static readonly Undefined = Type._getOrCreate<undefined>('undefined', undefined, TypeCode.Undefined, 'undefined');
|
|
23
|
+
public static readonly Object = Type._getOrCreate<object>(Object, Object, TypeCode.Object, 'Object');
|
|
24
|
+
public static readonly Function = Type._getOrCreate<Function>(Function, Function, TypeCode.Function, 'Function');
|
|
25
|
+
public static readonly Array = Type._getOrCreate<unknown[]>(Array, Array, TypeCode.Array, 'Array');
|
|
26
|
+
|
|
27
|
+
// ──────────────────────────────────────────────
|
|
28
|
+
// Propiedades inmutables
|
|
29
|
+
// ──────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
public readonly name: string;
|
|
32
|
+
public readonly constructorRef:
|
|
33
|
+
Constructor<T> | undefined;
|
|
34
|
+
public readonly typeCode: TypeCode;
|
|
35
|
+
|
|
36
|
+
private constructor(
|
|
37
|
+
name: string,
|
|
38
|
+
constructorRef: Constructor<T> | undefined,
|
|
39
|
+
typeCode: TypeCode,
|
|
40
|
+
) {
|
|
41
|
+
this.name = name;
|
|
42
|
+
this.constructorRef = constructorRef;
|
|
43
|
+
this.typeCode = typeCode;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ══════════════════════════════════════════════
|
|
47
|
+
// ÚNICO PUNTO DE ENTRADA
|
|
48
|
+
// ══════════════════════════════════════════════
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Obtiene el `Type` de cualquier valor, constructor o primitivo.
|
|
52
|
+
*
|
|
53
|
+
* Unifica `typeof()` y `.GetType()` de C# en un solo método.
|
|
54
|
+
*
|
|
55
|
+
* **Reglas de resolución:**
|
|
56
|
+
* 1. `null` / `undefined` → tipo nullish correspondiente
|
|
57
|
+
* 2. Primitivo (`number`, `string`, etc.) → tipo primitivo
|
|
58
|
+
* 3. Array → `Type.Array`
|
|
59
|
+
* 4. Función/Constructor → tipo del constructor (no "Function")
|
|
60
|
+
* 5. Objeto → tipo basado en su constructor
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* Type.of(42) // Type<number> — primitivo
|
|
65
|
+
* Type.of('hello') // Type<string> — primitivo
|
|
66
|
+
* Type.of(new Date()) // Type<Date> — desde instancia
|
|
67
|
+
* Type.of(Date) // Type<Date> — desde constructor
|
|
68
|
+
* Type.of(null) // Type<null>
|
|
69
|
+
* Type.of([1, 2, 3]) // Type<Array>
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
public static of<U>(target: U | Constructor<U>): Type<U> {
|
|
73
|
+
// ── Nullish ──
|
|
74
|
+
if (target === null) return Type.Null as Type<U>;
|
|
75
|
+
if (target === undefined) return Type.Undefined as Type<U>;
|
|
76
|
+
|
|
77
|
+
const jsType = typeof target;
|
|
78
|
+
|
|
79
|
+
// ── Primitivos ──
|
|
80
|
+
if (jsType !== 'object' && jsType !== 'function') {
|
|
81
|
+
return this._getOrCreate<U>(jsType, undefined, jsType as TypeCode);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ── Funciones y Constructores ──
|
|
85
|
+
if (jsType === 'function') {
|
|
86
|
+
return this._resolveFunction<U>(target as Function & Constructor<U>);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ── Arrays ──
|
|
90
|
+
if (Array.isArray(target)) {
|
|
91
|
+
return Type.Array as Type<U>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ── Objetos ──
|
|
95
|
+
return this._resolveObject<U>(target as object);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ──────────────────────────────────────────────
|
|
99
|
+
// Resolvers privados (separan la lógica compleja)
|
|
100
|
+
// ──────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Resuelve funciones y constructores.
|
|
104
|
+
*
|
|
105
|
+
* Decisión clave: si la función tiene un `prototype` con
|
|
106
|
+
* miembros propios o empieza con `class`, es un constructor
|
|
107
|
+
* y devolvemos el tipo que construye. Si no, es una función.
|
|
108
|
+
*/
|
|
109
|
+
private static _resolveFunction<U>(fn: Function & Constructor<U>): Type<U> {
|
|
110
|
+
const isConstructor = this._isConstructorLike(fn);
|
|
111
|
+
|
|
112
|
+
if (isConstructor) {
|
|
113
|
+
// Constructor → devuelve el tipo que construye
|
|
114
|
+
return this._getOrCreate<U>(
|
|
115
|
+
fn, // cacheamos por referencia al constructor
|
|
116
|
+
fn as Constructor<U>,
|
|
117
|
+
TypeCode.Object,
|
|
118
|
+
fn.name || 'AnonymousClass',
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Función pura → devuelve Type<Function>
|
|
123
|
+
return Type.Function as Type<U>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Resuelve objetos por su cadena de prototipos.
|
|
128
|
+
*/
|
|
129
|
+
private static _resolveObject<U>(obj: object): Type<U> {
|
|
130
|
+
const proto = Object.getPrototypeOf(obj) as { constructor?: Constructor<U> } | null;
|
|
131
|
+
const ctor = proto?.constructor as Constructor<U> | undefined;
|
|
132
|
+
const name = ctor?.name || 'Object';
|
|
133
|
+
|
|
134
|
+
if (!ctor || ctor === (Object as unknown)) {
|
|
135
|
+
return Type.Object as Type<U>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return this._getOrCreate<U>(ctor, ctor, TypeCode.Object, name);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Heurística para distinguir constructores de funciones planas.
|
|
143
|
+
*
|
|
144
|
+
* Un "constructor" cumple al menos una de:
|
|
145
|
+
* - Su `toString()` empieza con "class "
|
|
146
|
+
* - Tiene propiedades propias en su prototype (más allá de constructor)
|
|
147
|
+
* - Es un built-in conocido (Date, Map, Set, etc.)
|
|
148
|
+
*/
|
|
149
|
+
private static _isConstructorLike(fn: Function): boolean {
|
|
150
|
+
// 1. Clase ES6 explícita
|
|
151
|
+
try {
|
|
152
|
+
if (fn.toString().startsWith('class ')) return true;
|
|
153
|
+
} catch {
|
|
154
|
+
// toString puede fallar en Proxy, etc.
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// 2. Built-ins conocidos con prototype significativo
|
|
158
|
+
if (fn === Object || fn === Function || fn === Array) return true;
|
|
159
|
+
|
|
160
|
+
const proto = fn.prototype;
|
|
161
|
+
if (!proto || typeof proto !== 'object') return false;
|
|
162
|
+
|
|
163
|
+
// 3. Prototype con miembros propios (señal de constructor)
|
|
164
|
+
const ownKeys = Object.getOwnPropertyNames(proto);
|
|
165
|
+
return ownKeys.length > 1 || (ownKeys.length === 1 && ownKeys[0] !== 'constructor');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ══════════════════════════════════════════════
|
|
169
|
+
// PROPIEDADES COMPUTADAS
|
|
170
|
+
// ══════════════════════════════════════════════
|
|
171
|
+
|
|
172
|
+
public get isPrimitive(): boolean {
|
|
173
|
+
return (
|
|
174
|
+
this.typeCode === TypeCode.String ||
|
|
175
|
+
this.typeCode === TypeCode.Number ||
|
|
176
|
+
this.typeCode === TypeCode.Boolean ||
|
|
177
|
+
this.typeCode === TypeCode.Symbol ||
|
|
178
|
+
this.typeCode === TypeCode.BigInt
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public get isNullish(): boolean {
|
|
183
|
+
return this.typeCode === TypeCode.Null || this.typeCode === TypeCode.Undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public get isReferenceType(): boolean {
|
|
187
|
+
return !this.isPrimitive && !this.isNullish;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public get isArray(): boolean {
|
|
191
|
+
return this.typeCode === TypeCode.Array ||
|
|
192
|
+
this.constructorRef === (Array as unknown as Constructor<T>);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
public get isFunction(): boolean {
|
|
196
|
+
return this.typeCode === TypeCode.Function;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
public get isValueType(): boolean {
|
|
200
|
+
return this.isPrimitive;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public get isClass(): boolean {
|
|
204
|
+
if (!this.constructorRef) return false;
|
|
205
|
+
try {
|
|
206
|
+
return this.constructorRef.toString().startsWith('class ');
|
|
207
|
+
} catch {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public get fullName(): string {
|
|
213
|
+
if (this.isPrimitive || this.isNullish) {
|
|
214
|
+
return `System.${this.name.charAt(0).toUpperCase()}${this.name.slice(1)}`;
|
|
215
|
+
}
|
|
216
|
+
return this.name;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ══════════════════════════════════════════════
|
|
220
|
+
// JERARQUÍA
|
|
221
|
+
// ══════════════════════════════════════════════
|
|
222
|
+
|
|
223
|
+
public get baseType(): Type | undefined {
|
|
224
|
+
if (!this.constructorRef) return undefined;
|
|
225
|
+
|
|
226
|
+
const parentProto = Object.getPrototypeOf(this.constructorRef.prototype);
|
|
227
|
+
if (!parentProto || parentProto === Object.prototype) {
|
|
228
|
+
return this === Type.Object ? undefined : Type.Object;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const parentCtor = parentProto.constructor as Constructor;
|
|
232
|
+
return Type.of(parentCtor);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
public getInheritanceChain(): ReadonlyArray<Type> {
|
|
236
|
+
const chain: Type[] = [this];
|
|
237
|
+
let current: Type | undefined = this.baseType;
|
|
238
|
+
|
|
239
|
+
while (current && !chain.includes(current)) {
|
|
240
|
+
chain.push(current);
|
|
241
|
+
current = current.baseType;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return Object.freeze(chain);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
public isAssignableTo(target: Type): boolean {
|
|
248
|
+
if (this === target) return true;
|
|
249
|
+
if (!this.constructorRef || !target.constructorRef) return false;
|
|
250
|
+
|
|
251
|
+
let proto: object | null = this.constructorRef.prototype;
|
|
252
|
+
while (proto) {
|
|
253
|
+
if (proto.constructor === target.constructorRef) return true;
|
|
254
|
+
proto = Object.getPrototypeOf(proto) as object | null;
|
|
255
|
+
}
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
public isAssignableFrom(source: Type): boolean {
|
|
260
|
+
return source.isAssignableTo(this);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
public isSubclassOf(parent: Type): boolean {
|
|
264
|
+
if (this === parent) return false;
|
|
265
|
+
return this.isAssignableTo(parent);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
// ══════════════════════════════════════════════
|
|
270
|
+
// REFLEXIÓN
|
|
271
|
+
// ══════════════════════════════════════════════
|
|
272
|
+
|
|
273
|
+
public getMethodNames(): ReadonlyArray<string> {
|
|
274
|
+
if (!this.constructorRef?.prototype) return Object.freeze([]);
|
|
275
|
+
return Object.freeze(
|
|
276
|
+
Object.getOwnPropertyNames(this.constructorRef.prototype)
|
|
277
|
+
.filter(n => n !== 'constructor' &&
|
|
278
|
+
typeof Object.getOwnPropertyDescriptor(
|
|
279
|
+
this.constructorRef!.prototype, n
|
|
280
|
+
)?.value === 'function'
|
|
281
|
+
)
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
public getPropertyNames(): ReadonlyArray<string> {
|
|
286
|
+
if (!this.constructorRef?.prototype) return Object.freeze([]);
|
|
287
|
+
return Object.freeze(
|
|
288
|
+
Object.getOwnPropertyNames(this.constructorRef.prototype)
|
|
289
|
+
.filter(n => n !== 'constructor' &&
|
|
290
|
+
typeof Object.getOwnPropertyDescriptor(
|
|
291
|
+
this.constructorRef!.prototype, n
|
|
292
|
+
)?.value !== 'function'
|
|
293
|
+
)
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
public getMemberNames(): ReadonlyArray<string> {
|
|
298
|
+
if (!this.constructorRef?.prototype) return Object.freeze([]);
|
|
299
|
+
return Object.freeze(
|
|
300
|
+
Object.getOwnPropertyNames(this.constructorRef.prototype)
|
|
301
|
+
.filter(n => n !== 'constructor')
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
public hasMethod(name: string): boolean {
|
|
306
|
+
if (!this.constructorRef?.prototype) return false;
|
|
307
|
+
return typeof this.constructorRef.prototype[name] === 'function';
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
public hasProperty(name: string): boolean {
|
|
311
|
+
if (!this.constructorRef?.prototype) return false;
|
|
312
|
+
return name in this.constructorRef.prototype && !this.hasMethod(name);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
public getStaticMemberNames(): ReadonlyArray<string> {
|
|
316
|
+
if (!this.constructorRef) return Object.freeze([]);
|
|
317
|
+
const skip = new Set(['prototype', 'length', 'name', 'caller', 'arguments']);
|
|
318
|
+
return Object.freeze(
|
|
319
|
+
Object.getOwnPropertyNames(this.constructorRef)
|
|
320
|
+
.filter(n => !skip.has(n))
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
// ══════════════════════════════════════════════
|
|
326
|
+
// SERIALIZACIÓN
|
|
327
|
+
// ══════════════════════════════════════════════
|
|
328
|
+
|
|
329
|
+
public toString(): string {
|
|
330
|
+
return `Type [${this.name}]`;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
public [Symbol.toPrimitive](hint: string): string | number {
|
|
335
|
+
|
|
336
|
+
let hash = 0;
|
|
337
|
+
for (let i = 0; i < this.name.length; i++) {
|
|
338
|
+
hash = ((hash << 5) - hash + this.name.charCodeAt(i)) | 0;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return hint === 'number' ? hash : this.toString();
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
public get [Symbol.toStringTag](): string {
|
|
345
|
+
return `Type<${this.name}>`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// ──────────────────────────────────────────────
|
|
349
|
+
// FACTORY INTERNA
|
|
350
|
+
// ──────────────────────────────────────────────
|
|
351
|
+
|
|
352
|
+
private static _getOrCreate<V>(
|
|
353
|
+
key: unknown,
|
|
354
|
+
constructorRef: Constructor<V> | unknown,
|
|
355
|
+
typeCode: TypeCode,
|
|
356
|
+
customName?: string,
|
|
357
|
+
): Type<V> {
|
|
358
|
+
const cached = this._cache.get(key);
|
|
359
|
+
if (cached) return cached as Type<V>;
|
|
360
|
+
|
|
361
|
+
const name = customName
|
|
362
|
+
?? (typeof key === 'string' ? key : 'unknown');
|
|
363
|
+
|
|
364
|
+
const ref = typeof constructorRef === 'function'
|
|
365
|
+
? constructorRef as Constructor<V>
|
|
366
|
+
: undefined;
|
|
367
|
+
|
|
368
|
+
const instance = new Type<V>(name, ref, typeCode);
|
|
369
|
+
this._cache.set(key, instance as Type);
|
|
370
|
+
return instance;
|
|
371
|
+
}
|
|
372
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -25,8 +25,4 @@ export type QuaternaryFunction<T1, T2, T3, T4, R> = (
|
|
|
25
25
|
arg4: T4
|
|
26
26
|
) => R;
|
|
27
27
|
|
|
28
|
-
export type Constructor<T =
|
|
29
|
-
new (...args: A) => T;
|
|
30
|
-
|
|
31
|
-
export type AbstractConstructor<T = object, A extends any[] = any[]> =
|
|
32
|
-
abstract new (...args: A) => T;
|
|
28
|
+
export type Constructor<T = unknown> = abstract new (...args: any[]) => T;
|