{"id":24325691,"url":"https://github.com/zastinian/cache","last_synced_at":"2026-02-11T04:04:47.964Z","repository":{"id":272780477,"uuid":"917768263","full_name":"Zastinian/cache","owner":"Zastinian","description":"Cache using map","archived":false,"fork":false,"pushed_at":"2025-02-11T01:46:56.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-11T12:51:59.638Z","etag":null,"topics":["bun","cache","javascript","node","typescript"],"latest_commit_sha":null,"homepage":"https://docs.hedystia.com/cache/start","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Zastinian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-16T15:59:42.000Z","updated_at":"2025-02-11T01:46:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"a9d99c77-b24f-4390-ab87-8968a611b72c","html_url":"https://github.com/Zastinian/cache","commit_stats":null,"previous_names":["zastinian/cache"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zastinian%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zastinian%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zastinian%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Zastinian%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Zastinian","download_url":"https://codeload.github.com/Zastinian/cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248815453,"owners_count":21165929,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bun","cache","javascript","node","typescript"],"created_at":"2025-01-17T20:18:55.791Z","updated_at":"2026-02-11T04:04:47.938Z","avatar_url":"https://github.com/Zastinian.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cache\n\n## Overview\n\nThe Cache class is a generic key-value store that extends the native JavaScript `Map` object, providing additional utility methods for enhanced functionality. This README provides documentation for the Cache class, including method descriptions, parameters, return types, and usage examples.\n\n## Usage Examples\n\n### Creating an Instance\n\n```typescript\nconst cache = new Cache\u003cstring, number\u003e();\ncache.set('one', 1).set('two', 2);\n```\n\n### Using Utility Methods\n\n```typescript\nconst values = cache.map(val =\u003e val * 2); // [2, 4]\nconst firstValue = cache.first(); // 1\n```\n\n## Table of Contents\n\n- [Methods](#methods)\n  - [size](#size)\n  - [map](#map)\n  - [mapVal](#mapval)\n  - [first](#first)\n  - [find](#find)\n  - [filter](#filter)\n  - [filterKey](#filterkey)\n  - [last](#last)\n  - [lastKey](#lastkey)\n  - [tap](#tap)\n  - [has](#has)\n  - [array](#array)\n  - [keyArray](#keyarray)\n  - [hasAll](#hasall)\n  - [hasAny](#hasany)\n  - [some](#some)\n  - [random](#random)\n  - [remove](#remove)\n  - [removeByValue](#removebyvalue)\n  - [get](#get)\n  - [every](#every)\n  - [each](#each)\n  - [randomKey](#randomkey)\n  - [equals](#equals)\n  - [difference](#difference)\n  - [findKey](#findkey)\n  - [sort](#sort)\n  - [clear](#clear)\n  - [at](#at)\n- [Static Methods](#static-methods)\n  - [defaultCompareFunction](#defaultcomparefunction)\n\n## Methods\n\n### size\n\n- **Description**: Gets the current size of the cache.\n- **Returns**: `number` - The number of key-value pairs in the cache.\n\n### map\n\n- **Description**: Applies a function to each value in the cache and returns an array of the results.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e U` - The function to apply to each value.\n- **Returns**: `U[]` - An array of the results after applying the function.\n- **Example**:\n  ```typescript\n  const cache = new Cache\u003cstring, number\u003e();\n  cache.set('one', 1).set('two', 2);\n  const doubled = cache.map(val =\u003e val * 2); // [2, 4]\n  ```\n\n### mapVal\n\n- **Description**: Maps values using a function, returning an array of the results.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e U` - The function to apply to each value.\n- **Returns**: `U[]` - An array of the results, excluding undefined values.\n- **Example**:\n  ```typescript\n  const cache = new Cache\u003cstring, number\u003e();\n  cache.set('one', 1).set('two', 2);\n  const even = cache.mapVal(val =\u003e val % 2 === 0 ? val : undefined); // [2]\n  ```\n\n### first\n\n- **Description**: Retrieves the first value in the cache.\n- **Returns**: `V | undefined`\n\n### find\n\n- **Description**: Finds a value that satisfies the given predicate function.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e boolean` - The predicate function.\n- **Returns**: `V | undefined`\n- **Example**:\n  ```typescript\n  const found = cache.find(val =\u003e val \u003e 1); // 2\n  ```\n\n### filter\n\n- **Description**: Filters entries based on a predicate function and returns a new Cache.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e boolean` - The predicate function.\n- **Returns**: `Cache\u003cK, V\u003e`\n- **Example**:\n  ```typescript\n  const filtered = cache.filter(val =\u003e val \u003e 1); // Cache { 'two' =\u003e 2 }\n  ```\n\n### filterKey\n\n- **Description**: Filters entries based on a key predicate and returns a new Cache.\n- **Parameters**:\n  - `fn`: `(key: K) =\u003e boolean` - The predicate function for keys.\n- **Returns**: `Cache\u003cK, V\u003e`\n- **Example**:\n  ```typescript\n  const filtered = cache.filterKey(key =\u003e key === 'one'); // Cache { 'one' =\u003e 1 }\n  ```\n\n### last\n\n- **Description**: Retrieves the last value in the cache.\n- **Returns**: `V | undefined`\n\n### lastKey\n\n- **Description**: Retrieves the last key in the cache.\n- **Returns**: `K | undefined`\n\n### tap\n\n- **Description**: Executes a function with the cache and returns the cache instance.\n- **Parameters**:\n  - `fn`: `(map: Cache\u003cK, V\u003e) =\u003e void` - The function to execute.\n- **Returns**: `Cache\u003cK, V\u003e` - The cache instance.\n- **Example**:\n  ```typescript\n  const cache = new Cache\u003cstring, number\u003e();\n  cache.tap(map =\u003e map.set('one', 1)); // Cache { 'one' =\u003e 1 }\n  ```\n\n### has\n\n- **Description**: Checks if a key exists in the cache.\n- **Parameters**:\n  - `k`: `K` - The key to check.\n- **Returns**: `boolean` - True if the key exists, false otherwise.\n- **Example**:\n  ```typescript\n  const exists = cache.has('one'); // true\n  ```\n\n### array\n\n- **Description**: Returns an array of all values in the cache.\n- **Returns**: `V[]` - An array of values.\n- **Example**:\n  ```typescript\n  const values = cache.array(); // [1, 2]\n  ```\n\n### keyArray\n\n- **Description**: Returns an array of all keys in the cache.\n- **Returns**: `K[]` - An array of keys.\n- **Example**:\n  ```typescript\n  const keys = cache.keyArray(); // ['one', 'two']\n  ```\n\n### hasAll\n\n- **Description**: Checks if all provided keys exist in the cache.\n- **Parameters**:\n  - `...c`: `K[]` - The keys to check.\n- **Returns**: `boolean` - True if all keys exist, false otherwise.\n- **Example**:\n  ```typescript\n  const allExist = cache.hasAll('one', 'two'); // true\n  ```\n\n### hasAny\n\n- **Description**: Checks if any of the provided keys exist in the cache.\n- **Parameters**:\n  - `...keys`: `K[]` - The keys to check.\n- **Returns**: `boolean` - True if any key exists, false otherwise.\n- **Example**:\n  ```typescript\n  const anyExist = cache.hasAny('one', 'three'); // true\n  ```\n\n### some\n\n- **Description**: Checks if any entry satisfies the predicate function.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e boolean` - The predicate function.\n- **Returns**: `boolean` - True if any entry satisfies the predicate, false otherwise.\n- **Example**:\n  ```typescript\n  const hasEven = cache.some(val =\u003e val % 2 === 0); // true\n  ```\n\n### random\n\n- **Description**: Retrieves a random value from the cache.\n- **Returns**: `V | undefined` - A random value or undefined if the cache is empty.\n- **Example**:\n  ```typescript\n  const random = cache.random(); // 1 or 2\n  ```\n\n### remove\n\n- **Description**: Removes a key from the cache.\n- **Parameters**:\n  - `key`: `K` - The key to remove.\n- **Returns**: `boolean` - True if the key was removed, false otherwise.\n- **Example**:\n  ```typescript\n  const removed = cache.remove('one'); // true\n  ```\n\n### removeByValue\n\n- **Description**: Removes entries based on a predicate function.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e boolean` - The predicate function.\n- **Example**:\n  ```typescript\n  cache.removeByValue(val =\u003e val \u003c 2); // removes 'one'\n  ```\n\n### get\n\n- **Description**: Retrieves a value by key.\n- **Parameters**:\n  - `k`: `K` - The key to retrieve.\n- **Returns**: `V | undefined` - The value associated with the key or undefined.\n- **Example**:\n  ```typescript\n  const value = cache.get('one'); // 1\n  ```\n\n### every\n\n- **Description**: Checks if all entries satisfy the predicate function.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e boolean` - The predicate function.\n- **Returns**: `boolean` - True if all entries satisfy the predicate, false otherwise.\n- **Example**:\n  ```typescript\n  const allEven = cache.every(val =\u003e val % 2 === 0); // false\n  ```\n\n### each\n\n- **Description**: Iterates over each entry and executes a function.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e void` - The function to execute.\n- **Returns**: `Cache\u003cK, V\u003e` - The cache instance.\n- **Example**:\n  ```typescript\n  cache.each((val, key) =\u003e console.log(key, val)); // 'one 1', 'two 2'\n  ```\n\n### randomKey\n\n- **Description**: Retrieves a random key from the cache.\n- **Returns**: `K | undefined` - A random key or undefined if the cache is empty.\n- **Example**:\n  ```typescript\n  const randomKey = cache.randomKey(); // 'one' or 'two'\n  ```\n\n### equals\n\n- **Description**: Checks if another cache is equal in size and entries.\n- **Parameters**:\n  - `cache`: `Cache\u003cK, V\u003e` - The cache to compare.\n- **Returns**: `boolean` - True if caches are equal, false otherwise.\n- **Example**:\n  ```typescript\n  const isEqual = cache1.equals(cache2); // true\n  ```\n\n### difference\n\n- **Description**: Finds keys present in another cache but not in this cache.\n- **Parameters**:\n  - `cache`: `Cache\u003cK, V\u003e` - The cache to compare.\n- **Returns**: `K[] | string` - An array of missing keys or a size difference message.\n- **Example**:\n  ```typescript\n  const difference = cache1.difference(cache2); // ['three']\n  ```\n\n### findKey\n\n- **Description**: Finds a key based on a predicate function.\n- **Parameters**:\n  - `fn`: `(val: V, key: K, map: Cache\u003cK, V\u003e) =\u003e boolean` - The predicate function.\n- **Returns**: `K | undefined` - The key that satisfies the predicate or undefined.\n- **Example**:\n  ```typescript\n  const keyFound = cache.findKey((val, key) =\u003e val \u003e 1); // 'two'\n  ```\n\n### sort\n\n- **Description**: Sorts the cache based on a comparison function and updates the cache order.\n- **Parameters**:\n  - `compareFn`: `CompareFunction\u003cV\u003e` - The comparison function (default is `Cache.defaultCompareFunction`).\n- **Returns**: `Cache\u003cK, V\u003e` - The sorted cache instance.\n- **Example**:\n  ```typescript\n  cache.sort((a, b) =\u003e a - b); // Order: 'two' (1), 'three' (2), 'one' (3)\n  ```\n\n### clear\n\n- **Description**: Clears all entries from the cache.\n- **Returns**: `void`\n- **Example**:\n  ```typescript\n  cache.clear(); // Cache is empty\n  ```\n\n### at\n\n- **Description**: Retrieves a value at a specific index.\n- **Parameters**:\n  - `index`: `number` (default is 0) - The index of the value to retrieve.\n- **Returns**: `V | undefined` - The value at the specified index or undefined.\n- **Example**:\n  ```typescript\n  const secondValue = cache.at(1); // 2\n  ```\n\n## Static Methods\n\n### defaultCompareFunction\n\n- **Description**: The default comparison function for sorting.\n- **Parameters**:\n  - `a`: `V` - The first value to compare.\n  - `b`: `V` - The second value to compare.\n- **Returns**: `number` - `-1` if `a \u003c b`, `0` if `a == b`, `1` if `a \u003e b`.\n- **Example**:\n  ```typescript\n  const result = Cache.defaultCompareFunction(2, 1); // 1\n  ```\n\n## Differences from Standard Map\n\nThe Cache class inherits all methods from the standard Map but adds additional utility methods for enhanced functionality, such as `map`, `filter`, and `sort`.\n\n## License\n\n- This project is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzastinian%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzastinian%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzastinian%2Fcache/lists"}