@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
|
@@ -1,12 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
type CleanURLSearchParamsInput = string | URLSearchParams | string[][] | Record<string, any>;
|
|
2
|
+
|
|
3
|
+
function isRecordParams(params: unknown): params is Record<string, any> {
|
|
4
|
+
return params !== null &&
|
|
5
|
+
typeof params === 'object' &&
|
|
6
|
+
!(params instanceof URLSearchParams) &&
|
|
7
|
+
typeof (params as any)[Symbol.iterator] !== 'function';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function appendCleanValue(params: URLSearchParams, key: string, value: any): void {
|
|
11
|
+
if (value === null || value === undefined) return;
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
for (const item of value) {
|
|
15
|
+
appendCleanValue(params, key, item);
|
|
16
|
+
}
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
params.append(key, String(value));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default class CleanURLSearchParams extends URLSearchParams {
|
|
24
|
+
constructor(params?: CleanURLSearchParamsInput) {
|
|
25
|
+
if (!isRecordParams(params)) {
|
|
26
|
+
super(params as string | URLSearchParams | string[][] | undefined);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
super();
|
|
31
|
+
|
|
32
|
+
// Filtrar `null` y `undefined` antes de añadirlos a URLSearchParams
|
|
33
|
+
for (const [key, value] of Object.entries(params)) {
|
|
34
|
+
appendCleanValue(this, key, value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
12
37
|
}
|
|
@@ -1,116 +1,116 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A dictionary-like structure with caching and expiration capabilities.
|
|
3
|
-
* @template T - The type of the values stored in the cache.
|
|
4
|
-
*/
|
|
5
|
-
export default class CacheDictionary<T> {
|
|
6
|
-
private readonly fallback: ((key: string) => T) | null;
|
|
7
|
-
private readonly cache: { [key: string]: T };
|
|
8
|
-
private readonly defaultDuration: number | null;
|
|
9
|
-
private readonly expiration: { [key: string]: Date | undefined };
|
|
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
|
-
*/
|
|
15
|
-
constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {
|
|
16
|
-
this.fallback = fallback;
|
|
17
|
-
this.cache =
|
|
18
|
-
this.defaultDuration = defaultDuration;
|
|
19
|
-
this.expiration =
|
|
20
|
-
}
|
|
21
|
-
|
|
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
|
-
*/
|
|
27
|
-
public getExpiration(key: string): Date | undefined {
|
|
28
|
-
return this.expiration[key];
|
|
29
|
-
}
|
|
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
|
-
*/
|
|
36
|
-
public setExpiration(key: string, expiration: Date | undefined): void {
|
|
37
|
-
this.expiration[key] = expiration;
|
|
38
|
-
}
|
|
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
|
-
*/
|
|
45
|
-
public setDuration(key: string, duration: number | null): void {
|
|
46
|
-
if (duration === null) {
|
|
47
|
-
delete this.expiration[key];
|
|
48
|
-
} else {
|
|
49
|
-
const expiration = new Date();
|
|
50
|
-
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
51
|
-
this.expiration[key] = expiration;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
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
|
-
*/
|
|
61
|
-
public get(key: string): T | null {
|
|
62
|
-
if (!
|
|
63
|
-
if (this.fallback) {
|
|
64
|
-
this.cache[key] = this.fallback(key);
|
|
65
|
-
this.expiration[key] = this.calculateExpiration(this.defaultDuration);
|
|
66
|
-
} else {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (this.isExpired(key)) {
|
|
72
|
-
this.remove(key);
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return this.cache[key];
|
|
77
|
-
}
|
|
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
|
-
*/
|
|
85
|
-
public set(key: string, value: T, duration: number | null = null): void {
|
|
86
|
-
this.cache[key] = value;
|
|
87
|
-
this.expiration[key] = this.calculateExpiration(duration);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Removes an item from the cache.
|
|
92
|
-
* @param {string} key - The key to remove.
|
|
93
|
-
*/
|
|
94
|
-
public remove(key: string): void {
|
|
95
|
-
delete this.cache[key];
|
|
96
|
-
delete this.expiration[key];
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private isExpired(key: string): boolean {
|
|
100
|
-
const expiration = this.expiration[key];
|
|
101
|
-
if (expiration instanceof Date) {
|
|
102
|
-
return expiration < new Date();
|
|
103
|
-
}
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
private calculateExpiration(duration: number | null): Date | undefined {
|
|
108
|
-
if (duration === null) {
|
|
109
|
-
return undefined;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const expiration = new Date();
|
|
113
|
-
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
114
|
-
return expiration;
|
|
115
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* A dictionary-like structure with caching and expiration capabilities.
|
|
3
|
+
* @template T - The type of the values stored in the cache.
|
|
4
|
+
*/
|
|
5
|
+
export default class CacheDictionary<T> {
|
|
6
|
+
private readonly fallback: ((key: string) => T) | null;
|
|
7
|
+
private readonly cache: { [key: string]: T };
|
|
8
|
+
private readonly defaultDuration: number | null;
|
|
9
|
+
private readonly expiration: { [key: string]: Date | undefined };
|
|
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
|
+
*/
|
|
15
|
+
constructor(fallback: ((key: string) => T) | null = null, defaultDuration: number | null = null) {
|
|
16
|
+
this.fallback = fallback;
|
|
17
|
+
this.cache = Object.create(null);
|
|
18
|
+
this.defaultDuration = defaultDuration;
|
|
19
|
+
this.expiration = Object.create(null);
|
|
20
|
+
}
|
|
21
|
+
|
|
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
|
+
*/
|
|
27
|
+
public getExpiration(key: string): Date | undefined {
|
|
28
|
+
return this.expiration[key];
|
|
29
|
+
}
|
|
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
|
+
*/
|
|
36
|
+
public setExpiration(key: string, expiration: Date | undefined): void {
|
|
37
|
+
this.expiration[key] = expiration;
|
|
38
|
+
}
|
|
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
|
+
*/
|
|
45
|
+
public setDuration(key: string, duration: number | null): void {
|
|
46
|
+
if (duration === null) {
|
|
47
|
+
delete this.expiration[key];
|
|
48
|
+
} else {
|
|
49
|
+
const expiration = new Date();
|
|
50
|
+
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
51
|
+
this.expiration[key] = expiration;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
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
|
+
*/
|
|
61
|
+
public get(key: string): T | null {
|
|
62
|
+
if (!Object.prototype.hasOwnProperty.call(this.cache, key)) {
|
|
63
|
+
if (this.fallback) {
|
|
64
|
+
this.cache[key] = this.fallback(key);
|
|
65
|
+
this.expiration[key] = this.calculateExpiration(this.defaultDuration);
|
|
66
|
+
} else {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (this.isExpired(key)) {
|
|
72
|
+
this.remove(key);
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return this.cache[key];
|
|
77
|
+
}
|
|
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
|
+
*/
|
|
85
|
+
public set(key: string, value: T, duration: number | null = null): void {
|
|
86
|
+
this.cache[key] = value;
|
|
87
|
+
this.expiration[key] = this.calculateExpiration(duration);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Removes an item from the cache.
|
|
92
|
+
* @param {string} key - The key to remove.
|
|
93
|
+
*/
|
|
94
|
+
public remove(key: string): void {
|
|
95
|
+
delete this.cache[key];
|
|
96
|
+
delete this.expiration[key];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private isExpired(key: string): boolean {
|
|
100
|
+
const expiration = this.expiration[key];
|
|
101
|
+
if (expiration instanceof Date) {
|
|
102
|
+
return expiration < new Date();
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private calculateExpiration(duration: number | null): Date | undefined {
|
|
108
|
+
if (duration === null) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const expiration = new Date();
|
|
113
|
+
expiration.setSeconds(expiration.getSeconds() + duration);
|
|
114
|
+
return expiration;
|
|
115
|
+
}
|
|
116
116
|
}
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import type {Optional, UnaryFunction} from "../types.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* DefaultMap<K, V>
|
|
5
|
-
* A Map that automatically creates and saves a default value
|
|
6
|
-
* when accessing a non-existent key with get(key).
|
|
7
|
-
*
|
|
8
|
-
* @template K - The type of the keys.
|
|
9
|
-
* @template V - The type of the values.
|
|
10
|
-
* @example
|
|
11
|
-
* const m = new DefaultMap<string, number>(() => 0);
|
|
12
|
-
* m.get("a"); // 0
|
|
13
|
-
* m.set("a", m.get("a") + 1); // 1
|
|
14
|
-
*/
|
|
15
|
-
export default class DefaultMap<K, V> extends Map<K, V> {
|
|
16
|
-
private readonly _factoryMethod: (key: K) => V;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @param {UnaryFunction<K, V>} factoryMethod - Function that produces the default value for a key.
|
|
20
|
-
* @param {Optional<Iterable<[K, V]>>} entries - Initial entries.
|
|
21
|
-
*/
|
|
22
|
-
constructor(factoryMethod: UnaryFunction<K, V>, entries: Optional<Iterable<[K, V]>> = undefined) {
|
|
23
|
-
super(entries);
|
|
24
|
-
if (typeof factoryMethod !== "function") {
|
|
25
|
-
throw new TypeError("DefaultMap needs a factory (key => defaultValue).");
|
|
26
|
-
}
|
|
27
|
-
this._factoryMethod = factoryMethod;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
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.
|
|
36
|
-
*/
|
|
37
|
-
override get(key: K): V {
|
|
38
|
-
if (!super.has(key)) {
|
|
39
|
-
const value = this._factoryMethod(key);
|
|
40
|
-
super.set(key, value);
|
|
41
|
-
return value;
|
|
42
|
-
}
|
|
43
|
-
// ¡super.get puede devolver undefined si no existe! Pero ya comprobamos has(key)
|
|
44
|
-
return super.get(key)!;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
1
|
+
import type {Optional, UnaryFunction} from "../types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* DefaultMap<K, V>
|
|
5
|
+
* A Map that automatically creates and saves a default value
|
|
6
|
+
* when accessing a non-existent key with get(key).
|
|
7
|
+
*
|
|
8
|
+
* @template K - The type of the keys.
|
|
9
|
+
* @template V - The type of the values.
|
|
10
|
+
* @example
|
|
11
|
+
* const m = new DefaultMap<string, number>(() => 0);
|
|
12
|
+
* m.get("a"); // 0
|
|
13
|
+
* m.set("a", m.get("a") + 1); // 1
|
|
14
|
+
*/
|
|
15
|
+
export default class DefaultMap<K, V> extends Map<K, V> {
|
|
16
|
+
private readonly _factoryMethod: (key: K) => V;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {UnaryFunction<K, V>} factoryMethod - Function that produces the default value for a key.
|
|
20
|
+
* @param {Optional<Iterable<[K, V]>>} entries - Initial entries.
|
|
21
|
+
*/
|
|
22
|
+
constructor(factoryMethod: UnaryFunction<K, V>, entries: Optional<Iterable<[K, V]>> = undefined) {
|
|
23
|
+
super(entries);
|
|
24
|
+
if (typeof factoryMethod !== "function") {
|
|
25
|
+
throw new TypeError("DefaultMap needs a factory (key => defaultValue).");
|
|
26
|
+
}
|
|
27
|
+
this._factoryMethod = factoryMethod;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
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.
|
|
36
|
+
*/
|
|
37
|
+
override get(key: K): V {
|
|
38
|
+
if (!super.has(key)) {
|
|
39
|
+
const value = this._factoryMethod(key);
|
|
40
|
+
super.set(key, value);
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
// ¡super.get puede devolver undefined si no existe! Pero ya comprobamos has(key)
|
|
44
|
+
return super.get(key)!;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
}
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import type {Optional, UnaryFunction} from "../types.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
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
|
-
*
|
|
8
|
-
* @template K - The type of the keys. Must be an object.
|
|
9
|
-
* @template V - The type of the values.
|
|
10
|
-
*/
|
|
11
|
-
export default class DefaultWeakMap<K extends object, V> extends WeakMap<K, V> {
|
|
12
|
-
private readonly _factoryMethod: (key: K) => V;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @param {UnaryFunction<K, V>} factoryMethod - Function that produces the default value for a key.
|
|
16
|
-
* @param {Optional<Iterable<[K, V]>>} entries - Initial entries.
|
|
17
|
-
*/
|
|
18
|
-
constructor(factoryMethod: UnaryFunction<K, V>, entries: Optional<Iterable<[K, V]>> = undefined) {
|
|
19
|
-
super();
|
|
20
|
-
|
|
21
|
-
if (entries != null) {
|
|
22
|
-
for (const [key, value] of entries ?? []) {
|
|
23
|
-
this.set(key, value);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (typeof factoryMethod !== "function") {
|
|
28
|
-
throw new TypeError("DefaultWeakMap needs a factory (key => defaultValue).");
|
|
29
|
-
}
|
|
30
|
-
this._factoryMethod = factoryMethod;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
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.
|
|
39
|
-
*/
|
|
40
|
-
override get(key: K): V {
|
|
41
|
-
if (!super.has(key)) {
|
|
42
|
-
const value = this._factoryMethod(key);
|
|
43
|
-
super.set(key, value);
|
|
44
|
-
return value;
|
|
45
|
-
}
|
|
46
|
-
// ¡super.get puede devolver undefined si no existe! Pero ya comprobamos has(key)
|
|
47
|
-
return super.get(key)!;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
1
|
+
import type {Optional, UnaryFunction} from "../types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
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
|
+
*
|
|
8
|
+
* @template K - The type of the keys. Must be an object.
|
|
9
|
+
* @template V - The type of the values.
|
|
10
|
+
*/
|
|
11
|
+
export default class DefaultWeakMap<K extends object, V> extends WeakMap<K, V> {
|
|
12
|
+
private readonly _factoryMethod: (key: K) => V;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {UnaryFunction<K, V>} factoryMethod - Function that produces the default value for a key.
|
|
16
|
+
* @param {Optional<Iterable<[K, V]>>} entries - Initial entries.
|
|
17
|
+
*/
|
|
18
|
+
constructor(factoryMethod: UnaryFunction<K, V>, entries: Optional<Iterable<[K, V]>> = undefined) {
|
|
19
|
+
super();
|
|
20
|
+
|
|
21
|
+
if (entries != null) {
|
|
22
|
+
for (const [key, value] of entries ?? []) {
|
|
23
|
+
this.set(key, value);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (typeof factoryMethod !== "function") {
|
|
28
|
+
throw new TypeError("DefaultWeakMap needs a factory (key => defaultValue).");
|
|
29
|
+
}
|
|
30
|
+
this._factoryMethod = factoryMethod;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
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.
|
|
39
|
+
*/
|
|
40
|
+
override get(key: K): V {
|
|
41
|
+
if (!super.has(key)) {
|
|
42
|
+
const value = this._factoryMethod(key);
|
|
43
|
+
super.set(key, value);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
// ¡super.get puede devolver undefined si no existe! Pero ya comprobamos has(key)
|
|
47
|
+
return super.get(key)!;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
}
|