@develia/commons 0.4.14 → 0.4.15

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 (220) hide show
  1. package/dist/cjs/array.js +27 -1
  2. package/dist/cjs/array.js.map +1 -1
  3. package/dist/cjs/async-pool-executor.js +15 -0
  4. package/dist/cjs/async-pool-executor.js.map +1 -1
  5. package/dist/cjs/browser/form-utilities.js +26 -0
  6. package/dist/cjs/browser/form-utilities.js.map +1 -1
  7. package/dist/cjs/browser/geo-location.js +5 -0
  8. package/dist/cjs/browser/geo-location.js.map +1 -1
  9. package/dist/cjs/browser/index.js.map +1 -1
  10. package/dist/cjs/browser/native-bridge.js +9 -0
  11. package/dist/cjs/browser/native-bridge.js.map +1 -1
  12. package/dist/cjs/cancellable-promise.js.map +1 -1
  13. package/dist/cjs/clean-url-search-params.js +1 -0
  14. package/dist/cjs/clean-url-search-params.js.map +1 -1
  15. package/dist/cjs/collections/cache-dictionary.js +39 -0
  16. package/dist/cjs/collections/cache-dictionary.js.map +1 -1
  17. package/dist/cjs/collections/default-map.js +25 -1
  18. package/dist/cjs/collections/default-map.js.map +1 -1
  19. package/dist/cjs/collections/default-weak-map.js +23 -3
  20. package/dist/cjs/collections/default-weak-map.js.map +1 -1
  21. package/dist/cjs/{from.js → collections/from.js} +278 -4
  22. package/dist/cjs/collections/from.js.map +1 -0
  23. package/dist/cjs/collections/index.js +3 -1
  24. package/dist/cjs/collections/index.js.map +1 -1
  25. package/dist/cjs/draft.js +37 -0
  26. package/dist/cjs/draft.js.map +1 -1
  27. package/dist/cjs/event-bus.js +1 -0
  28. package/dist/cjs/event-bus.js.map +1 -1
  29. package/dist/cjs/index.js +14 -3
  30. package/dist/cjs/index.js.map +1 -1
  31. package/dist/cjs/key-value-pair.js +6 -0
  32. package/dist/cjs/key-value-pair.js.map +1 -1
  33. package/dist/cjs/lazy.js +4 -0
  34. package/dist/cjs/lazy.js.map +1 -1
  35. package/dist/cjs/math.js +19 -0
  36. package/dist/cjs/math.js.map +1 -1
  37. package/dist/cjs/order.js.map +1 -1
  38. package/dist/cjs/parse.js +96 -0
  39. package/dist/cjs/parse.js.map +1 -1
  40. package/dist/cjs/patch.js +10 -2
  41. package/dist/cjs/patch.js.map +1 -1
  42. package/dist/cjs/stopwatch.js +10 -0
  43. package/dist/cjs/stopwatch.js.map +1 -1
  44. package/dist/cjs/string.js +62 -0
  45. package/dist/cjs/string.js.map +1 -1
  46. package/dist/cjs/timer.js +7 -0
  47. package/dist/cjs/timer.js.map +1 -1
  48. package/dist/cjs/timespan.js +202 -0
  49. package/dist/cjs/timespan.js.map +1 -1
  50. package/dist/cjs/type-code.js.map +1 -1
  51. package/dist/cjs/type.js +80 -1
  52. package/dist/cjs/type.js.map +1 -1
  53. package/dist/cjs/types.js.map +1 -1
  54. package/dist/cjs/utilities.js +183 -0
  55. package/dist/cjs/utilities.js.map +1 -1
  56. package/dist/esm/array.js +27 -1
  57. package/dist/esm/array.js.map +1 -1
  58. package/dist/esm/async-pool-executor.js +15 -0
  59. package/dist/esm/async-pool-executor.js.map +1 -1
  60. package/dist/esm/browser/form-utilities.js +26 -0
  61. package/dist/esm/browser/form-utilities.js.map +1 -1
  62. package/dist/esm/browser/geo-location.js +5 -0
  63. package/dist/esm/browser/geo-location.js.map +1 -1
  64. package/dist/esm/browser/index.js.map +1 -1
  65. package/dist/esm/browser/native-bridge.js +9 -0
  66. package/dist/esm/browser/native-bridge.js.map +1 -1
  67. package/dist/esm/cancellable-promise.js.map +1 -1
  68. package/dist/esm/clean-url-search-params.js +1 -0
  69. package/dist/esm/clean-url-search-params.js.map +1 -1
  70. package/dist/esm/collections/cache-dictionary.js +39 -0
  71. package/dist/esm/collections/cache-dictionary.js.map +1 -1
  72. package/dist/esm/collections/default-map.js +25 -1
  73. package/dist/esm/collections/default-map.js.map +1 -1
  74. package/dist/esm/collections/default-weak-map.js +22 -2
  75. package/dist/esm/collections/default-weak-map.js.map +1 -1
  76. package/dist/esm/{from.js → collections/from.js} +278 -4
  77. package/dist/esm/collections/from.js.map +1 -0
  78. package/dist/esm/collections/index.js +1 -0
  79. package/dist/esm/collections/index.js.map +1 -1
  80. package/dist/esm/draft.js +37 -0
  81. package/dist/esm/draft.js.map +1 -1
  82. package/dist/esm/event-bus.js +1 -0
  83. package/dist/esm/event-bus.js.map +1 -1
  84. package/dist/esm/index.js +13 -1
  85. package/dist/esm/index.js.map +1 -1
  86. package/dist/esm/key-value-pair.js +6 -0
  87. package/dist/esm/key-value-pair.js.map +1 -1
  88. package/dist/esm/lazy.js +4 -0
  89. package/dist/esm/lazy.js.map +1 -1
  90. package/dist/esm/math.js +19 -0
  91. package/dist/esm/math.js.map +1 -1
  92. package/dist/esm/order.js.map +1 -1
  93. package/dist/esm/parse.js +96 -0
  94. package/dist/esm/parse.js.map +1 -1
  95. package/dist/esm/patch.js +10 -2
  96. package/dist/esm/patch.js.map +1 -1
  97. package/dist/esm/stopwatch.js +10 -0
  98. package/dist/esm/stopwatch.js.map +1 -1
  99. package/dist/esm/string.js +62 -0
  100. package/dist/esm/string.js.map +1 -1
  101. package/dist/esm/timer.js +7 -0
  102. package/dist/esm/timer.js.map +1 -1
  103. package/dist/esm/timespan.js +202 -0
  104. package/dist/esm/timespan.js.map +1 -1
  105. package/dist/esm/type-code.js.map +1 -1
  106. package/dist/esm/type.js +80 -1
  107. package/dist/esm/type.js.map +1 -1
  108. package/dist/esm/types.js.map +1 -1
  109. package/dist/esm/utilities.js +183 -0
  110. package/dist/esm/utilities.js.map +1 -1
  111. package/dist/types/array.d.ts +26 -0
  112. package/dist/types/array.d.ts.map +1 -1
  113. package/dist/types/async-pool-executor.d.ts +15 -0
  114. package/dist/types/async-pool-executor.d.ts.map +1 -1
  115. package/dist/types/browser/form-utilities.d.ts +13 -0
  116. package/dist/types/browser/form-utilities.d.ts.map +1 -1
  117. package/dist/types/browser/index.d.ts.map +1 -1
  118. package/dist/types/browser/native-bridge.d.ts +6 -0
  119. package/dist/types/browser/native-bridge.d.ts.map +1 -1
  120. package/dist/types/collections/cache-dictionary.d.ts +39 -0
  121. package/dist/types/collections/cache-dictionary.d.ts.map +1 -1
  122. package/dist/types/collections/default-map.d.ts +23 -0
  123. package/dist/types/collections/default-map.d.ts.map +1 -1
  124. package/dist/types/collections/default-weak-map.d.ts +20 -1
  125. package/dist/types/collections/default-weak-map.d.ts.map +1 -1
  126. package/dist/types/collections/from.d.ts +333 -0
  127. package/dist/types/collections/from.d.ts.map +1 -0
  128. package/dist/types/collections/index.d.ts +1 -0
  129. package/dist/types/collections/index.d.ts.map +1 -1
  130. package/dist/types/draft.d.ts +68 -0
  131. package/dist/types/draft.d.ts.map +1 -1
  132. package/dist/types/index.d.ts +4 -1
  133. package/dist/types/index.d.ts.map +1 -1
  134. package/dist/types/key-value-pair.d.ts +6 -0
  135. package/dist/types/key-value-pair.d.ts.map +1 -1
  136. package/dist/types/lazy.d.ts +4 -0
  137. package/dist/types/lazy.d.ts.map +1 -1
  138. package/dist/types/math.d.ts +19 -0
  139. package/dist/types/math.d.ts.map +1 -1
  140. package/dist/types/parse.d.ts +121 -0
  141. package/dist/types/parse.d.ts.map +1 -1
  142. package/dist/types/string.d.ts +51 -0
  143. package/dist/types/string.d.ts.map +1 -1
  144. package/dist/types/timer.d.ts +7 -0
  145. package/dist/types/timer.d.ts.map +1 -1
  146. package/dist/types/timespan.d.ts +202 -0
  147. package/dist/types/timespan.d.ts.map +1 -1
  148. package/dist/types/type.d.ts +40 -0
  149. package/dist/types/type.d.ts.map +1 -1
  150. package/dist/types/utilities.d.ts +171 -0
  151. package/dist/types/utilities.d.ts.map +1 -1
  152. package/dist/web/array.js +27 -1
  153. package/dist/web/array.js.map +1 -1
  154. package/dist/web/async-pool-executor.js +15 -0
  155. package/dist/web/async-pool-executor.js.map +1 -1
  156. package/dist/web/browser/form-utilities.js +26 -0
  157. package/dist/web/browser/form-utilities.js.map +1 -1
  158. package/dist/web/browser/geo-location.js +5 -0
  159. package/dist/web/browser/geo-location.js.map +1 -1
  160. package/dist/web/browser/index.js.map +1 -1
  161. package/dist/web/browser/native-bridge.js +9 -0
  162. package/dist/web/browser/native-bridge.js.map +1 -1
  163. package/dist/web/cancellable-promise.js.map +1 -1
  164. package/dist/web/clean-url-search-params.js +1 -0
  165. package/dist/web/clean-url-search-params.js.map +1 -1
  166. package/dist/web/collections/cache-dictionary.js +39 -0
  167. package/dist/web/collections/cache-dictionary.js.map +1 -1
  168. package/dist/web/collections/default-map.js +25 -1
  169. package/dist/web/collections/default-map.js.map +1 -1
  170. package/dist/web/collections/default-weak-map.js +22 -2
  171. package/dist/web/collections/default-weak-map.js.map +1 -1
  172. package/dist/web/{from.js → collections/from.js} +279 -5
  173. package/dist/web/collections/from.js.map +1 -0
  174. package/dist/web/collections/index.js +1 -0
  175. package/dist/web/collections/index.js.map +1 -1
  176. package/dist/web/draft.js +37 -0
  177. package/dist/web/draft.js.map +1 -1
  178. package/dist/web/event-bus.js +1 -0
  179. package/dist/web/event-bus.js.map +1 -1
  180. package/dist/web/index.js +13 -1
  181. package/dist/web/index.js.map +1 -1
  182. package/dist/web/key-value-pair.js +6 -0
  183. package/dist/web/key-value-pair.js.map +1 -1
  184. package/dist/web/lazy.js +4 -0
  185. package/dist/web/lazy.js.map +1 -1
  186. package/dist/web/math.js +19 -0
  187. package/dist/web/math.js.map +1 -1
  188. package/dist/web/order.js.map +1 -1
  189. package/dist/web/parse.js +96 -0
  190. package/dist/web/parse.js.map +1 -1
  191. package/dist/web/patch.js +10 -2
  192. package/dist/web/patch.js.map +1 -1
  193. package/dist/web/stopwatch.js +10 -0
  194. package/dist/web/stopwatch.js.map +1 -1
  195. package/dist/web/string.js +62 -0
  196. package/dist/web/string.js.map +1 -1
  197. package/dist/web/timer.js +7 -0
  198. package/dist/web/timer.js.map +1 -1
  199. package/dist/web/timespan.js +202 -0
  200. package/dist/web/timespan.js.map +1 -1
  201. package/dist/web/type-code.js.map +1 -1
  202. package/dist/web/type.js +80 -1
  203. package/dist/web/type.js.map +1 -1
  204. package/dist/web/types.js.map +1 -1
  205. package/dist/web/utilities.js +183 -0
  206. package/dist/web/utilities.js.map +1 -1
  207. package/package.json +1 -1
  208. package/src/browser/index.ts +2 -2
  209. package/src/collections/cache-dictionary.ts +39 -1
  210. package/src/collections/default-map.ts +15 -9
  211. package/src/collections/default-weak-map.ts +14 -12
  212. package/src/{from.ts → collections/from.ts} +285 -15
  213. package/src/collections/index.ts +4 -3
  214. package/src/index.ts +1 -1
  215. package/src/timespan.ts +199 -20
  216. package/dist/cjs/from.js.map +0 -1
  217. package/dist/esm/from.js.map +0 -1
  218. package/dist/types/from.d.ts +0 -59
  219. package/dist/types/from.d.ts.map +0 -1
  220. package/dist/web/from.js.map +0 -1
@@ -1,9 +1,17 @@
1
+ /**
2
+ * A dictionary-like structure with caching and expiration capabilities.
3
+ * @template T - The type of the values stored in the cache.
4
+ */
1
5
  export default class CacheDictionary<T> {
2
6
  private readonly fallback: ((key: string) => T) | null;
3
7
  private readonly cache: { [key: string]: T };
4
8
  private readonly defaultDuration: number | null;
5
9
  private readonly expiration: { [key: string]: Date | undefined };
6
10
 
11
+ /**
12
+ * @param {((key: string) => T) | null} fallback - Optional function to call when a key is not found.
13
+ * @param {number | null} defaultDuration - Default duration in seconds for cache items.
14
+ */
7
15
  constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {
8
16
  this.fallback = fallback;
9
17
  this.cache = {};
@@ -11,15 +19,29 @@ export default class CacheDictionary<T> {
11
19
  this.expiration = {};
12
20
  }
13
21
 
14
-
22
+ /**
23
+ * Gets the expiration date for a specific key.
24
+ * @param {string} key - The key.
25
+ * @returns {Date | undefined} The expiration date or undefined.
26
+ */
15
27
  public getExpiration(key: string): Date | undefined {
16
28
  return this.expiration[key];
17
29
  }
18
30
 
31
+ /**
32
+ * Sets a specific expiration date for a key.
33
+ * @param {string} key - The key.
34
+ * @param {Date | undefined} expiration - The expiration date.
35
+ */
19
36
  public setExpiration(key: string, expiration: Date | undefined): void {
20
37
  this.expiration[key] = expiration;
21
38
  }
22
39
 
40
+ /**
41
+ * Sets a duration in seconds for a key's expiration.
42
+ * @param {string} key - The key.
43
+ * @param {number | null} duration - Duration in seconds. If null, expiration is removed.
44
+ */
23
45
  public setDuration(key: string, duration: number | null): void {
24
46
  if (duration === null) {
25
47
  delete this.expiration[key];
@@ -30,6 +52,12 @@ export default class CacheDictionary<T> {
30
52
  }
31
53
  }
32
54
 
55
+ /**
56
+ * Gets a value from the cache. If not found and a fallback exists, it uses the fallback.
57
+ * If the item is expired, it's removed and null is returned.
58
+ * @param {string} key - The key to retrieve.
59
+ * @returns {T | null} The value or null if not found/expired.
60
+ */
33
61
  public get(key: string): T | null {
34
62
  if (!this.cache.hasOwnProperty(key)) {
35
63
  if (this.fallback) {
@@ -48,11 +76,21 @@ export default class CacheDictionary<T> {
48
76
  return this.cache[key];
49
77
  }
50
78
 
79
+ /**
80
+ * Sets a value in the cache with an optional duration.
81
+ * @param {string} key - The key.
82
+ * @param {T} value - The value to store.
83
+ * @param {number | null} duration - Duration in seconds.
84
+ */
51
85
  public set(key: string, value: T, duration: number | null = null): void {
52
86
  this.cache[key] = value;
53
87
  this.expiration[key] = this.calculateExpiration(duration);
54
88
  }
55
89
 
90
+ /**
91
+ * Removes an item from the cache.
92
+ * @param {string} key - The key to remove.
93
+ */
56
94
  public remove(key: string): void {
57
95
  delete this.cache[key];
58
96
  delete this.expiration[key];
@@ -2,31 +2,37 @@ import type {Optional, UnaryFunction} from "../types.js";
2
2
 
3
3
  /**
4
4
  * DefaultMap<K, V>
5
- * Un Map que crea y guarda automáticamente un valor por defecto
6
- * cuando accedes a una clave inexistente con get(key).
5
+ * A Map that automatically creates and saves a default value
6
+ * when accessing a non-existent key with get(key).
7
7
  *
8
- * Ejemplo rápido:
8
+ * @template K - The type of the keys.
9
+ * @template V - The type of the values.
10
+ * @example
9
11
  * const m = new DefaultMap<string, number>(() => 0);
10
- * m.get("a") += 1; // ahora m.get("a") === 1
12
+ * m.get("a"); // 0
13
+ * m.set("a", m.get("a") + 1); // 1
11
14
  */
12
15
  export default class DefaultMap<K, V> extends Map<K, V> {
13
16
  private readonly _factoryMethod: (key: K) => V;
14
17
 
15
18
  /**
16
- * @param factoryMethod Función que produce el valor por defecto para una clave.
17
- * @param entries Entradas iniciales.
19
+ * @param {UnaryFunction<K, V>} factoryMethod - Function that produces the default value for a key.
20
+ * @param {Optional<Iterable<[K, V]>>} entries - Initial entries.
18
21
  */
19
22
  constructor(factoryMethod: UnaryFunction<K, V>, entries: Optional<Iterable<[K, V]>> = undefined) {
20
23
  super(entries);
21
24
  if (typeof factoryMethod !== "function") {
22
- throw new TypeError("DefaultMap necesita una factory (key => defaultValue).");
25
+ throw new TypeError("DefaultMap needs a factory (key => defaultValue).");
23
26
  }
24
27
  this._factoryMethod = factoryMethod;
25
28
  }
26
29
 
27
30
  /**
28
- * Obtiene el valor de la clave. Si no existe, lo crea con la factory,
29
- * lo guarda y lo devuelve.
31
+ * Gets the value for the key. If it does not exist, it creates it using the factory,
32
+ * saves it, and returns it.
33
+ *
34
+ * @param {K} key - The key to get.
35
+ * @returns {V} The value associated with the key.
30
36
  */
31
37
  override get(key: K): V {
32
38
  if (!super.has(key)) {
@@ -1,20 +1,19 @@
1
1
  import type {Optional, UnaryFunction} from "../types.js";
2
2
 
3
3
  /**
4
- * DefaultMap<K, V>
5
- * Un Map que crea y guarda automáticamente un valor por defecto
6
- * cuando accedes a una clave inexistente con get(key).
4
+ * DefaultWeakMap<K, V>
5
+ * A WeakMap that automatically creates and saves a default value
6
+ * when accessing a non-existent key with get(key).
7
7
  *
8
- * Ejemplo rápido:
9
- * const m = new DefaultMap<string, number>(() => 0);
10
- * m.get("a") += 1; // ahora m.get("a") === 1
8
+ * @template K - The type of the keys. Must be an object.
9
+ * @template V - The type of the values.
11
10
  */
12
- export default class DefaultMap<K extends object, V> extends WeakMap<K, V> {
11
+ export default class DefaultWeakMap<K extends object, V> extends WeakMap<K, V> {
13
12
  private readonly _factoryMethod: (key: K) => V;
14
13
 
15
14
  /**
16
- * @param factoryMethod Función que produce el valor por defecto para una clave.
17
- * @param entries Entradas iniciales.
15
+ * @param {UnaryFunction<K, V>} factoryMethod - Function that produces the default value for a key.
16
+ * @param {Optional<Iterable<[K, V]>>} entries - Initial entries.
18
17
  */
19
18
  constructor(factoryMethod: UnaryFunction<K, V>, entries: Optional<Iterable<[K, V]>> = undefined) {
20
19
  super();
@@ -26,14 +25,17 @@ export default class DefaultMap<K extends object, V> extends WeakMap<K, V> {
26
25
  }
27
26
 
28
27
  if (typeof factoryMethod !== "function") {
29
- throw new TypeError("DefaultMap necesita una factory (key => defaultValue).");
28
+ throw new TypeError("DefaultWeakMap needs a factory (key => defaultValue).");
30
29
  }
31
30
  this._factoryMethod = factoryMethod;
32
31
  }
33
32
 
34
33
  /**
35
- * Obtiene el valor de la clave. Si no existe, lo crea con la factory,
36
- * lo guarda y lo devuelve.
34
+ * Gets the value for the key. If it does not exist, it creates it using the factory,
35
+ * saves it, and returns it.
36
+ *
37
+ * @param {K} key - The key to get.
38
+ * @returns {V} The value associated with the key.
37
39
  */
38
40
  override get(key: K): V {
39
41
  if (!super.has(key)) {