@develia/commons 0.4.15 → 0.4.18
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/array.js +38 -2
- package/dist/cjs/array.js.map +1 -1
- package/dist/cjs/async-pool-executor.js +26 -11
- package/dist/cjs/async-pool-executor.js.map +1 -1
- package/dist/cjs/browser/form-utilities.js +35 -34
- package/dist/cjs/browser/form-utilities.js.map +1 -1
- package/dist/cjs/browser/geo-location.js +1 -1
- package/dist/cjs/browser/geo-location.js.map +1 -1
- package/dist/cjs/browser/native-bridge.js +3 -1
- package/dist/cjs/browser/native-bridge.js.map +1 -1
- package/dist/cjs/cancellable-promise.js +34 -13
- package/dist/cjs/cancellable-promise.js.map +1 -1
- package/dist/cjs/clean-url-search-params.js +26 -3
- package/dist/cjs/clean-url-search-params.js.map +1 -1
- package/dist/cjs/collections/cache-dictionary.js +3 -3
- package/dist/cjs/collections/cache-dictionary.js.map +1 -1
- package/dist/cjs/collections/default-map.js.map +1 -1
- package/dist/cjs/collections/default-weak-map.js.map +1 -1
- package/dist/cjs/collections/from.js +3 -3
- package/dist/cjs/collections/from.js.map +1 -1
- package/dist/cjs/collections/index.js.map +1 -1
- package/dist/cjs/draft.js +46 -11
- package/dist/cjs/draft.js.map +1 -1
- package/dist/cjs/event-bus.js +1 -1
- package/dist/cjs/event-bus.js.map +1 -1
- package/dist/cjs/functions.js +348 -0
- package/dist/cjs/functions.js.map +1 -0
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/key-value-pair.js.map +1 -1
- package/dist/cjs/lazy.js.map +1 -1
- package/dist/cjs/math.js +3 -0
- package/dist/cjs/math.js.map +1 -1
- package/dist/cjs/order.js.map +1 -1
- package/dist/cjs/parse.js +10 -1
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/patch.js.map +1 -1
- package/dist/cjs/range.js +154 -0
- package/dist/cjs/range.js.map +1 -0
- package/dist/cjs/stopwatch.js.map +1 -1
- package/dist/cjs/string.js +8 -5
- package/dist/cjs/string.js.map +1 -1
- package/dist/cjs/timer.js +1 -1
- package/dist/cjs/timer.js.map +1 -1
- package/dist/cjs/timespan.js +21 -8
- package/dist/cjs/timespan.js.map +1 -1
- package/dist/cjs/type-code.js.map +1 -1
- package/dist/cjs/type.js +20 -19
- package/dist/cjs/type.js.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utilities.js +175 -39
- package/dist/cjs/utilities.js.map +1 -1
- package/dist/esm/array.js +36 -2
- package/dist/esm/array.js.map +1 -1
- package/dist/esm/async-pool-executor.js +26 -11
- package/dist/esm/async-pool-executor.js.map +1 -1
- package/dist/esm/browser/form-utilities.js +35 -34
- package/dist/esm/browser/form-utilities.js.map +1 -1
- package/dist/esm/browser/geo-location.js +1 -1
- package/dist/esm/browser/geo-location.js.map +1 -1
- package/dist/esm/browser/native-bridge.js +3 -1
- package/dist/esm/browser/native-bridge.js.map +1 -1
- package/dist/esm/cancellable-promise.js +34 -13
- package/dist/esm/cancellable-promise.js.map +1 -1
- package/dist/esm/clean-url-search-params.js +26 -3
- package/dist/esm/clean-url-search-params.js.map +1 -1
- package/dist/esm/collections/cache-dictionary.js +3 -3
- package/dist/esm/collections/cache-dictionary.js.map +1 -1
- package/dist/esm/collections/default-map.js.map +1 -1
- package/dist/esm/collections/default-weak-map.js.map +1 -1
- package/dist/esm/collections/from.js +3 -3
- package/dist/esm/collections/from.js.map +1 -1
- package/dist/esm/collections/index.js.map +1 -1
- package/dist/esm/draft.js +46 -11
- package/dist/esm/draft.js.map +1 -1
- package/dist/esm/event-bus.js +1 -1
- package/dist/esm/event-bus.js.map +1 -1
- package/dist/esm/functions.js +336 -0
- package/dist/esm/functions.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/key-value-pair.js.map +1 -1
- package/dist/esm/lazy.js.map +1 -1
- package/dist/esm/math.js +3 -0
- package/dist/esm/math.js.map +1 -1
- package/dist/esm/order.js.map +1 -1
- package/dist/esm/parse.js +10 -1
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/range.js +151 -0
- package/dist/esm/range.js.map +1 -0
- package/dist/esm/stopwatch.js.map +1 -1
- package/dist/esm/string.js +8 -5
- package/dist/esm/string.js.map +1 -1
- package/dist/esm/timer.js +1 -1
- package/dist/esm/timer.js.map +1 -1
- package/dist/esm/timespan.js +21 -8
- package/dist/esm/timespan.js.map +1 -1
- package/dist/esm/type-code.js.map +1 -1
- package/dist/esm/type.js +20 -19
- package/dist/esm/type.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utilities.js +174 -39
- package/dist/esm/utilities.js.map +1 -1
- package/dist/types/array.d.ts +29 -0
- package/dist/types/array.d.ts.map +1 -1
- package/dist/types/async-pool-executor.d.ts +2 -2
- package/dist/types/async-pool-executor.d.ts.map +1 -1
- package/dist/types/browser/form-utilities.d.ts.map +1 -1
- package/dist/types/browser/native-bridge.d.ts.map +1 -1
- package/dist/types/cancellable-promise.d.ts.map +1 -1
- package/dist/types/clean-url-search-params.d.ts +3 -1
- package/dist/types/clean-url-search-params.d.ts.map +1 -1
- package/dist/types/collections/from.d.ts +1 -1
- package/dist/types/draft.d.ts.map +1 -1
- package/dist/types/functions.d.ts +231 -0
- package/dist/types/functions.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/math.d.ts.map +1 -1
- package/dist/types/parse.d.ts.map +1 -1
- package/dist/types/range.d.ts +72 -0
- package/dist/types/range.d.ts.map +1 -0
- package/dist/types/string.d.ts.map +1 -1
- package/dist/types/timer.d.ts +1 -1
- package/dist/types/timespan.d.ts.map +1 -1
- package/dist/types/type.d.ts +2 -0
- package/dist/types/type.d.ts.map +1 -1
- package/dist/types/types.d.ts +1 -1
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utilities.d.ts +21 -3
- package/dist/types/utilities.d.ts.map +1 -1
- package/dist/web/array.js +36 -2
- package/dist/web/array.js.map +1 -1
- package/dist/web/async-pool-executor.js +26 -11
- package/dist/web/async-pool-executor.js.map +1 -1
- package/dist/web/browser/form-utilities.js +35 -34
- package/dist/web/browser/form-utilities.js.map +1 -1
- package/dist/web/browser/geo-location.js +1 -1
- package/dist/web/browser/geo-location.js.map +1 -1
- package/dist/web/browser/native-bridge.js +3 -1
- package/dist/web/browser/native-bridge.js.map +1 -1
- package/dist/web/cancellable-promise.js +34 -13
- package/dist/web/cancellable-promise.js.map +1 -1
- package/dist/web/clean-url-search-params.js +26 -3
- package/dist/web/clean-url-search-params.js.map +1 -1
- package/dist/web/collections/cache-dictionary.js +3 -3
- package/dist/web/collections/cache-dictionary.js.map +1 -1
- package/dist/web/collections/default-map.js.map +1 -1
- package/dist/web/collections/default-weak-map.js.map +1 -1
- package/dist/web/collections/from.js +3 -3
- package/dist/web/collections/from.js.map +1 -1
- package/dist/web/collections/index.js.map +1 -1
- package/dist/web/draft.js +46 -11
- package/dist/web/draft.js.map +1 -1
- package/dist/web/event-bus.js +1 -1
- package/dist/web/event-bus.js.map +1 -1
- package/dist/web/functions.js +336 -0
- package/dist/web/functions.js.map +1 -0
- package/dist/web/index.js +2 -0
- package/dist/web/index.js.map +1 -1
- package/dist/web/key-value-pair.js.map +1 -1
- package/dist/web/lazy.js.map +1 -1
- package/dist/web/math.js +3 -0
- package/dist/web/math.js.map +1 -1
- package/dist/web/order.js.map +1 -1
- package/dist/web/parse.js +10 -1
- package/dist/web/parse.js.map +1 -1
- package/dist/web/patch.js.map +1 -1
- package/dist/web/range.js +151 -0
- package/dist/web/range.js.map +1 -0
- package/dist/web/stopwatch.js.map +1 -1
- package/dist/web/string.js +8 -5
- package/dist/web/string.js.map +1 -1
- package/dist/web/timer.js +1 -1
- package/dist/web/timer.js.map +1 -1
- package/dist/web/timespan.js +21 -8
- package/dist/web/timespan.js.map +1 -1
- package/dist/web/type-code.js.map +1 -1
- package/dist/web/type.js +20 -19
- package/dist/web/type.js.map +1 -1
- package/dist/web/types.js.map +1 -1
- package/dist/web/utilities.js +174 -39
- package/dist/web/utilities.js.map +1 -1
- package/package.json +66 -60
- package/src/array.ts +116 -68
- package/src/async-pool-executor.ts +107 -85
- package/src/browser/form-utilities.ts +32 -31
- package/src/browser/geo-location.ts +113 -113
- package/src/browser/native-bridge.ts +118 -116
- package/src/cancellable-promise.ts +102 -76
- package/src/clean-url-search-params.ts +36 -11
- package/src/collections/cache-dictionary.ts +115 -115
- package/src/collections/default-map.ts +48 -48
- package/src/collections/default-weak-map.ts +51 -51
- package/src/collections/from.ts +859 -859
- package/src/collections/index.ts +3 -3
- package/src/draft.ts +55 -16
- package/src/event-bus.ts +83 -83
- package/src/functions.ts +496 -0
- package/src/index.ts +44 -42
- package/src/key-value-pair.ts +24 -24
- package/src/lazy.ts +32 -32
- package/src/math.ts +33 -30
- package/src/order.ts +5 -5
- package/src/parse.ts +251 -240
- package/src/patch.ts +79 -79
- package/src/range.ts +165 -0
- package/src/stopwatch.ts +43 -43
- package/src/string.ts +8 -5
- package/src/timer.ts +67 -67
- package/src/timespan.ts +412 -398
- package/src/type-code.ts +15 -15
- package/src/type.ts +372 -371
- package/src/types.ts +30 -30
- package/src/utilities.ts +653 -480
package/src/type.ts
CHANGED
|
@@ -1,372 +1,373 @@
|
|
|
1
|
-
import TypeCode from "./type-code.js";
|
|
2
|
-
import {type Constructor} from "./types.js";
|
|
3
|
-
|
|
4
|
-
export default class Type<T = unknown> {
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
public
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
);
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
//
|
|
327
|
-
//
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
//
|
|
350
|
-
//
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
1
|
+
import TypeCode from "./type-code.js";
|
|
2
|
+
import {type Constructor} from "./types.js";
|
|
3
|
+
|
|
4
|
+
export default class Type<T = unknown> {
|
|
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
|
+
return Object.freeze(
|
|
275
|
+
this._getPrototypeMemberNames((_, descriptor) => typeof descriptor.value === 'function')
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
public getPropertyNames(): ReadonlyArray<string> {
|
|
280
|
+
return Object.freeze(
|
|
281
|
+
this._getPrototypeMemberNames((_, descriptor) => typeof descriptor.value !== 'function')
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
public getMemberNames(): ReadonlyArray<string> {
|
|
286
|
+
return Object.freeze(this._getPrototypeMemberNames());
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
public hasMethod(name: string): boolean {
|
|
290
|
+
const descriptor = this._getOwnPrototypeDescriptor(name);
|
|
291
|
+
return typeof descriptor?.value === 'function';
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
public hasProperty(name: string): boolean {
|
|
295
|
+
const descriptor = this._getOwnPrototypeDescriptor(name);
|
|
296
|
+
return !!descriptor && typeof descriptor.value !== 'function';
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private _getPrototypeMemberNames(
|
|
300
|
+
predicate: (name: string, descriptor: PropertyDescriptor) => boolean = () => true
|
|
301
|
+
): string[] {
|
|
302
|
+
if (!this.constructorRef?.prototype) return [];
|
|
303
|
+
|
|
304
|
+
return Object.getOwnPropertyNames(this.constructorRef.prototype)
|
|
305
|
+
.filter(name => {
|
|
306
|
+
const descriptor = this._getOwnPrototypeDescriptor(name);
|
|
307
|
+
return !!descriptor && predicate(name, descriptor);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private _getOwnPrototypeDescriptor(name: string): PropertyDescriptor | undefined {
|
|
312
|
+
if (name === 'constructor' || !this.constructorRef?.prototype) return undefined;
|
|
313
|
+
return Object.getOwnPropertyDescriptor(this.constructorRef.prototype, name);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
public getStaticMemberNames(): ReadonlyArray<string> {
|
|
317
|
+
if (!this.constructorRef) return Object.freeze([]);
|
|
318
|
+
const skip = new Set(['prototype', 'length', 'name', 'caller', 'arguments']);
|
|
319
|
+
return Object.freeze(
|
|
320
|
+
Object.getOwnPropertyNames(this.constructorRef)
|
|
321
|
+
.filter(n => !skip.has(n))
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
// ══════════════════════════════════════════════
|
|
327
|
+
// SERIALIZACIÓN
|
|
328
|
+
// ══════════════════════════════════════════════
|
|
329
|
+
|
|
330
|
+
public toString(): string {
|
|
331
|
+
return `Type [${this.name}]`;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
public [Symbol.toPrimitive](hint: string): string | number {
|
|
336
|
+
|
|
337
|
+
let hash = 0;
|
|
338
|
+
for (let i = 0; i < this.name.length; i++) {
|
|
339
|
+
hash = ((hash << 5) - hash + this.name.charCodeAt(i)) | 0;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return hint === 'number' ? hash : this.toString();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
public get [Symbol.toStringTag](): string {
|
|
346
|
+
return `Type<${this.name}>`;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ──────────────────────────────────────────────
|
|
350
|
+
// FACTORY INTERNA
|
|
351
|
+
// ──────────────────────────────────────────────
|
|
352
|
+
|
|
353
|
+
private static _getOrCreate<V>(
|
|
354
|
+
key: unknown,
|
|
355
|
+
constructorRef: Constructor<V> | unknown,
|
|
356
|
+
typeCode: TypeCode,
|
|
357
|
+
customName?: string,
|
|
358
|
+
): Type<V> {
|
|
359
|
+
const cached = this._cache.get(key);
|
|
360
|
+
if (cached) return cached as Type<V>;
|
|
361
|
+
|
|
362
|
+
const name = customName
|
|
363
|
+
?? (typeof key === 'string' ? key : 'unknown');
|
|
364
|
+
|
|
365
|
+
const ref = typeof constructorRef === 'function'
|
|
366
|
+
? constructorRef as Constructor<V>
|
|
367
|
+
: undefined;
|
|
368
|
+
|
|
369
|
+
const instance = new Type<V>(name, ref, typeCode);
|
|
370
|
+
this._cache.set(key, instance as Type);
|
|
371
|
+
return instance;
|
|
372
|
+
}
|
|
372
373
|
}
|