@pawells/typescript-common 1.1.6 → 1.1.7
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/build/index.d.ts +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -0
- package/build/index.js.map +1 -1
- package/build/lru-cache.d.ts +90 -0
- package/build/lru-cache.d.ts.map +1 -0
- package/build/lru-cache.js +121 -0
- package/build/lru-cache.js.map +1 -0
- package/package.json +1 -1
package/build/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export * as StringUtils from './string/index.js';
|
|
|
26
26
|
export * as TimeUtils from './time/index.js';
|
|
27
27
|
export * as EnumUtils from './enum/index.js';
|
|
28
28
|
export * as FunctionUtils from './function/index.js';
|
|
29
|
+
export { LRUCache } from './lru-cache.js';
|
|
29
30
|
export { ArrayFilter, ArrayContains, Unique, ArrayIntersection, ArrayChunk, ArrayShuffle, ArrayGroupBy, ArrayDifference, ArrayFlatten, ArrayCompact, ArrayPartition, ArrayZip, ArrayRange, ArraySortBy, ArrayCountBy, ArraySample, } from './array/index.js';
|
|
30
31
|
export { CamelCase, PASCAL_CASE, KEBAB_CASE, SNAKE_CASE, SCREAMING_SNAKE_CASE, FormatString, EscapeHTML, StripHTML, Pluralize, WordCount, CountOccurrences, } from './string/index.js';
|
|
31
32
|
export { AssertObject, ObjectClone, TransformObject, ObjectEquals, ObjectFilter, FilterObject, ObjectPick, ObjectOmit, ObjectMerge, MapObject, ObjectHash, ObjectSortKeys, ObjectFromKeyValuePairs, ObjectToKeyValuePairs, ObjectGetPropertyByPath, ObjectSetPropertyByPath, ObjectInvert, ObjectFlatten, ObjectDiff, } from './object/index.js';
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,KAAK,UAAU,MAAM,k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,EACR,QAAQ,EACR,OAAO,EACP,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,KAAK,GACL,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAGN,WAAW,EACX,aAAa,EACb,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,aAAa,EACb,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,aAAa,EACb,UAAU,EACV,YAAY,EACZ,0BAA0B,EAC1B,uBAAuB,EACvB,2BAA2B,EAC3B,eAAe,EACf,YAAY,EACZ,mBAAmB,EACnB,oBAAoB,EACpB,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,EACnB,cAAc,EAEd,cAAc,EACd,SAAS,EACT,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,eAAe,EACf,YAAY,EACZ,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACnB,cAAc,EACd,WAAW,EACX,WAAW,EACX,cAAc,GACd,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EAEX,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,MAAM,EACN,oBAAoB,EACpB,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EAEpB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,oBAAoB,EAEpB,iBAAiB,EACjB,kBAAkB,EAClB,wBAAwB,EACxB,uBAAuB,GACvB,MAAM,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAG/C,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAGjD,OAAO,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AAO,EACP,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,KAAK,GACL,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAGN,WAAW,EACX,aAAa,EACb,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,aAAa,EACb,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,aAAa,EACb,UAAU,EACV,YAAY,EACZ,0BAA0B,EAC1B,uBAAuB,EACvB,2BAA2B,EAC3B,eAAe,EACf,YAAY,EACZ,mBAAmB,EACnB,oBAAoB,EACpB,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,EACnB,cAAc,EAEd,cAAc,EACd,SAAS,EACT,mBAAmB,EACnB,oBAAoB,EACpB,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,eAAe,EACf,YAAY,EACZ,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,mBAAmB,EACnB,cAAc,EACd,WAAW,EACX,WAAW,EACX,cAAc,GACd,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EAEX,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,EACnB,iBAAiB,EACjB,MAAM,EACN,oBAAoB,EACpB,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EAEpB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,oBAAoB,EAEpB,iBAAiB,EACjB,kBAAkB,EAClB,wBAAwB,EACxB,uBAAuB,GACvB,MAAM,oBAAoB,CAAC"}
|
package/build/index.js
CHANGED
|
@@ -35,6 +35,8 @@ export * as TimeUtils from './time/index.js';
|
|
|
35
35
|
export * as EnumUtils from './enum/index.js';
|
|
36
36
|
// Function utilities
|
|
37
37
|
export * as FunctionUtils from './function/index.js';
|
|
38
|
+
// LRU Cache
|
|
39
|
+
export { LRUCache } from './lru-cache.js';
|
|
38
40
|
// Direct exports for commonly used functions
|
|
39
41
|
export {
|
|
40
42
|
// Array utilities
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,kBAAkB;AAClB,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAE/C,mBAAmB;AACnB,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAEjD,oBAAoB;AACpB,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAEnD,oBAAoB;AACpB,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAC;AAEnD,mBAAmB;AACnB,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAEjD,mBAAmB;AACnB,OAAO,KAAK,WAAW,MAAM,mBAAmB,CAAC;AAEjD,iBAAiB;AACjB,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAE7C,iBAAiB;AACjB,OAAO,KAAK,SAAS,MAAM,iBAAiB,CAAC;AAE7C,qBAAqB;AACrB,OAAO,KAAK,aAAa,MAAM,qBAAqB,CAAC;AAErD,6CAA6C;AAC7C,OAAO;AACN,kBAAkB;AAClB,WAAW,EACX,aAAa,EACb,MAAM,EACN,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,UAAU,EACV,WAAW,EACX,YAAY,EACZ,WAAW,GACX,MAAM,kBAAkB,CAAC;AAE1B,OAAO;AACN,mBAAmB;AACnB,SAAS,EACT,WAAW,EACX,UAAU,EACV,UAAU,EACV,oBAAoB,EACpB,YAAY,EACZ,UAAU,EACV,SAAS,EACT,SAAS,EACT,SAAS,EACT,gBAAgB,GAChB,MAAM,mBAAmB,CAAC;AAE3B,OAAO;AACN,mBAAmB;AACnB,YAAY,EACZ,WAAW,EACX,eAAe,EACf,YA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) Cache implementation.
|
|
3
|
+
*
|
|
4
|
+
* An `LRUCache` maintains a fixed-size cache of key-value pairs.
|
|
5
|
+
* When capacity is reached, the least recently used (oldest) entry is evicted.
|
|
6
|
+
* Both `get()` and `set()` operations update the recency order.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam K - Key type
|
|
9
|
+
* @typeParam V - Value type
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const cache = new LRUCache<string, number>(2);
|
|
14
|
+
* cache.set('a', 1);
|
|
15
|
+
* cache.set('b', 2);
|
|
16
|
+
* cache.set('c', 3); // Evicts 'a' since capacity is 2
|
|
17
|
+
* console.log(cache.get('a')); // undefined
|
|
18
|
+
* console.log(cache.get('b')); // 2
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class LRUCache<K, V> {
|
|
22
|
+
private readonly _map;
|
|
23
|
+
private readonly _maxSize;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new `LRUCache` instance.
|
|
26
|
+
*
|
|
27
|
+
* @param maxSize - Maximum number of entries to cache before evicting the least recently used
|
|
28
|
+
* @throws {Error} If maxSize is less than 1
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const cache = new LRUCache<string, string>(100);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
constructor(maxSize: number);
|
|
36
|
+
/**
|
|
37
|
+
* The current number of cached entries.
|
|
38
|
+
*/
|
|
39
|
+
get size(): number;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves a value from the cache by key.
|
|
42
|
+
*
|
|
43
|
+
* Accessing a value marks it as recently used, moving it to the end of the
|
|
44
|
+
* eviction order.
|
|
45
|
+
*
|
|
46
|
+
* @param key - The key to look up
|
|
47
|
+
* @returns The cached value, or `undefined` if not found
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const cache = new LRUCache<string, number>(2);
|
|
52
|
+
* cache.set('a', 1);
|
|
53
|
+
* console.log(cache.get('a')); // 1
|
|
54
|
+
* console.log(cache.get('b')); // undefined
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
get(key: K): V | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Sets a value in the cache.
|
|
60
|
+
*
|
|
61
|
+
* If the key already exists, its value is updated and it is marked as
|
|
62
|
+
* recently used. If the cache is at capacity and a new key is added,
|
|
63
|
+
* the least recently used (first) entry is evicted.
|
|
64
|
+
*
|
|
65
|
+
* @param key - The key to cache
|
|
66
|
+
* @param value - The value to associate with the key
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const cache = new LRUCache<string, number>(2);
|
|
71
|
+
* cache.set('a', 1);
|
|
72
|
+
* cache.set('b', 2);
|
|
73
|
+
* cache.set('c', 3); // Evicts 'a' since cache is full
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
set(key: K, value: V): void;
|
|
77
|
+
/**
|
|
78
|
+
* Clears all entries from the cache.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const cache = new LRUCache<string, number>(10);
|
|
83
|
+
* cache.set('a', 1);
|
|
84
|
+
* cache.clear();
|
|
85
|
+
* console.log(cache.size); // 0
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
clear(): void;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=lru-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lru-cache.d.ts","sourceRoot":"","sources":["../src/lru-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,QAAQ,CAAC,CAAC,EAAE,CAAC;IACzB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAmB;IAExC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAElC;;;;;;;;;;OAUG;gBACS,OAAO,EAAE,MAAM;IAO3B;;OAEG;IACH,IAAW,IAAI,IAAI,MAAM,CAExB;IAED;;;;;;;;;;;;;;;;OAgBG;IACI,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAUjC;;;;;;;;;;;;;;;;;OAiBG;IACI,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAgBlC;;;;;;;;;;OAUG;IACI,KAAK,IAAI,IAAI;CAGpB"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic Least Recently Used (LRU) Cache implementation.
|
|
3
|
+
*
|
|
4
|
+
* An `LRUCache` maintains a fixed-size cache of key-value pairs.
|
|
5
|
+
* When capacity is reached, the least recently used (oldest) entry is evicted.
|
|
6
|
+
* Both `get()` and `set()` operations update the recency order.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam K - Key type
|
|
9
|
+
* @typeParam V - Value type
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const cache = new LRUCache<string, number>(2);
|
|
14
|
+
* cache.set('a', 1);
|
|
15
|
+
* cache.set('b', 2);
|
|
16
|
+
* cache.set('c', 3); // Evicts 'a' since capacity is 2
|
|
17
|
+
* console.log(cache.get('a')); // undefined
|
|
18
|
+
* console.log(cache.get('b')); // 2
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class LRUCache {
|
|
22
|
+
_map = new Map();
|
|
23
|
+
_maxSize;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new `LRUCache` instance.
|
|
26
|
+
*
|
|
27
|
+
* @param maxSize - Maximum number of entries to cache before evicting the least recently used
|
|
28
|
+
* @throws {Error} If maxSize is less than 1
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const cache = new LRUCache<string, string>(100);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
constructor(maxSize) {
|
|
36
|
+
if (maxSize < 1) {
|
|
37
|
+
throw new Error('LRUCache maxSize must be at least 1');
|
|
38
|
+
}
|
|
39
|
+
this._maxSize = maxSize;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The current number of cached entries.
|
|
43
|
+
*/
|
|
44
|
+
get size() {
|
|
45
|
+
return this._map.size;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves a value from the cache by key.
|
|
49
|
+
*
|
|
50
|
+
* Accessing a value marks it as recently used, moving it to the end of the
|
|
51
|
+
* eviction order.
|
|
52
|
+
*
|
|
53
|
+
* @param key - The key to look up
|
|
54
|
+
* @returns The cached value, or `undefined` if not found
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const cache = new LRUCache<string, number>(2);
|
|
59
|
+
* cache.set('a', 1);
|
|
60
|
+
* console.log(cache.get('a')); // 1
|
|
61
|
+
* console.log(cache.get('b')); // undefined
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
get(key) {
|
|
65
|
+
const value = this._map.get(key);
|
|
66
|
+
if (value !== undefined) {
|
|
67
|
+
// Move to end (mark as recently used)
|
|
68
|
+
this._map.delete(key);
|
|
69
|
+
this._map.set(key, value);
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Sets a value in the cache.
|
|
75
|
+
*
|
|
76
|
+
* If the key already exists, its value is updated and it is marked as
|
|
77
|
+
* recently used. If the cache is at capacity and a new key is added,
|
|
78
|
+
* the least recently used (first) entry is evicted.
|
|
79
|
+
*
|
|
80
|
+
* @param key - The key to cache
|
|
81
|
+
* @param value - The value to associate with the key
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const cache = new LRUCache<string, number>(2);
|
|
86
|
+
* cache.set('a', 1);
|
|
87
|
+
* cache.set('b', 2);
|
|
88
|
+
* cache.set('c', 3); // Evicts 'a' since cache is full
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
set(key, value) {
|
|
92
|
+
// If key already exists, delete it (will re-add at end)
|
|
93
|
+
if (this._map.has(key)) {
|
|
94
|
+
this._map.delete(key);
|
|
95
|
+
}
|
|
96
|
+
else if (this._map.size >= this._maxSize) {
|
|
97
|
+
// If at capacity and adding a new key, evict the oldest (first) entry
|
|
98
|
+
const firstKey = this._map.keys().next().value;
|
|
99
|
+
if (firstKey !== undefined) {
|
|
100
|
+
this._map.delete(firstKey);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// Add at the end (mark as recently used)
|
|
104
|
+
this._map.set(key, value);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Clears all entries from the cache.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const cache = new LRUCache<string, number>(10);
|
|
112
|
+
* cache.set('a', 1);
|
|
113
|
+
* cache.clear();
|
|
114
|
+
* console.log(cache.size); // 0
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
clear() {
|
|
118
|
+
this._map.clear();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=lru-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lru-cache.js","sourceRoot":"","sources":["../src/lru-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,QAAQ;IACH,IAAI,GAAG,IAAI,GAAG,EAAQ,CAAC;IAEvB,QAAQ,CAAS;IAElC;;;;;;;;;;OAUG;IACH,YAAY,OAAe;QAC1B,IAAI,OAAO,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QACxD,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,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/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pawells/typescript-common",
|
|
3
3
|
"displayName": "TypeScript Common Utilities",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.7",
|
|
5
5
|
"description": "Shared TypeScript utility library — array, object, string, time, and enum helpers. ESM-only, no runtime dependencies.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./build/index.js",
|