@develia/commons 0.4.8 → 0.4.12

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.
Files changed (103) hide show
  1. package/dist/cjs/array.js +5 -4
  2. package/dist/cjs/array.js.map +1 -1
  3. package/dist/cjs/browser/form-utilities.js +29 -23
  4. package/dist/cjs/browser/form-utilities.js.map +1 -1
  5. package/dist/cjs/browser/geo-location.js +61 -37
  6. package/dist/cjs/browser/geo-location.js.map +1 -1
  7. package/dist/cjs/browser/index.js +46 -1
  8. package/dist/cjs/browser/index.js.map +1 -1
  9. package/dist/cjs/cancellable-promise.js +60 -0
  10. package/dist/cjs/cancellable-promise.js.map +1 -0
  11. package/dist/cjs/event-bus.js +70 -0
  12. package/dist/cjs/event-bus.js.map +1 -0
  13. package/dist/cjs/from.js +1 -1
  14. package/dist/cjs/from.js.map +1 -1
  15. package/dist/cjs/index.js +6 -1
  16. package/dist/cjs/index.js.map +1 -1
  17. package/dist/cjs/math.js +3 -2
  18. package/dist/cjs/math.js.map +1 -1
  19. package/dist/cjs/parse.js +70 -0
  20. package/dist/cjs/parse.js.map +1 -0
  21. package/dist/cjs/patch.js +3 -2
  22. package/dist/cjs/patch.js.map +1 -1
  23. package/dist/cjs/string.js +43 -3
  24. package/dist/cjs/string.js.map +1 -1
  25. package/dist/cjs/utilities.js +25 -24
  26. package/dist/cjs/utilities.js.map +1 -1
  27. package/dist/esm/browser/form-utilities.js +23 -18
  28. package/dist/esm/browser/form-utilities.js.map +1 -1
  29. package/dist/esm/browser/geo-location.js +60 -36
  30. package/dist/esm/browser/geo-location.js.map +1 -1
  31. package/dist/esm/browser/index.js +44 -0
  32. package/dist/esm/browser/index.js.map +1 -1
  33. package/dist/esm/cancellable-promise.js +57 -0
  34. package/dist/esm/cancellable-promise.js.map +1 -0
  35. package/dist/esm/event-bus.js +65 -0
  36. package/dist/esm/event-bus.js.map +1 -0
  37. package/dist/esm/index.js +2 -0
  38. package/dist/esm/index.js.map +1 -1
  39. package/dist/esm/parse.js +64 -0
  40. package/dist/esm/parse.js.map +1 -0
  41. package/dist/esm/string.js +34 -0
  42. package/dist/esm/string.js.map +1 -1
  43. package/dist/types/browser/form-utilities.d.ts +2 -2
  44. package/dist/types/browser/form-utilities.d.ts.map +1 -1
  45. package/dist/types/browser/geo-location.d.ts.map +1 -1
  46. package/dist/types/browser/index.d.ts +7 -0
  47. package/dist/types/browser/index.d.ts.map +1 -1
  48. package/dist/types/cancellable-promise.d.ts +13 -0
  49. package/dist/types/cancellable-promise.d.ts.map +1 -0
  50. package/dist/types/event-bus.d.ts +21 -0
  51. package/dist/types/event-bus.d.ts.map +1 -0
  52. package/dist/types/index.d.ts +2 -0
  53. package/dist/types/index.d.ts.map +1 -1
  54. package/dist/types/parse.d.ts +16 -0
  55. package/dist/types/parse.d.ts.map +1 -0
  56. package/dist/types/string.d.ts +5 -0
  57. package/dist/types/string.d.ts.map +1 -1
  58. package/dist/web/browser/form-utilities.js +23 -18
  59. package/dist/web/browser/form-utilities.js.map +1 -1
  60. package/dist/web/browser/geo-location.js +60 -36
  61. package/dist/web/browser/geo-location.js.map +1 -1
  62. package/dist/web/browser/index.js +44 -0
  63. package/dist/web/browser/index.js.map +1 -1
  64. package/dist/web/cancellable-promise.js +57 -0
  65. package/dist/web/cancellable-promise.js.map +1 -0
  66. package/dist/web/event-bus.js +65 -0
  67. package/dist/web/event-bus.js.map +1 -0
  68. package/dist/web/index.js +2 -0
  69. package/dist/web/index.js.map +1 -1
  70. package/dist/web/parse.js +64 -0
  71. package/dist/web/parse.js.map +1 -0
  72. package/dist/web/string.js +34 -0
  73. package/dist/web/string.js.map +1 -1
  74. package/package.json +56 -56
  75. package/src/array.ts +0 -0
  76. package/src/async-pool-executor.ts +0 -0
  77. package/src/browser/form-utilities.ts +179 -173
  78. package/src/browser/geo-location.ts +113 -79
  79. package/src/browser/index.ts +78 -4
  80. package/src/browser/native-bridge.ts +116 -116
  81. package/src/cancellable-promise.ts +77 -0
  82. package/src/clean-url-search-params.ts +0 -0
  83. package/src/collections/cache-dictionary.ts +0 -0
  84. package/src/collections/default-map.ts +0 -0
  85. package/src/collections/default-weak-map.ts +0 -0
  86. package/src/collections/index.ts +2 -2
  87. package/src/event-bus.ts +84 -0
  88. package/src/from.ts +0 -0
  89. package/src/index.ts +37 -35
  90. package/src/key-value-pair.ts +0 -0
  91. package/src/lazy.ts +0 -0
  92. package/src/math.ts +0 -0
  93. package/src/order.ts +0 -0
  94. package/src/parse.ts +241 -0
  95. package/src/patch.ts +0 -0
  96. package/src/stopwatch.ts +0 -0
  97. package/src/string.ts +82 -2
  98. package/src/timer.ts +0 -0
  99. package/src/timespan.ts +0 -0
  100. package/src/type-code.ts +15 -15
  101. package/src/type.ts +371 -371
  102. package/src/types.ts +30 -30
  103. package/src/utilities.ts +404 -404
@@ -1,117 +1,117 @@
1
- // ─── Tipos ───────────────────────────────────────────────
2
- interface BridgeOptions {
3
- channel: Channel;
4
- timeout?: number | null;
5
- callbackName?: string; // ← nuevo
6
- }
7
- interface PendingCall {
8
- resolve: (value: unknown) => void;
9
- reject: (reason: Error) => void;
10
- timer: ReturnType<typeof setTimeout> | null;
11
- }
12
-
13
- interface Channel {
14
- postMessage: (message: string) => void;
15
- }
16
-
17
-
18
-
19
- // ─── Clase ──────────────────────────────────────────────
20
-
21
- export default class NativeBridge {
22
- private _pending = new Map<number, PendingCall>();
23
- private _callId = 0;
24
- private readonly _defaultTimeout: number | null;
25
- private readonly _channel: Channel;
26
-
27
-
28
- constructor({ channel, timeout = 3000, callbackName = '__nativeResponseHandler' }: BridgeOptions) {
29
- this._defaultTimeout = timeout;
30
- this._channel = channel;
31
-
32
- if (!this._channel) {
33
- throw new Error('Channel not found');
34
- }
35
-
36
- (window as any)[callbackName] = this._callback;
37
-
38
- // Proxy: cualquier propiedad no existente se convierte en call()
39
- return new Proxy(this, {
40
- get(target: NativeBridge, prop: string | symbol) {
41
- if (prop in target) {
42
- return (target as any)[prop];
43
- }
44
- return (data?: unknown, timeout?: number | null) => target.call(String(prop), data, timeout);
45
- },
46
- }) as unknown as NativeBridge;
47
- }
48
-
49
-
50
- private _callback(id: number, error: any, result: any) {
51
-
52
- const pending = this._pending.get(id);
53
- if (!pending) {
54
- return;
55
- }
56
-
57
- if (pending.timer !== null) {
58
- clearTimeout(pending.timer);
59
- }
60
-
61
- this._pending.delete(id);
62
-
63
- if (error) {
64
- pending.reject(error instanceof Error? error : new Error(String(error)));
65
- } else {
66
- pending.resolve(result);
67
- }
68
- }
69
-
70
- call(method: string, data: unknown = null, timeout?: number | null): Promise<unknown> {
71
-
72
-
73
- const effectiveTimeout = timeout !== undefined ? timeout : this._defaultTimeout;
74
-
75
- return new Promise<unknown>((resolve, reject) => {
76
- const id = this._callId++;
77
-
78
- let timer: ReturnType<typeof setTimeout> | null = null;
79
-
80
- // Solo crear timer si hay timeout
81
- if (effectiveTimeout !== null && effectiveTimeout > 0) {
82
- timer = setTimeout(() => {
83
- this._pending.delete(id);
84
- reject(new Error(`Timeout (${effectiveTimeout}ms): ${method}`));
85
- }, effectiveTimeout);
86
- }
87
-
88
- this._pending.set(id, { resolve, reject, timer });
89
- this._channel!.postMessage(JSON.stringify({ id, method, data }));
90
- });
91
- }
92
-
93
-
94
- send(method: string, data: unknown = null): void {
95
- this._channel!.postMessage(JSON.stringify({ method, data }));
96
- }
97
-
98
- /**
99
- * Número de llamadas pendientes
100
- */
101
- get pendingCount(): number {
102
- return this._pending.size;
103
- }
104
-
105
- /**
106
- * Cancela todas las llamadas pendientes
107
- */
108
- cancelAll(): void {
109
- for (const [, { reject, timer }] of this._pending) {
110
- if (timer !== null) {
111
- clearTimeout(timer);
112
- }
113
- reject(new Error('Cancelled'));
114
- }
115
- this._pending.clear();
116
- }
1
+ // ─── Tipos ───────────────────────────────────────────────
2
+ interface BridgeOptions {
3
+ channel: Channel;
4
+ timeout?: number | null;
5
+ callbackName?: string; // ← nuevo
6
+ }
7
+ interface PendingCall {
8
+ resolve: (value: unknown) => void;
9
+ reject: (reason: Error) => void;
10
+ timer: ReturnType<typeof setTimeout> | null;
11
+ }
12
+
13
+ interface Channel {
14
+ postMessage: (message: string) => void;
15
+ }
16
+
17
+
18
+
19
+ // ─── Clase ──────────────────────────────────────────────
20
+
21
+ export default class NativeBridge {
22
+ private _pending = new Map<number, PendingCall>();
23
+ private _callId = 0;
24
+ private readonly _defaultTimeout: number | null;
25
+ private readonly _channel: Channel;
26
+
27
+
28
+ constructor({ channel, timeout = 3000, callbackName = '__nativeResponseHandler' }: BridgeOptions) {
29
+ this._defaultTimeout = timeout;
30
+ this._channel = channel;
31
+
32
+ if (!this._channel) {
33
+ throw new Error('Channel not found');
34
+ }
35
+
36
+ (window as any)[callbackName] = this._callback;
37
+
38
+ // Proxy: cualquier propiedad no existente se convierte en call()
39
+ return new Proxy(this, {
40
+ get(target: NativeBridge, prop: string | symbol) {
41
+ if (prop in target) {
42
+ return (target as any)[prop];
43
+ }
44
+ return (data?: unknown, timeout?: number | null) => target.call(String(prop), data, timeout);
45
+ },
46
+ }) as unknown as NativeBridge;
47
+ }
48
+
49
+
50
+ private _callback(id: number, error: any, result: any) {
51
+
52
+ const pending = this._pending.get(id);
53
+ if (!pending) {
54
+ return;
55
+ }
56
+
57
+ if (pending.timer !== null) {
58
+ clearTimeout(pending.timer);
59
+ }
60
+
61
+ this._pending.delete(id);
62
+
63
+ if (error) {
64
+ pending.reject(error instanceof Error? error : new Error(String(error)));
65
+ } else {
66
+ pending.resolve(result);
67
+ }
68
+ }
69
+
70
+ call(method: string, data: unknown = null, timeout?: number | null): Promise<unknown> {
71
+
72
+
73
+ const effectiveTimeout = timeout !== undefined ? timeout : this._defaultTimeout;
74
+
75
+ return new Promise<unknown>((resolve, reject) => {
76
+ const id = this._callId++;
77
+
78
+ let timer: ReturnType<typeof setTimeout> | null = null;
79
+
80
+ // Solo crear timer si hay timeout
81
+ if (effectiveTimeout !== null && effectiveTimeout > 0) {
82
+ timer = setTimeout(() => {
83
+ this._pending.delete(id);
84
+ reject(new Error(`Timeout (${effectiveTimeout}ms): ${method}`));
85
+ }, effectiveTimeout);
86
+ }
87
+
88
+ this._pending.set(id, { resolve, reject, timer });
89
+ this._channel!.postMessage(JSON.stringify({ id, method, data }));
90
+ });
91
+ }
92
+
93
+
94
+ send(method: string, data: unknown = null): void {
95
+ this._channel!.postMessage(JSON.stringify({ method, data }));
96
+ }
97
+
98
+ /**
99
+ * Número de llamadas pendientes
100
+ */
101
+ get pendingCount(): number {
102
+ return this._pending.size;
103
+ }
104
+
105
+ /**
106
+ * Cancela todas las llamadas pendientes
107
+ */
108
+ cancelAll(): void {
109
+ for (const [, { reject, timer }] of this._pending) {
110
+ if (timer !== null) {
111
+ clearTimeout(timer);
112
+ }
113
+ reject(new Error('Cancelled'));
114
+ }
115
+ this._pending.clear();
116
+ }
117
117
  }
@@ -0,0 +1,77 @@
1
+ type CancellablePromiseExecutor<T> = (
2
+ resolve: (value: T | PromiseLike<T>) => void,
3
+ reject: (reason?: unknown) => void,
4
+ signal: AbortSignal
5
+ ) => void;
6
+
7
+ export default class CancellablePromise<T> extends Promise<T> {
8
+
9
+ private _controller: AbortController;
10
+
11
+ static get [Symbol.species]() {
12
+ return Promise;
13
+ }
14
+
15
+ constructor(executor: CancellablePromiseExecutor<T>) {
16
+ const controller = new AbortController();
17
+
18
+ super((resolve, reject) => {
19
+ controller.signal.addEventListener("abort", () => {
20
+ reject(controller.signal.reason);
21
+ });
22
+
23
+ executor(resolve, reject, controller.signal);
24
+ });
25
+
26
+ this._controller = controller;
27
+ }
28
+
29
+ get cancelled(): boolean {
30
+ return this._controller.signal.aborted;
31
+ }
32
+
33
+ cancel(reason: unknown = new Error("Promesa cancelada")): void {
34
+ if (!this.cancelled) {
35
+ this._controller.abort(reason);
36
+ }
37
+ }
38
+
39
+ static async compete<T>(promises: CancellablePromise<T>[]): Promise<T> {
40
+ return await Promise.race(promises).then(
41
+ (resultado) => {
42
+ for (const p of promises) {
43
+ p.cancel();
44
+ }
45
+ return resultado;
46
+ },
47
+ (error) => {
48
+ for (const p of promises) {
49
+ p.cancel();
50
+ }
51
+ throw error;
52
+ }
53
+ );
54
+ }
55
+
56
+ static async cooperate<T>(promises: CancellablePromise<T>[]): Promise<T[]> {
57
+ try {
58
+ return await Promise.all(promises);
59
+ } catch (error) {
60
+ for (const p of promises) {
61
+ p.cancel();
62
+ }
63
+ throw error;
64
+ }
65
+ }
66
+
67
+ static withTimeout<T>(executor: CancellablePromiseExecutor<T>, ms: number): CancellablePromise<T> {
68
+ return new CancellablePromise<T>((resolve, reject, signal) => {
69
+ const timer = setTimeout(() => {
70
+ reject(new Error(`Timeout: ${ms}ms`));
71
+ }, ms);
72
+
73
+ signal.addEventListener("abort", () => clearTimeout(timer));
74
+ executor(resolve, reject, signal);
75
+ });
76
+ }
77
+ }
File without changes
File without changes
File without changes
File without changes
@@ -1,3 +1,3 @@
1
- export {default as DefaultWeakMap} from './default-weak-map.js'
2
- export {default as DefaultMap} from './default-map.js'
1
+ export {default as DefaultWeakMap} from './default-weak-map.js'
2
+ export {default as DefaultMap} from './default-map.js'
3
3
  export {default as CacheDictionary} from './cache-dictionary.js';
@@ -0,0 +1,84 @@
1
+ // event.ts
2
+
3
+ export abstract class Event {
4
+ readonly timestamp: Date;
5
+
6
+ protected constructor() {
7
+ this.timestamp = new Date();
8
+ }
9
+
10
+ toString(): string {
11
+ return `${this.constructor.name}(${this.timestamp.toISOString()})`;
12
+ }
13
+ }
14
+
15
+ type EventClass<T extends Event> = new (...args: any[]) => T;
16
+ type Handler<T extends Event> = (event: T) => void;
17
+
18
+ interface HandlerWrapper {
19
+ original: Function;
20
+ wrapped: (event: Event) => void;
21
+ }
22
+
23
+ export class EventBus {
24
+ private static _instance: EventBus | null = null;
25
+
26
+ static get shared(): EventBus {
27
+ return this._instance ??= new EventBus();
28
+ }
29
+
30
+ private _handlers = new Map<Function, HandlerWrapper[]>();
31
+
32
+ on<T extends Event>(type: EventClass<T>, handler: Handler<T>): void {
33
+ if (!this._handlers.has(type)) {
34
+ this._handlers.set(type, []);
35
+ }
36
+ this._handlers.get(type)!.push({
37
+ original: handler,
38
+ wrapped: (event: Event) => handler(event as T),
39
+ });
40
+ }
41
+
42
+ off<T extends Event>(type: EventClass<T>, handler: Handler<T>): void {
43
+ const handlers = this._handlers.get(type);
44
+ if (!handlers) return;
45
+
46
+ let index = handlers.findIndex((h) => h.original === handler);
47
+ if (index !== -1) {
48
+ handlers.splice(index, 1);
49
+ }
50
+ }
51
+
52
+ emit<T extends Event>(event: T): void {
53
+ const handlers = this._handlers.get(event.constructor);
54
+ if (!handlers) return;
55
+
56
+ for (const handler of [...handlers]) {
57
+ try {
58
+ handler.wrapped(event);
59
+ } catch (e) {
60
+ console.error(`[EventBus] Error en handler de ${event.constructor.name}:`, e);
61
+ }
62
+ }
63
+ }
64
+
65
+ once<T extends Event>(type: EventClass<T>, handler: Handler<T>): void {
66
+ const wrapper: Handler<T> = (event: T) => {
67
+ handler(event);
68
+ this.off(type, wrapper);
69
+ };
70
+ this.on(type, wrapper);
71
+ }
72
+
73
+ clear<T extends Event>(type: EventClass<T>): void {
74
+ this._handlers.delete(type);
75
+ }
76
+
77
+ clearAll(): void {
78
+ this._handlers.clear();
79
+ }
80
+
81
+ hasListeners<T extends Event>(type: EventClass<T>): boolean {
82
+ return (this._handlers.get(type)?.length ?? 0) > 0;
83
+ }
84
+ }
package/src/from.ts CHANGED
File without changes
package/src/index.ts CHANGED
@@ -1,36 +1,38 @@
1
- // src/index.ts
2
-
3
- // ─── Types ───────────────────────────────────────────
4
- export * from './types.js';
5
- export {default as Type} from './type.js';
6
- export {default as TypeCode} from './type-code.js';
7
-
8
- // ─── Utilities ───────────────────────────────────────
9
- export * from './utilities.js';
10
- export * from './string.js';
11
- export * from './math.js';
12
-
13
- // ─── Data Structures ────────────────────────────────
14
- export {default as KeyValuePair} from './key-value-pair.js';
15
- export {default as Order} from './order.js';
16
-
17
- // ─── Async & Timing ─────────────────────────────────
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';
22
-
23
- // ─── Patterns ────────────────────────────────────────
24
- export {default as Lazy} from './lazy.js';
25
- export {default as from, From} from './from.js';
26
-
27
- // ─── Web ─────────────────────────────────────────────
28
- export {default as CleanURLSearchParams} from './clean-url-search-params.js';
29
- /** @deprecated */
30
- export { ajaxSubmit, ajaxSubmission, submitForm , formToObject} from './browser/index.js';
31
-
32
- // ─── Deprecated ──────────────────────────────────────
33
- /** @deprecated */
34
- export * from './array.js';
35
- /** @deprecated */
1
+ // src/index.ts
2
+
3
+ // ─── Types ───────────────────────────────────────────
4
+ export * from './types.js';
5
+ export {default as Type} from './type.js';
6
+ export {default as TypeCode} from './type-code.js';
7
+
8
+ // ─── Utilities ───────────────────────────────────────
9
+ export * from './utilities.js';
10
+ export * from './string.js';
11
+ export * from './math.js';
12
+ export {default as CancellablePromise} from "./cancellable-promise.js";
13
+
14
+ // ─── Data Structures ────────────────────────────────
15
+ export {default as KeyValuePair} from './key-value-pair.js';
16
+ export {default as Order} from './order.js';
17
+
18
+ // ─── Async & Timing ─────────────────────────────────
19
+ export {default as Timer} from './timer.js';
20
+ export {default as TimeSpan} from './timespan.js';
21
+ export {default as Stopwatch} from './stopwatch.js';
22
+ export {default as AsyncPoolExecutor} from './async-pool-executor.js';
23
+
24
+ // ─── Patterns ────────────────────────────────────────
25
+ export {default as Lazy} from './lazy.js';
26
+ export {default as from, From} from './from.js';
27
+ export {EventBus, Event} from './event-bus.js';
28
+
29
+ // ─── Web ─────────────────────────────────────────────
30
+ export {default as CleanURLSearchParams} from './clean-url-search-params.js';
31
+ /** @deprecated */
32
+ export {ajaxSubmit, ajaxSubmission, submitForm, formToObject} from './browser/index.js';
33
+
34
+ // ─── Deprecated ──────────────────────────────────────
35
+ /** @deprecated */
36
+ export * from './array.js';
37
+ /** @deprecated */
36
38
  export * from './collections/index.js';
File without changes
package/src/lazy.ts CHANGED
File without changes
package/src/math.ts CHANGED
File without changes
package/src/order.ts CHANGED
File without changes