Methods
Table of Contents
Section titled “Table of Contents”Methods
Section titled “Methods”- Description: Gets the current size of the cache.
- Returns:
number- The number of key-value pairs in the cache.
- Description: Applies a function to each value in the cache and returns an array of the results.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => U- The function to apply to each value.
- Returns:
U[]- An array of the results after applying the function. - Example:
const cache = new Cache<string, number>();cache.set('one', 1).set('two', 2);const doubled = cache.map(val => val * 2); // [2, 4]
mapVal
Section titled “mapVal”- Description: Maps values using a function, returning an array of the results.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => U- The function to apply to each value.
- Returns:
U[]- An array of the results, excluding undefined values. - Example:
const cache = new Cache<string, number>();cache.set('one', 1).set('two', 2);const even = cache.mapVal(val => val % 2 === 0 ? val : undefined); // [2]
- Description: Retrieves the first value in the cache.
- Returns:
V | undefined
- Description: Finds a value that satisfies the given predicate function.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => boolean- The predicate function.
- Returns:
V | undefined - Example:
const found = cache.find(val => val > 1); // 2
filter
Section titled “filter”- Description: Filters entries based on a predicate function and returns a new Cache.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => boolean- The predicate function.
- Returns:
Cache<K, V> - Example:
const filtered = cache.filter(val => val > 1); // Cache { 'two' => 2 }
filterKey
Section titled “filterKey”- Description: Filters entries based on a key predicate and returns a new Cache.
- Parameters:
fn:(key: K) => boolean- The predicate function for keys.
- Returns:
Cache<K, V> - Example:
const filtered = cache.filterKey(key => key === 'one'); // Cache { 'one' => 1 }
- Description: Retrieves the last value in the cache.
- Returns:
V | undefined
lastKey
Section titled “lastKey”- Description: Retrieves the last key in the cache.
- Returns:
K | undefined
- Description: Executes a function with the cache and returns the cache instance.
- Parameters:
fn:(map: Cache<K, V>) => void- The function to execute.
- Returns:
Cache<K, V>- The cache instance. - Example:
const cache = new Cache<string, number>();cache.tap(map => map.set('one', 1)); // Cache { 'one' => 1 }
- Description: Checks if a key exists in the cache.
- Parameters:
k:K- The key to check.
- Returns:
boolean- True if the key exists, false otherwise. - Example:
const exists = cache.has('one'); // true
- Description: Returns an array of all values in the cache.
- Returns:
V[]- An array of values. - Example:
const values = cache.array(); // [1, 2]
keyArray
Section titled “keyArray”- Description: Returns an array of all keys in the cache.
- Returns:
K[]- An array of keys. - Example:
const keys = cache.keyArray(); // ['one', 'two']
hasAll
Section titled “hasAll”- Description: Checks if all provided keys exist in the cache.
- Parameters:
...c:K[]- The keys to check.
- Returns:
boolean- True if all keys exist, false otherwise. - Example:
const allExist = cache.hasAll('one', 'two'); // true
hasAny
Section titled “hasAny”- Description: Checks if any of the provided keys exist in the cache.
- Parameters:
...keys:K[]- The keys to check.
- Returns:
boolean- True if any key exists, false otherwise. - Example:
const anyExist = cache.hasAny('one', 'three'); // true
- Description: Checks if any entry satisfies the predicate function.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => boolean- The predicate function.
- Returns:
boolean- True if any entry satisfies the predicate, false otherwise. - Example:
const hasEven = cache.some(val => val % 2 === 0); // true
random
Section titled “random”- Description: Retrieves a random value from the cache.
- Returns:
V | undefined- A random value or undefined if the cache is empty. - Example:
const random = cache.random(); // 1 or 2
remove
Section titled “remove”- Description: Removes a key from the cache.
- Parameters:
key:K- The key to remove.
- Returns:
boolean- True if the key was removed, false otherwise. - Example:
const removed = cache.remove('one'); // true
removeByValue
Section titled “removeByValue”- Description: Removes entries based on a predicate function.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => boolean- The predicate function.
- Example:
cache.removeByValue(val => val < 2); // removes 'one'
- Description: Retrieves a value by key.
- Parameters:
k:K- The key to retrieve.
- Returns:
V | undefined- The value associated with the key or undefined. - Example:
const value = cache.get('one'); // 1
- Description: Checks if all entries satisfy the predicate function.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => boolean- The predicate function.
- Returns:
boolean- True if all entries satisfy the predicate, false otherwise. - Example:
const allEven = cache.every(val => val % 2 === 0); // false
- Description: Iterates over each entry and executes a function.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => void- The function to execute.
- Returns:
Cache<K, V>- The cache instance. - Example:
cache.each((val, key) => console.log(key, val)); // 'one 1', 'two 2'
randomKey
Section titled “randomKey”- Description: Retrieves a random key from the cache.
- Returns:
K | undefined- A random key or undefined if the cache is empty. - Example:
const randomKey = cache.randomKey(); // 'one' or 'two'
equals
Section titled “equals”- Description: Checks if another cache is equal in size and entries.
- Parameters:
cache:Cache<K, V>- The cache to compare.
- Returns:
boolean- True if caches are equal, false otherwise. - Example:
const isEqual = cache1.equals(cache2); // true
difference
Section titled “difference”- Description: Finds keys present in another cache but not in this cache.
- Parameters:
cache:Cache<K, V>- The cache to compare.
- Returns:
K[] | string- An array of missing keys or a size difference message. - Example:
const difference = cache1.difference(cache2); // ['three']
findKey
Section titled “findKey”- Description: Finds a key based on a predicate function.
- Parameters:
fn:(val: V, key: K, map: Cache<K, V>) => boolean- The predicate function.
- Returns:
K | undefined- The key that satisfies the predicate or undefined. - Example:
const keyFound = cache.findKey((val, key) => val > 1); // 'two'
- Description: Sorts the cache based on a comparison function and updates the cache order.
- Parameters:
compareFn:CompareFunction<V>- The comparison function (default isCache.defaultCompareFunction).
- Returns:
Cache<K, V>- The sorted cache instance. - Example:
cache.sort((a, b) => a - b); // Order: 'two' (1), 'three' (2), 'one' (3)
- Description: Clears all entries from the cache.
- Returns:
void - Example:
cache.clear(); // Cache is empty
- Description: Retrieves a value at a specific index.
- Parameters:
index:number(default is 0) - The index of the value to retrieve.
- Returns:
V | undefined- The value at the specified index or undefined. - Example:
const secondValue = cache.at(1); // 2
Static Methods
Section titled “Static Methods”defaultCompareFunction
Section titled “defaultCompareFunction”- Description: The default comparison function for sorting.
- Parameters:
a:V- The first value to compare.b:V- The second value to compare.
- Returns:
number--1ifa < b,0ifa == b,1ifa > b. - Example:
const result = Cache.defaultCompareFunction(2, 1); // 1