@develia/commons 0.3.44 → 0.4.1

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 (200) hide show
  1. package/dist/cjs/array.js +44 -0
  2. package/dist/cjs/array.js.map +1 -0
  3. package/dist/cjs/async-pool-executor.js +49 -0
  4. package/dist/cjs/async-pool-executor.js.map +1 -0
  5. package/dist/cjs/clean-url-search-params.js +10 -0
  6. package/dist/cjs/clean-url-search-params.js.map +1 -0
  7. package/dist/cjs/collections/cache-dictionary.js +67 -0
  8. package/dist/cjs/collections/cache-dictionary.js.map +1 -0
  9. package/dist/cjs/collections/default-map.js +21 -0
  10. package/dist/cjs/collections/default-map.js.map +1 -0
  11. package/dist/cjs/collections/default-weak-map.js +26 -0
  12. package/dist/cjs/collections/default-weak-map.js.map +1 -0
  13. package/dist/cjs/collections/index.js +13 -0
  14. package/dist/cjs/collections/index.js.map +1 -0
  15. package/dist/cjs/from.js +504 -0
  16. package/dist/cjs/from.js.map +1 -0
  17. package/dist/cjs/index.js +50 -0
  18. package/dist/cjs/index.js.map +1 -0
  19. package/dist/cjs/key-value-pair.js +16 -0
  20. package/dist/cjs/key-value-pair.js.map +1 -0
  21. package/dist/cjs/lazy.js +25 -0
  22. package/dist/cjs/lazy.js.map +1 -0
  23. package/dist/cjs/math.js +15 -0
  24. package/dist/cjs/math.js.map +1 -0
  25. package/dist/cjs/order.js +9 -0
  26. package/dist/cjs/order.js.map +1 -0
  27. package/dist/cjs/patch.js +44 -0
  28. package/dist/cjs/patch.js.map +1 -0
  29. package/dist/cjs/remote-data.js +115 -0
  30. package/dist/cjs/remote-data.js.map +1 -0
  31. package/dist/cjs/stopwatch.js +36 -0
  32. package/dist/cjs/stopwatch.js.map +1 -0
  33. package/dist/cjs/string.js +29 -0
  34. package/dist/cjs/string.js.map +1 -0
  35. package/dist/cjs/timer.js +50 -0
  36. package/dist/cjs/timer.js.map +1 -0
  37. package/dist/cjs/timespan.js +152 -0
  38. package/dist/cjs/timespan.js.map +1 -0
  39. package/dist/cjs/type.js +16 -0
  40. package/dist/cjs/type.js.map +1 -0
  41. package/dist/cjs/types.js +3 -0
  42. package/dist/cjs/types.js.map +1 -0
  43. package/dist/cjs/utilities.js +309 -0
  44. package/dist/cjs/utilities.js.map +1 -0
  45. package/dist/esm/array.js +38 -0
  46. package/dist/esm/array.js.map +1 -0
  47. package/dist/esm/async-pool-executor.js +46 -0
  48. package/dist/esm/async-pool-executor.js.map +1 -0
  49. package/dist/esm/clean-url-search-params.js +7 -0
  50. package/dist/esm/clean-url-search-params.js.map +1 -0
  51. package/dist/esm/collections/cache-dictionary.js +64 -0
  52. package/dist/esm/collections/cache-dictionary.js.map +1 -0
  53. package/dist/esm/collections/default-map.js +18 -0
  54. package/dist/esm/collections/default-map.js.map +1 -0
  55. package/dist/esm/collections/default-weak-map.js +23 -0
  56. package/dist/esm/collections/default-weak-map.js.map +1 -0
  57. package/dist/esm/collections/index.js +4 -0
  58. package/dist/esm/collections/index.js.map +1 -0
  59. package/dist/esm/from.js +495 -0
  60. package/dist/esm/from.js.map +1 -0
  61. package/dist/esm/index.js +18 -0
  62. package/dist/esm/index.js.map +1 -0
  63. package/dist/esm/key-value-pair.js +13 -0
  64. package/dist/esm/key-value-pair.js.map +1 -0
  65. package/dist/esm/lazy.js +22 -0
  66. package/dist/esm/lazy.js.map +1 -0
  67. package/dist/esm/math.js +11 -0
  68. package/dist/esm/math.js.map +1 -0
  69. package/dist/esm/order.js +7 -0
  70. package/dist/esm/order.js.map +1 -0
  71. package/dist/esm/patch.js +40 -0
  72. package/dist/esm/patch.js.map +1 -0
  73. package/dist/esm/remote-data.js +112 -0
  74. package/dist/esm/remote-data.js.map +1 -0
  75. package/dist/esm/stopwatch.js +30 -0
  76. package/dist/esm/stopwatch.js.map +1 -0
  77. package/dist/esm/string.js +24 -0
  78. package/dist/esm/string.js.map +1 -0
  79. package/dist/esm/timer.js +47 -0
  80. package/dist/esm/timer.js.map +1 -0
  81. package/dist/esm/timespan.js +150 -0
  82. package/dist/esm/timespan.js.map +1 -0
  83. package/dist/esm/type.js +14 -0
  84. package/dist/esm/type.js.map +1 -0
  85. package/dist/esm/types.js +2 -0
  86. package/dist/esm/types.js.map +1 -0
  87. package/dist/esm/utilities.js +277 -0
  88. package/dist/esm/utilities.js.map +1 -0
  89. package/dist/types/array.d.ts +6 -0
  90. package/dist/types/array.d.ts.map +1 -0
  91. package/dist/types/async-pool-executor.d.ts +14 -0
  92. package/dist/types/async-pool-executor.d.ts.map +1 -0
  93. package/dist/types/clean-url-search-params.d.ts +4 -0
  94. package/dist/types/clean-url-search-params.d.ts.map +1 -0
  95. package/dist/types/collections/cache-dictionary.d.ts +16 -0
  96. package/dist/types/collections/cache-dictionary.d.ts.map +1 -0
  97. package/dist/types/collections/default-map.d.ts +7 -0
  98. package/dist/types/collections/default-map.d.ts.map +1 -0
  99. package/dist/types/collections/default-weak-map.d.ts +7 -0
  100. package/dist/types/collections/default-weak-map.d.ts.map +1 -0
  101. package/dist/types/collections/index.d.ts +4 -0
  102. package/dist/types/collections/index.d.ts.map +1 -0
  103. package/dist/types/from.d.ts +59 -0
  104. package/dist/types/from.d.ts.map +1 -0
  105. package/dist/types/index.d.ts +18 -0
  106. package/dist/types/index.d.ts.map +1 -0
  107. package/dist/types/key-value-pair.d.ts +8 -0
  108. package/dist/types/key-value-pair.d.ts.map +1 -0
  109. package/dist/types/lazy.d.ts +11 -0
  110. package/dist/types/lazy.d.ts.map +1 -0
  111. package/dist/types/math.d.ts +3 -0
  112. package/dist/types/math.d.ts.map +1 -0
  113. package/dist/types/order.d.ts +6 -0
  114. package/dist/types/order.d.ts.map +1 -0
  115. package/dist/types/patch.d.ts +8 -0
  116. package/dist/types/patch.d.ts.map +1 -0
  117. package/dist/types/remote-data.d.ts +32 -0
  118. package/dist/types/remote-data.d.ts.map +1 -0
  119. package/dist/types/stopwatch.d.ts +10 -0
  120. package/dist/types/stopwatch.d.ts.map +1 -0
  121. package/dist/types/string.d.ts +4 -0
  122. package/dist/types/string.d.ts.map +1 -0
  123. package/dist/types/timer.d.ts +15 -0
  124. package/dist/types/timer.d.ts.map +1 -0
  125. package/dist/types/timespan.d.ts +47 -0
  126. package/dist/types/timespan.d.ts.map +1 -0
  127. package/dist/types/type.d.ts +13 -0
  128. package/dist/types/type.d.ts.map +1 -0
  129. package/dist/types/types.d.ts +14 -0
  130. package/dist/types/types.d.ts.map +1 -0
  131. package/dist/types/utilities.d.ts +31 -0
  132. package/dist/types/utilities.d.ts.map +1 -0
  133. package/dist/web/array.js +38 -0
  134. package/dist/web/array.js.map +1 -0
  135. package/dist/web/async-pool-executor.js +46 -0
  136. package/dist/web/async-pool-executor.js.map +1 -0
  137. package/dist/web/clean-url-search-params.js +7 -0
  138. package/dist/web/clean-url-search-params.js.map +1 -0
  139. package/dist/web/collections/cache-dictionary.js +64 -0
  140. package/dist/web/collections/cache-dictionary.js.map +1 -0
  141. package/dist/web/collections/default-map.js +18 -0
  142. package/dist/web/collections/default-map.js.map +1 -0
  143. package/dist/web/collections/default-weak-map.js +23 -0
  144. package/dist/web/collections/default-weak-map.js.map +1 -0
  145. package/dist/web/collections/index.js +4 -0
  146. package/dist/web/collections/index.js.map +1 -0
  147. package/dist/web/from.js +496 -0
  148. package/dist/web/from.js.map +1 -0
  149. package/dist/web/index.js +18 -0
  150. package/dist/web/index.js.map +1 -0
  151. package/dist/web/key-value-pair.js +13 -0
  152. package/dist/web/key-value-pair.js.map +1 -0
  153. package/dist/web/lazy.js +22 -0
  154. package/dist/web/lazy.js.map +1 -0
  155. package/dist/web/math.js +11 -0
  156. package/dist/web/math.js.map +1 -0
  157. package/dist/web/order.js +7 -0
  158. package/dist/web/order.js.map +1 -0
  159. package/dist/web/patch.js +40 -0
  160. package/dist/web/patch.js.map +1 -0
  161. package/dist/web/remote-data.js +112 -0
  162. package/dist/web/remote-data.js.map +1 -0
  163. package/dist/web/stopwatch.js +30 -0
  164. package/dist/web/stopwatch.js.map +1 -0
  165. package/dist/web/string.js +24 -0
  166. package/dist/web/string.js.map +1 -0
  167. package/dist/web/timer.js +47 -0
  168. package/dist/web/timer.js.map +1 -0
  169. package/dist/web/timespan.js +150 -0
  170. package/dist/web/timespan.js.map +1 -0
  171. package/dist/web/type.js +14 -0
  172. package/dist/web/type.js.map +1 -0
  173. package/dist/web/types.js +2 -0
  174. package/dist/web/types.js.map +1 -0
  175. package/dist/web/utilities.js +277 -0
  176. package/dist/web/utilities.js.map +1 -0
  177. package/package.json +34 -12
  178. package/src/array.ts +49 -0
  179. package/src/async-pool-executor.ts +2 -2
  180. package/src/collections/default-map.ts +42 -0
  181. package/src/collections/default-weak-map.ts +49 -0
  182. package/src/collections/index.ts +3 -0
  183. package/src/from.ts +4 -4
  184. package/src/index.ts +34 -22
  185. package/src/lazy.ts +1 -1
  186. package/src/patch.ts +80 -0
  187. package/src/remote-data.ts +149 -0
  188. package/src/stopwatch.ts +1 -1
  189. package/src/{typing.ts → type.ts} +3 -1
  190. package/src/types.ts +4 -1
  191. package/src/utilities.ts +19 -18
  192. package/dist/develia-commons.js +0 -1690
  193. package/dist/develia-commons.min.js +0 -1
  194. package/dist/index.cjs.js +0 -1688
  195. package/dist/index.cjs.js.map +0 -1
  196. package/dist/index.esm.js +0 -1641
  197. package/dist/index.esm.js.map +0 -1
  198. package/src/array-manipulator.ts +0 -159
  199. package/src/collections.ts +0 -24
  200. /package/src/{cache-dictionary.ts → collections/cache-dictionary.ts} +0 -0
package/src/patch.ts ADDED
@@ -0,0 +1,80 @@
1
+ import type {AbstractConstructor} from "./types.js";
2
+
3
+ export type PureMethod<T extends object, A extends any[] = any[], R = any> =
4
+ (obj: T, ...args: A) => R;
5
+
6
+ export type PatchSpec<T extends object> =
7
+ | Record<string, PureMethod<T, any[], any>>
8
+ | Map<PropertyKey, PureMethod<T, any[], any>>;
9
+
10
+
11
+ export function patch<C extends AbstractConstructor<any>>(
12
+ targetOrCtor: C,
13
+ fns: PatchSpec<InstanceType<C>>
14
+ ): Map<PropertyKey, PropertyDescriptor | null>;
15
+
16
+ export function patch<T extends object>(
17
+ targetOrCtor: T,
18
+ fns: PatchSpec<T>
19
+ ): Map<PropertyKey, PropertyDescriptor | null>;
20
+
21
+ /**
22
+ * Devuelve un Map donde:
23
+ * - clave -> null (método añadido nuevo)
24
+ * - clave -> descriptorOriginal (método sustituido)
25
+ */
26
+ export function patch(
27
+ targetOrCtor: any,
28
+ fns: PatchSpec<any>
29
+ ): Map<PropertyKey, PropertyDescriptor | null> {
30
+ const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
31
+
32
+ const result = new Map<PropertyKey, PropertyDescriptor | null>();
33
+ const entries: Array<[PropertyKey, PureMethod<any>]> =
34
+ fns instanceof Map ? [...fns.entries()] : Object.entries(fns) as any;
35
+
36
+ for (const [key, fn] of entries) {
37
+ const prev = Object.getOwnPropertyDescriptor(target, key);
38
+ const method = function (this: unknown, ...args: unknown[]) {
39
+ return (fn as any)(this, ...args);
40
+ };
41
+ try {
42
+ Object.defineProperty(target, key, {
43
+ value: method,
44
+ writable: true,
45
+ configurable: true,
46
+ enumerable: false,
47
+ });
48
+ result.set(key, prev ?? null);
49
+ } catch { /* ignorar */
50
+ }
51
+ }
52
+ return result;
53
+ }
54
+
55
+ // Overloads unpatch
56
+ export function unpatch<C extends AbstractConstructor<any>>(
57
+ targetOrCtor: C,
58
+ changes: Map<PropertyKey, PropertyDescriptor | null>
59
+ ): void;
60
+ export function unpatch<T extends object>(
61
+ targetOrCtor: T,
62
+ changes: Map<PropertyKey, PropertyDescriptor | null>
63
+ ): void;
64
+
65
+ /**
66
+ * Revierte un patch a partir del mapa devuelto por `patch`.
67
+ */
68
+ export function unpatch(targetOrCtor: any, changes: Map<PropertyKey, PropertyDescriptor | null>): void {
69
+ const target = typeof targetOrCtor === "function" ? targetOrCtor.prototype : targetOrCtor;
70
+ for (const [key, prev] of changes) {
71
+ try {
72
+ if (prev === null) {
73
+ if (Object.prototype.hasOwnProperty.call(target, key)) delete target[key];
74
+ } else {
75
+ Object.defineProperty(target, key, prev);
76
+ }
77
+ } catch { /* ignorar */
78
+ }
79
+ }
80
+ }
@@ -0,0 +1,149 @@
1
+ export interface RemoteDataOptions<T = unknown> {
2
+ /** Opciones nativas de fetch (headers, method, body, signal, etc.) */
3
+ fetchOptions?: RequestInit;
4
+ /** Función para transformar la respuesta antes de almacenarla */
5
+ transform?: (raw: unknown) => T;
6
+ /** Tiempo máximo de espera en ms */
7
+ timeout?: number;
8
+ /** Número de reintentos ante fallo (por defecto: 0) */
9
+ retries?: number;
10
+ /** Delay entre reintentos en ms (por defecto: 1000) */
11
+ retryDelay?: number;
12
+ /** Datos iniciales (evita el primer fetch si se proporcionan) */
13
+ initialData?: T;
14
+ }
15
+
16
+ export default class RemoteData<T = unknown> {
17
+ _loaded: boolean = false;
18
+ _loading: boolean = false;
19
+ _data: T | null = null;
20
+ _url: string;
21
+ _error: Error | null = null;
22
+ _promise: Promise<T | null> | null = null;
23
+ _options: RemoteDataOptions<T>;
24
+
25
+ constructor(url: string, options: RemoteDataOptions<T> = {}) {
26
+ this._url = url;
27
+ this._options = options;
28
+
29
+ if (options.initialData !== undefined) {
30
+ this._data = options.initialData;
31
+ this._loaded = true;
32
+ }
33
+ }
34
+
35
+ get url(): string {
36
+ return this._url;
37
+ }
38
+
39
+ set url(value: string) {
40
+ if (this._url !== value) {
41
+ this._url = value;
42
+ this._loaded = false;
43
+ }
44
+ }
45
+
46
+ get loaded(): boolean {
47
+ return this._loaded;
48
+ }
49
+
50
+ get loading(): boolean {
51
+ return this._loading;
52
+ }
53
+
54
+ get error(): Error | null {
55
+ return this._error;
56
+ }
57
+
58
+ get succeeded(): boolean {
59
+ return this._loaded && !this._error;
60
+ }
61
+
62
+ get data(): T | null {
63
+ return this._data;
64
+ }
65
+
66
+ async getData(): Promise<T | null> {
67
+ if (!this._loaded) {
68
+ await this.refresh();
69
+ }
70
+ return this._data;
71
+ }
72
+
73
+ async refresh(): Promise<T | null> {
74
+ if (this._loading && this._promise) {
75
+ return this._promise;
76
+ }
77
+ this._promise = this._fetchWithRetries();
78
+ return this._promise;
79
+ }
80
+
81
+ reset(): void {
82
+ this._loaded = false;
83
+ this._loading = false;
84
+ this._data = null;
85
+ this._error = null;
86
+ this._promise = null;
87
+ }
88
+
89
+ // ── Internos ────────────────────────────────────────────────
90
+
91
+ async _fetchWithRetries(): Promise<T | null> {
92
+ const maxAttempts = (this._options.retries ?? 0) + 1;
93
+ const retryDelay = this._options.retryDelay ?? 1000;
94
+
95
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
96
+ try {
97
+ return await this._doFetch();
98
+ } catch (error) {
99
+ if (attempt === maxAttempts) throw this._error;
100
+ await new Promise((r) => setTimeout(r, retryDelay));
101
+ }
102
+ }
103
+
104
+ return null; // unreachable, satisface al compilador
105
+ }
106
+
107
+ async _doFetch(): Promise<T | null> {
108
+ try {
109
+ this._error = null;
110
+ this._loading = true;
111
+
112
+ const fetchOptions = this._buildFetchOptions();
113
+ const response = await fetch(this._url, fetchOptions);
114
+
115
+ if (!response.ok) {
116
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
117
+ }
118
+
119
+ const raw = await response.json();
120
+ this._data = this._options.transform
121
+ ? this._options.transform(raw)
122
+ : (raw as T);
123
+ this._loaded = true;
124
+
125
+ return this._data;
126
+ } catch (error) {
127
+ this._error = error instanceof Error
128
+ ? error
129
+ : new Error(String(error));
130
+ this._loaded = false;
131
+ throw this._error;
132
+ } finally {
133
+ this._loading = false;
134
+ this._promise = null;
135
+ }
136
+ }
137
+
138
+ _buildFetchOptions(): RequestInit {
139
+ const options: RequestInit = {...this._options.fetchOptions};
140
+
141
+ if (this._options.timeout && this._options.timeout > 0) {
142
+ const controller = new AbortController();
143
+ setTimeout(() => controller.abort(), this._options.timeout);
144
+ options.signal = controller.signal;
145
+ }
146
+
147
+ return options;
148
+ }
149
+ }
package/src/stopwatch.ts CHANGED
@@ -1,4 +1,4 @@
1
- import TimeSpan from "./timespan";
1
+ import TimeSpan from "./timespan.js";
2
2
 
3
3
  export default class Stopwatch {
4
4
  private startTime: number | null = null;
@@ -3,7 +3,7 @@
3
3
  * Represents different types in JavaScript.
4
4
  * @enum {string}
5
5
  */
6
- export enum Type {
6
+ enum Type {
7
7
  Undefined = "undefined",
8
8
  Number = "number",
9
9
  String = "string",
@@ -15,3 +15,5 @@ export enum Type {
15
15
  Null = "null"
16
16
  }
17
17
 
18
+ export default Type;
19
+
package/src/types.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export type EqualityComparer<T> = (arg1: T, arg2: T) => boolean;
2
2
 
3
-
4
3
  export type Consumer<T> = (arg: T) => void
5
4
 
6
5
  export type Predicate<T> = (arg: T) => boolean;
@@ -26,4 +25,8 @@ export type QuaternaryFunction<T1, T2, T3, T4, R> = (
26
25
  arg4: T4
27
26
  ) => R;
28
27
 
28
+ export type Constructor<T = object, A extends any[] = any[]> =
29
+ new (...args: A) => T;
29
30
 
31
+ export type AbstractConstructor<T = object, A extends any[] = any[]> =
32
+ abstract new (...args: A) => T;
package/src/utilities.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  // noinspection JSUnusedGlobalSymbols
2
2
 
3
3
 
4
- import KeyValuePair from "./key-value-pair";
5
- import {Optional, Resolvable, Supplier} from "./types";
6
- import {Type} from "./typing";
4
+ import KeyValuePair from "./key-value-pair.js";
5
+ import type {Optional, Resolvable, Supplier} from "./types.js";
6
+ import Type from "./type.js";
7
7
 
8
8
  /**
9
9
  * Checks if an object is iterable.
@@ -454,24 +454,25 @@ export function getType(value: any): Type {
454
454
  }
455
455
 
456
456
  export function formToObject(form: HTMLFormElement): Record<string, FormDataEntryValue | FormDataEntryValue[]> {
457
- const fd = new FormData(form);
458
- const out: Record<string, FormDataEntryValue | FormDataEntryValue[]> = {};
459
-
460
- for (const [key, value] of fd.entries()) {
461
- if (key in out) {
462
- const current = out[key];
463
- if (Array.isArray(current)) {
464
- current.push(value);
465
- } else {
466
- out[key] = [current, value];
467
- }
468
- } else {
469
- out[key] = value;
457
+ const fd = new FormData(form);
458
+ const out: Record<string, FormDataEntryValue | FormDataEntryValue[]> = {};
459
+
460
+ for (const [key, value] of fd.entries()) {
461
+ if (key in out) {
462
+ const current = out[key];
463
+ if (Array.isArray(current)) {
464
+ current.push(value);
465
+ } else {
466
+ out[key] = [current, value];
467
+ }
468
+ } else {
469
+ out[key] = value;
470
+ }
470
471
  }
471
- }
472
472
 
473
- return out;
473
+ return out;
474
474
  }
475
+
475
476
  /**
476
477
  * Converts an object to `FormData` format recursively.
477
478
  *