@oozcitak/util
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -0,1 +1,2 @@ | ||
export { FixedSizeSet } from './FixedSizeSet'; | ||
export { ObjectCache } from './ObjectCache'; | ||
@@ -2,0 +3,0 @@ export { CompareCache } from './CompareCache'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var FixedSizeSet_1 = require("./FixedSizeSet"); | ||
exports.FixedSizeSet = FixedSizeSet_1.FixedSizeSet; | ||
var ObjectCache_1 = require("./ObjectCache"); | ||
@@ -4,0 +6,0 @@ exports.ObjectCache = ObjectCache_1.ObjectCache; |
/** | ||
* Represents an object cache with a size limit. | ||
* Represents a cache of objects with a size limit. | ||
*/ | ||
export declare class ObjectCache<T> { | ||
export declare class ObjectCache<TKey, TValue> { | ||
private _limit; | ||
@@ -15,7 +15,14 @@ private _items; | ||
/** | ||
* Gets an item from the cache. | ||
* | ||
* @param key - object key | ||
*/ | ||
get(key: TKey): TValue | undefined; | ||
/** | ||
* Adds a new item to the cache. | ||
* | ||
* @param item - an item | ||
* @param key - object key | ||
* @param value - object value | ||
*/ | ||
add(item: T): void; | ||
set(key: TKey, value: TValue): void; | ||
/** | ||
@@ -26,4 +33,10 @@ * Removes an item from the cache. | ||
*/ | ||
remove(item: T): void; | ||
delete(key: TKey): boolean; | ||
/** | ||
* Determines if an item is in the cache. | ||
* | ||
* @param item - an item | ||
*/ | ||
has(key: TKey): boolean; | ||
/** | ||
* Removes all items from the cache. | ||
@@ -35,15 +48,27 @@ */ | ||
*/ | ||
get length(): number; | ||
get size(): number; | ||
/** | ||
* Applies the given callback function to all elements of the cache. | ||
*/ | ||
forEach(callback: (item: T) => void): void; | ||
forEach(callback: (key: TKey, value: TValue) => void, thisArg?: any): void; | ||
/** | ||
* Iterates through the items in the cache. | ||
* Iterates through the items in the set. | ||
*/ | ||
entries(): IterableIterator<T>; | ||
keys(): IterableIterator<TKey>; | ||
/** | ||
* Iterates through the items in the cache. | ||
* Iterates through the items in the set. | ||
*/ | ||
[Symbol.iterator](): IterableIterator<T>; | ||
values(): IterableIterator<TValue>; | ||
/** | ||
* Iterates through the items in the set. | ||
*/ | ||
entries(): IterableIterator<[TKey, TValue]>; | ||
/** | ||
* Iterates through the items in the set. | ||
*/ | ||
[Symbol.iterator](): IterableIterator<[TKey, TValue]>; | ||
/** | ||
* Returns the string tag of the cache. | ||
*/ | ||
get [Symbol.toStringTag](): string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Represents an object cache with a size limit. | ||
* Represents a cache of objects with a size limit. | ||
*/ | ||
@@ -14,14 +14,23 @@ class ObjectCache { | ||
constructor(limit = 1000) { | ||
this._items = new Set(); | ||
this._items = new Map(); | ||
this._limit = limit; | ||
} | ||
/** | ||
* Gets an item from the cache. | ||
* | ||
* @param key - object key | ||
*/ | ||
get(key) { | ||
return this._items.get(key); | ||
} | ||
/** | ||
* Adds a new item to the cache. | ||
* | ||
* @param item - an item | ||
* @param key - object key | ||
* @param value - object value | ||
*/ | ||
add(item) { | ||
this._items.add(item); | ||
set(key, value) { | ||
this._items.set(key, value); | ||
if (this._items.size > this._limit) { | ||
const it = this._items.values().next(); | ||
const it = this._items.keys().next(); | ||
/* istanbul ignore else */ | ||
@@ -38,6 +47,14 @@ if (!it.done) { | ||
*/ | ||
remove(item) { | ||
this._items.delete(item); | ||
delete(key) { | ||
return this._items.delete(key); | ||
} | ||
/** | ||
* Determines if an item is in the cache. | ||
* | ||
* @param item - an item | ||
*/ | ||
has(key) { | ||
return this._items.has(key); | ||
} | ||
/** | ||
* Removes all items from the cache. | ||
@@ -51,17 +68,29 @@ */ | ||
*/ | ||
get length() { return this._items.size; } | ||
get size() { return this._items.size; } | ||
/** | ||
* Applies the given callback function to all elements of the cache. | ||
*/ | ||
forEach(callback) { | ||
this._items.forEach(callback); | ||
forEach(callback, thisArg) { | ||
this._items.forEach((v, k) => callback.call(thisArg, k, v)); | ||
} | ||
/** | ||
* Iterates through the items in the cache. | ||
* Iterates through the items in the set. | ||
*/ | ||
*keys() { | ||
yield* this._items.keys(); | ||
} | ||
/** | ||
* Iterates through the items in the set. | ||
*/ | ||
*values() { | ||
yield* this._items.values(); | ||
} | ||
/** | ||
* Iterates through the items in the set. | ||
*/ | ||
*entries() { | ||
yield* this; | ||
yield* this._items.entries(); | ||
} | ||
/** | ||
* Iterates through the items in the cache. | ||
* Iterates through the items in the set. | ||
*/ | ||
@@ -71,4 +100,10 @@ *[Symbol.iterator]() { | ||
} | ||
/** | ||
* Returns the string tag of the cache. | ||
*/ | ||
get [Symbol.toStringTag]() { | ||
return "ObjectCache"; | ||
} | ||
} | ||
exports.ObjectCache = ObjectCache; | ||
//# sourceMappingURL=ObjectCache.js.map |
{ | ||
"name": "@oozcitak/util", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "util", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60822
21
1345