cached-factory 0.0.1

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/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ # MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ <h1 align="center">cached-factory</h1>
2
+
3
+ <p align="center">Creates and caches values under keys. 🏭</p>
4
+
5
+ <p align="center">
6
+ <a href="#contributors" target="_blank">
7
+ <!-- prettier-ignore-start -->
8
+ <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
9
+ <img alt="All Contributors: 1" src="https://img.shields.io/badge/all_contributors-1-21bb42.svg" />
10
+ <!-- ALL-CONTRIBUTORS-BADGE:END -->
11
+ <!-- prettier-ignore-end -->
12
+ </a>
13
+ <a href="https://codecov.io/gh/JoshuaKGoldberg/cached-factory" target="_blank">
14
+ <img alt="Codecov Test Coverage" src="https://codecov.io/gh/JoshuaKGoldberg/cached-factory/branch/main/graph/badge.svg"/>
15
+ </a>
16
+ <a href="https://github.com/JoshuaKGoldberg/cached-factory/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank">
17
+ <img alt="Contributor Covenant" src="https://img.shields.io/badge/code_of_conduct-enforced-21bb42" />
18
+ </a>
19
+ <a href="https://github.com/JoshuaKGoldberg/cached-factory/blob/main/LICENSE.md" target="_blank">
20
+ <img alt="License: MIT" src="https://img.shields.io/github/license/JoshuaKGoldberg/cached-factory?color=21bb42">
21
+ </a>
22
+ <img alt="Style: Prettier" src="https://img.shields.io/badge/style-prettier-21bb42.svg" />
23
+ <img alt="TypeScript: Strict" src="https://img.shields.io/badge/typescript-strict-21bb42.svg" />
24
+ </p>
25
+
26
+ ## Usage
27
+
28
+ `cached-factory` exports a `CachedFactory` class that takes in "factory" function in its constructor.
29
+ Each time a factory's `.get(key)` is called with any `key` for the first time, that factory is used to create a value under the `key`.
30
+
31
+ ```ts
32
+ const cache = new CachedFactory((key) => `Cached: ${key}!`);
33
+
34
+ // "Cached: apple!"
35
+ cache.get("apple");
36
+ ```
37
+
38
+ Values are cached so that subsequent `.get(key)` calls with the same `key` instantly return the same value.
39
+
40
+ ```ts
41
+ const cache = new CachedFactory((key) => ({ key }));
42
+
43
+ // { key: "banana" }
44
+ cache.get("banana");
45
+
46
+ // true
47
+ cache.get("banana") === cached.get("banana");
48
+ ```
49
+
50
+ ### Asynchronous Factories
51
+
52
+ `CachedFactory` does not itself handle `Promise` logic, but it doesn't have to!
53
+ Provided factory functions can themselves be `async` / return `Promise` values.
54
+
55
+ ```ts
56
+ const cache = new CachedFactory(
57
+ async (key) => await fetch(`/some/resource?key=${key}`),
58
+ );
59
+
60
+ // Type: Promise<Response>
61
+ cache.get("cherry");
62
+
63
+ // Type: Response
64
+ await cache.get("cherry");
65
+ ```
66
+
67
+ ### Other Methods
68
+
69
+ #### `clear`
70
+
71
+ Clears the cache.
72
+
73
+ ```ts
74
+ cache.clear();
75
+ ```
76
+
77
+ ### TypeScript
78
+
79
+ `CachedFactory` is written in TypeScript and ships with strong typing. 💪
80
+
81
+ > 👉 Tip: if you're working with [`noImplicitAny`](https://aka.ms/tsconfig#noImplicitAny) enabled _(which is generally a good idea)_, an inline function provided as an argument to `CachedFactory` may need an explicit type annotation for its key.
82
+ >
83
+ > ```ts
84
+ > new CachedFactory((key: string) => `Cached: ${key}!`);
85
+ > ```
86
+
87
+ ## Contributors
88
+
89
+ <!-- spellchecker: disable -->
90
+ <!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
91
+ <!-- prettier-ignore-start -->
92
+ <!-- markdownlint-disable -->
93
+ <table>
94
+ <tbody>
95
+ <tr>
96
+ <td align="center" valign="top" width="14.28%"><a href="http://www.joshuakgoldberg.com/"><img src="https://avatars.githubusercontent.com/u/3335181?v=4?s=100" width="100px;" alt="Josh Goldberg ✨"/><br /><sub><b>Josh Goldberg ✨</b></sub></a><br /><a href="https://github.com/JoshuaKGoldberg/cached-factory/commits?author=JoshuaKGoldberg" title="Code">💻</a> <a href="#content-JoshuaKGoldberg" title="Content">🖋</a> <a href="https://github.com/JoshuaKGoldberg/cached-factory/commits?author=JoshuaKGoldberg" title="Documentation">📖</a> <a href="#ideas-JoshuaKGoldberg" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-JoshuaKGoldberg" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#maintenance-JoshuaKGoldberg" title="Maintenance">🚧</a> <a href="#projectManagement-JoshuaKGoldberg" title="Project Management">📆</a> <a href="#tool-JoshuaKGoldberg" title="Tools">🔧</a></td>
97
+ </tr>
98
+ </tbody>
99
+ </table>
100
+
101
+ <!-- markdownlint-restore -->
102
+ <!-- prettier-ignore-end -->
103
+
104
+ <!-- ALL-CONTRIBUTORS-LIST:END -->
105
+ <!-- spellchecker: enable -->
106
+
107
+ <!-- You can remove this notice if you don't want it 🙂 no worries! -->
108
+
109
+ > 💙 This package is based on [@JoshuaKGoldberg](https://github.com/JoshuaKGoldberg)'s [template-typescript-node-package](https://github.com/JoshuaKGoldberg/template-typescript-node-package).
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ type Factory<Key, Value> = (key: Key) => Value;
2
+ declare class CachedFactory<Key, Value> {
3
+ #private;
4
+ constructor(factory: Factory<Key, Value>);
5
+ clear(): void;
6
+ get(key: Key): Value;
7
+ }
8
+
9
+ export { CachedFactory, Factory };
package/lib/index.js ADDED
@@ -0,0 +1,23 @@
1
+ class CachedFactory {
2
+ #cache = /* @__PURE__ */ new Map();
3
+ #getter;
4
+ constructor(factory) {
5
+ this.#getter = factory;
6
+ }
7
+ clear() {
8
+ this.#cache.clear();
9
+ }
10
+ get(key) {
11
+ const existing = this.#cache.get(key);
12
+ if (existing) {
13
+ return existing;
14
+ }
15
+ const value = this.#getter(key);
16
+ this.#cache.set(key, value);
17
+ return value;
18
+ }
19
+ }
20
+ export {
21
+ CachedFactory
22
+ };
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export type Factory<Key, Value> = (key: Key) => Value;\n\nexport class CachedFactory<Key, Value> {\n\t#cache = new Map<Key, Value>();\n\t#getter: Factory<Key, Value>;\n\n\tconstructor(factory: Factory<Key, Value>) {\n\t\tthis.#getter = factory;\n\t}\n\n\tclear() {\n\t\tthis.#cache.clear();\n\t}\n\n\tget(key: Key) {\n\t\tconst existing = this.#cache.get(key);\n\t\tif (existing) {\n\t\t\treturn existing;\n\t\t}\n\n\t\tconst value = this.#getter(key);\n\t\tthis.#cache.set(key, value);\n\t\treturn value;\n\t}\n}\n"],"mappings":"AAEO,MAAM,cAA0B;AAAA,EACtC,SAAS,oBAAI,IAAgB;AAAA,EAC7B;AAAA,EAEA,YAAY,SAA8B;AACzC,SAAK,UAAU;AAAA,EAChB;AAAA,EAEA,QAAQ;AACP,SAAK,OAAO,MAAM;AAAA,EACnB;AAAA,EAEA,IAAI,KAAU;AACb,UAAM,WAAW,KAAK,OAAO,IAAI,GAAG;AACpC,QAAI,UAAU;AACb,aAAO;AAAA,IACR;AAEA,UAAM,QAAQ,KAAK,QAAQ,GAAG;AAC9B,SAAK,OAAO,IAAI,KAAK,KAAK;AAC1B,WAAO;AAAA,EACR;AACD;","names":[]}
package/package.json ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "name": "cached-factory",
3
+ "version": "0.0.1",
4
+ "description": "Creates and caches values under keys. 🏭",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/JoshuaKGoldberg/cached-factory"
8
+ },
9
+ "license": "MIT",
10
+ "author": {
11
+ "name": "joshuakgoldberg",
12
+ "email": "npm@joshuakgoldberg.com"
13
+ },
14
+ "type": "module",
15
+ "main": "./lib/index.js",
16
+ "files": [
17
+ "lib/",
18
+ "package.json",
19
+ "LICENSE.md",
20
+ "README.md"
21
+ ],
22
+ "scripts": {
23
+ "build": "tsup",
24
+ "format": "prettier \"**/*\" --ignore-unknown",
25
+ "format:write": "pnpm format --write",
26
+ "lint": "eslint . .*js --max-warnings 0 --report-unused-disable-directives",
27
+ "lint:knip": "knip",
28
+ "lint:md": "markdownlint \"**/*.md\" \".github/**/*.md\" --rules sentences-per-line",
29
+ "lint:package": "npmPkgJsonLint .",
30
+ "lint:packages": "pnpm dedupe --check",
31
+ "lint:spelling": "cspell \"**\" \".github/**/*\"",
32
+ "prepare": "husky install",
33
+ "should-semantic-release": "should-semantic-release --verbose",
34
+ "test": "vitest",
35
+ "tsc": "tsc"
36
+ },
37
+ "lint-staged": {
38
+ "*": "prettier --ignore-unknown --write"
39
+ },
40
+ "devDependencies": {
41
+ "@types/eslint": "^8.44.2",
42
+ "@typescript-eslint/eslint-plugin": "^6.4.1",
43
+ "@typescript-eslint/parser": "^6.4.1",
44
+ "@vitest/coverage-istanbul": "^0.34.3",
45
+ "console-fail-test": "^0.2.3",
46
+ "cspell": "^7.0.1",
47
+ "eslint": "^8.48.0",
48
+ "eslint-config-prettier": "^9.0.0",
49
+ "eslint-plugin-deprecation": "^1.5.0",
50
+ "eslint-plugin-eslint-comments": "^3.2.0",
51
+ "eslint-plugin-import": "^2.28.1",
52
+ "eslint-plugin-jsdoc": "^46.5.0",
53
+ "eslint-plugin-jsonc": "^2.9.0",
54
+ "eslint-plugin-markdown": "^3.0.1",
55
+ "eslint-plugin-n": "^16.0.2",
56
+ "eslint-plugin-no-only-tests": "^3.1.0",
57
+ "eslint-plugin-perfectionist": "^1.5.1",
58
+ "eslint-plugin-regexp": "^1.15.0",
59
+ "eslint-plugin-vitest": "^0.2.8",
60
+ "eslint-plugin-yml": "^1.8.0",
61
+ "husky": "^8.0.3",
62
+ "jsonc-eslint-parser": "^2.3.0",
63
+ "knip": "^2.20.1",
64
+ "lint-staged": "^14.0.1",
65
+ "markdownlint": "^0.30.0",
66
+ "markdownlint-cli": "^0.35.0",
67
+ "npm-package-json-lint": "^7.0.0",
68
+ "npm-package-json-lint-config-default": "^6.0.0",
69
+ "prettier": "^3.0.2",
70
+ "prettier-plugin-curly": "^0.1.2",
71
+ "prettier-plugin-packagejson": "^2.4.5",
72
+ "release-it": "^16.1.5",
73
+ "sentences-per-line": "^0.2.1",
74
+ "should-semantic-release": "^0.1.1",
75
+ "tsup": "^7.2.0",
76
+ "typescript": "^5.1.6",
77
+ "vitest": "^0.34.3",
78
+ "yaml-eslint-parser": "^1.2.2"
79
+ },
80
+ "packageManager": "pnpm@8.5.0",
81
+ "engines": {
82
+ "node": ">=18"
83
+ },
84
+ "publishConfig": {
85
+ "access": "public",
86
+ "provenance": true
87
+ }
88
+ }