├── .all-contributorsrc ├── .github ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── DEVELOPMENT.md ├── ISSUE_TEMPLATE.md ├── ISSUE_TEMPLATE │ ├── 01-bug.yml │ ├── 02-documentation.yml │ ├── 03-feature.yml │ └── 04-tooling.yml ├── PULL_REQUEST_TEMPLATE.md ├── SECURITY.md ├── actions │ ├── prepare │ │ └── action.yml │ └── transition │ │ └── action.yml ├── renovate.json └── workflows │ ├── accessibility-alt-text-bot.yml │ ├── ci.yml │ ├── contributors.yml │ ├── cta.yml │ ├── octoguide.yml │ ├── post-release.yml │ ├── pr-review-requested.yml │ └── release.yml ├── .gitignore ├── .husky ├── .gitignore └── pre-commit ├── .markdownlint.json ├── .markdownlintignore ├── .nvmrc ├── .prettierignore ├── .prettierrc.json ├── .release-it.json ├── .vscode ├── extensions.json ├── launch.json ├── settings.json └── tasks.json ├── LICENSE.md ├── README.md ├── cspell.json ├── eslint.config.js ├── knip.json ├── package.json ├── pnpm-lock.yaml ├── src ├── CachedFactory.test.ts └── index.ts ├── tsconfig.eslint.json ├── tsconfig.json ├── tsup.config.ts └── vitest.config.ts /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "badgeTemplate": "\t\"👪\" src=\"https://img.shields.io/badge/%F0%9F%91%AA_all_contributors-<%= contributors.length %>-21bb42.svg\" />", 3 | "commitType": "docs", 4 | "contributors": [ 5 | { 6 | "login": "JoshuaKGoldberg", 7 | "name": "Josh Goldberg ✨", 8 | "avatar_url": "https://avatars.githubusercontent.com/u/3335181?v=4", 9 | "profile": "http://www.joshuakgoldberg.com/", 10 | "contributions": [ 11 | "code", 12 | "content", 13 | "doc", 14 | "ideas", 15 | "infra", 16 | "maintenance", 17 | "projectManagement", 18 | "tool" 19 | ] 20 | } 21 | ], 22 | "contributorsPerLine": 7, 23 | "contributorsSortAlphabetically": true, 24 | "files": [ 25 | "README.md" 26 | ], 27 | "projectName": "cached-factory", 28 | "projectOwner": "JoshuaKGoldberg", 29 | "repoType": "github", 30 | "commitConvention": "angular" 31 | } 32 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, caste, color, religion, or sexual 10 | identity and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | - Demonstrating empathy and kindness toward other people 21 | - Being respectful of differing opinions, viewpoints, and experiences 22 | - Giving and gracefully accepting constructive feedback 23 | - Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | - Focusing on what is best not just for us as individuals, but for the overall 26 | community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | - The use of sexualized language or imagery, and sexual attention or advances of 31 | any kind 32 | - Trolling, insulting or derogatory comments, and personal or political attacks 33 | - Public or private harassment 34 | - Publishing others' private information, such as a physical or email address, 35 | without their explicit permission 36 | - Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | github@joshuakgoldberg.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series of 86 | actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or permanent 93 | ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within the 113 | community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.1, available at 119 | [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. 120 | 121 | Community Impact Guidelines were inspired by 122 | [Mozilla's code of conduct enforcement ladder][mozilla coc]. 123 | 124 | For answers to common questions about this code of conduct, see the FAQ at 125 | [https://www.contributor-covenant.org/faq][faq]. Translations are available at 126 | [https://www.contributor-covenant.org/translations][translations]. 127 | 128 | [homepage]: https://www.contributor-covenant.org 129 | [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html 130 | [mozilla coc]: https://github.com/mozilla/diversity 131 | [faq]: https://www.contributor-covenant.org/faq 132 | [translations]: https://www.contributor-covenant.org/translations 133 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thanks for your interest in contributing to `cached-factory`! 🏭 4 | 5 | > After this page, see [DEVELOPMENT.md](./DEVELOPMENT.md) for local development instructions. 6 | 7 | ## Code of Conduct 8 | 9 | This project contains a [Contributor Covenant code of conduct](./CODE_OF_CONDUCT.md) all contributors are expected to follow. 10 | 11 | ## Reporting Issues 12 | 13 | Please do [report an issue on the issue tracker](https://github.com/JoshuaKGoldberg/cached-factory/issues/new/choose) if there's any bugfix, documentation improvement, or general enhancement you'd like to see in the repository! Please fully fill out all required fields in the most appropriate issue form. 14 | 15 | ## Sending Contributions 16 | 17 | Sending your own changes as contribution is always appreciated! 18 | There are two steps involved: 19 | 20 | 1. [Finding an Issue](#finding-an-issue) 21 | 2. [Sending a Pull Request](#sending-a-pull-request) 22 | 23 | ### Finding an Issue 24 | 25 | With the exception of very small typos, all changes to this repository generally need to correspond to an [unassigned open issue marked as `status: accepting prs` on the issue tracker](https://github.com/JoshuaKGoldberg/cached-factory/issues?q=is%3Aissue+is%3Aopen+label%3A%22status%3A+accepting+prs%22+no%3Aassignee+). 26 | If this is your first time contributing, consider searching for [unassigned issues that also have the `good first issue` label](https://github.com/JoshuaKGoldberg/cached-factory/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3A%22status%3A+accepting+prs%22+no%3Aassignee+). 27 | If the issue you'd like to fix isn't found on the issue, see [Reporting Issues](#reporting-issues) for filing your own (please do!). 28 | 29 | #### Issue Claiming 30 | 31 | We don't use any kind of issue claiming system. 32 | We've found in the past that they result in accidental ["licked cookie"](https://devblogs.microsoft.com/oldnewthing/20091201-00/?p=15843) situations where contributors claim an issue but run out of time or energy trying before sending a PR. 33 | 34 | If an unassigned issue has been marked as `status: accepting prs` and an open PR does not exist, feel free to send a PR. 35 | Please don't post comments asking for permission or stating you will work on an issue. 36 | 37 | ### Sending a Pull Request 38 | 39 | Once you've identified an open issue accepting PRs that doesn't yet have a PR sent, you're free to send a pull request. 40 | Be sure to fill out the pull request template's requested information -- otherwise your PR will likely be closed. 41 | 42 | PRs are also expected to have a title that adheres to [conventional commits](https://www.conventionalcommits.org/en/v1.0.0). 43 | Only PR titles need to be in that format, not individual commits. 44 | Don't worry if you get this wrong: you can always change the PR title after sending it. 45 | Check [previously merged PRs](https://github.com/JoshuaKGoldberg/cached-factory/pulls?q=is%3Apr+is%3Amerged+-label%3Adependencies+) for reference. 46 | 47 | #### Draft PRs 48 | 49 | If you don't think your PR is ready for review, [set it as a draft](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#converting-a-pull-request-to-a-draft). 50 | Draft PRs won't be reviewed. 51 | 52 | #### Granular PRs 53 | 54 | Please keep pull requests single-purpose: in other words, don't attempt to solve multiple unrelated problems in one pull request. 55 | Send one PR per area of concern. 56 | Multi-purpose pull requests are harder and slower to review, block all changes from being merged until the whole pull request is reviewed, and are difficult to name well with semantic PR titles. 57 | 58 | #### Pull Request Reviews 59 | 60 | When a PR is not in draft, it's considered ready for review. 61 | Please don't manually `@` tag anybody to request review. 62 | A maintainer will look at it when they're next able to. 63 | 64 | PRs should have passing [GitHub status checks](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/about-status-checks) before review is requested (unless there are explicit questions asked in the PR about any failures). 65 | 66 | #### Asking Questions 67 | 68 | If you need help and/or have a question, posting a comment in the PR is a great way to do so. 69 | There's no need to tag anybody individually. 70 | One of us will drop by and help when we can. 71 | 72 | Please post comments as [line comments](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/commenting-on-a-pull-request#adding-line-comments-to-a-pull-request) when possible, so that they can be threaded. 73 | You can [resolve conversations](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/commenting-on-a-pull-request#resolving-conversations) on your own when you feel they're resolved - no need to comment explicitly and/or wait for a maintainer. 74 | 75 | #### Requested Changes 76 | 77 | After a maintainer reviews your PR, they may request changes on it. 78 | Once you've made those changes, [re-request review on GitHub](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/about-pull-request-reviews#re-requesting-a-review). 79 | 80 | Please try not to force-push commits to PRs that have already been reviewed. 81 | Doing so makes it harder to review the changes. 82 | We squash merge all commits so there's no need to try to preserve Git history within a PR branch. 83 | 84 | Once you've addressed all our feedback by making code changes and/or started a followup discussion, [re-request review](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/about-pull-request-reviews#re-requesting-a-review) from each maintainer whose feedback you addressed. 85 | 86 | Once all feedback is addressed and the PR is approved, we'll ensure the branch is up to date with `main` and merge it for you. 87 | 88 | #### Post-Merge Recognition 89 | 90 | Once your PR is merged, if you haven't yet been added to the [_Contributors_ table in the README.md](../README.md#contributors) for its [type of contribution](https://allcontributors.org/docs/en/emoji-key "Allcontributors emoji key"), you should be soon. 91 | Please do ping the maintainer who merged your PR if that doesn't happen within 24 hours - it was likely an oversight on our end! 92 | 93 | ## Emojis & Appreciation 94 | 95 | If you made it all the way to the end, bravo dear user, we love you. 96 | Please include an emoji in the bottom of your issues and PRs to signal to us that you did in fact read this file and are trying to conform to it as best as possible. 97 | 🏭 is a good starter if you're not sure which to use. 98 | -------------------------------------------------------------------------------- /.github/DEVELOPMENT.md: -------------------------------------------------------------------------------- 1 | # Development 2 | 3 | After [forking the repo from GitHub](https://help.github.com/articles/fork-a-repo) and [installing pnpm](https://pnpm.io/installation): 4 | 5 | ```shell 6 | git clone https://github.com/(your-name-here)/cached-factory 7 | cd cached-factory 8 | pnpm install 9 | ``` 10 | 11 | > This repository includes a list of suggested VS Code extensions. 12 | > It's a good idea to use [VS Code](https://code.visualstudio.com) and accept its suggestion to install them, as they'll help with development. 13 | 14 | ## Building 15 | 16 | Run [**tsup**](https://tsup.egoist.dev) locally to build source files from `src/` into output files in `lib/`: 17 | 18 | ```shell 19 | pnpm build 20 | ``` 21 | 22 | Add `--watch` to run the builder in a watch mode that continuously cleans and recreates `lib/` as you save files: 23 | 24 | ```shell 25 | pnpm build --watch 26 | ``` 27 | 28 | ## Formatting 29 | 30 | [Prettier](https://prettier.io) is used to format code. 31 | It should be applied automatically when you save files in VS Code or make a Git commit. 32 | 33 | To manually reformat all files, you can run: 34 | 35 | ```shell 36 | pnpm format --write 37 | ``` 38 | 39 | ## Linting 40 | 41 | This package includes several forms of linting to enforce consistent code quality and styling. 42 | Each should be shown in VS Code, and can be run manually on the command-line: 43 | 44 | - `pnpm lint` ([ESLint](https://eslint.org) with [typescript-eslint](https://typescript-eslint.io)): Lints JavaScript and TypeScript source files 45 | - `pnpm lint:knip` ([knip](https://github.com/webpro/knip)): Detects unused files, dependencies, and code exports 46 | - `pnpm lint:md` ([Markdownlint](https://github.com/DavidAnson/markdownlint)): Checks Markdown source files 47 | - `pnpm lint:packages` ([pnpm dedupe --check](https://pnpm.io/cli/dedupe)): Checks for unnecessarily duplicated packages in the `pnpm-lock.yml` file 48 | - `pnpm lint:spelling` ([cspell](https://cspell.org)): Spell checks across all source files 49 | 50 | Read the individual documentation for each linter to understand how it can be configured and used best. 51 | 52 | For example, ESLint can be run with `--fix` to auto-fix some lint rule complaints: 53 | 54 | ```shell 55 | pnpm run lint --fix 56 | ``` 57 | 58 | Note that you'll need to run `pnpm build` before `pnpm lint` so that lint rules which check the file system can pick up on any built files. 59 | 60 | ## Testing 61 | 62 | [Vitest](https://vitest.dev) is used for tests. 63 | You can run it locally on the command-line: 64 | 65 | ```shell 66 | pnpm run test 67 | ``` 68 | 69 | Add the `--coverage` flag to compute test coverage and place reports in the `coverage/` directory: 70 | 71 | ```shell 72 | pnpm run test --coverage 73 | ``` 74 | 75 | Note that [console-fail-test](https://github.com/JoshuaKGoldberg/console-fail-test) is enabled for all test runs. 76 | Calls to `console.log`, `console.warn`, and other console methods will cause a test to fail. 77 | 78 | ### Debugging Tests 79 | 80 | This repository includes a [VS Code launch configuration](https://code.visualstudio.com/docs/editor/debugging) for debugging unit tests. 81 | To launch it, open a test file, then run _Debug Current Test File_ from the VS Code Debug panel (or press F5). 82 | 83 | ## Type Checking 84 | 85 | You should be able to see suggestions from [TypeScript](https://typescriptlang.org) in your editor for all open files. 86 | 87 | However, it can be useful to run the TypeScript command-line (`tsc`) to type check all files in `src/`: 88 | 89 | ```shell 90 | pnpm tsc 91 | ``` 92 | 93 | Add `--watch` to keep the type checker running in a watch mode that updates the display as you save files: 94 | 95 | ```shell 96 | pnpm tsc --watch 97 | ``` 98 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ## Overview 8 | 9 | ... 10 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/01-bug.yml: -------------------------------------------------------------------------------- 1 | body: 2 | - attributes: 3 | description: If any of these required steps are not taken, we may not be able to review your issue. Help us to help you! 4 | label: Bug Report Checklist 5 | options: 6 | - label: I have tried restarting my IDE and the issue persists. 7 | required: true 8 | - label: I have pulled the latest `main` branch of the repository. 9 | required: true 10 | - label: I have [searched for related issues](https://github.com/JoshuaKGoldberg/cached-factory/issues?q=is%3Aissue) and found none that matched my issue. 11 | required: true 12 | type: checkboxes 13 | - attributes: 14 | description: What did you expect to happen? 15 | label: Expected 16 | type: textarea 17 | validations: 18 | required: true 19 | - attributes: 20 | description: What happened instead? 21 | label: Actual 22 | type: textarea 23 | validations: 24 | required: true 25 | - attributes: 26 | description: Any additional info you'd like to provide. 27 | label: Additional Info 28 | type: textarea 29 | 30 | description: Report a bug trying to run the code 31 | 32 | labels: 33 | - "type: bug" 34 | 35 | name: 🐛 Bug 36 | 37 | title: "🐛 Bug: " 38 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/02-documentation.yml: -------------------------------------------------------------------------------- 1 | body: 2 | - attributes: 3 | description: If any of these required steps are not taken, we may not be able to review your issue. Help us to help you! 4 | label: Documentation Report Checklist 5 | options: 6 | - label: I have pulled the latest `main` branch of the repository. 7 | required: true 8 | - label: I have [searched for related issues](https://github.com/JoshuaKGoldberg/cached-factory/issues?q=is%3Aissue) and found none that matched my issue. 9 | required: true 10 | type: checkboxes 11 | - attributes: 12 | description: What would you like to report? 13 | label: Overview 14 | type: textarea 15 | validations: 16 | required: true 17 | - attributes: 18 | description: Any additional info you'd like to provide. 19 | label: Additional Info 20 | type: textarea 21 | 22 | description: Report a typo or missing area of documentation 23 | 24 | labels: 25 | - "area: documentation" 26 | 27 | name: 📝 Documentation 28 | 29 | title: "📝 Documentation: " 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/03-feature.yml: -------------------------------------------------------------------------------- 1 | body: 2 | - attributes: 3 | description: If any of these required steps are not taken, we may not be able to review your issue. Help us to help you! 4 | label: Feature Request Checklist 5 | options: 6 | - label: I have pulled the latest `main` branch of the repository. 7 | required: true 8 | - label: I have [searched for related issues](https://github.com/JoshuaKGoldberg/cached-factory/issues?q=is%3Aissue) and found none that matched my issue. 9 | required: true 10 | type: checkboxes 11 | - attributes: 12 | description: What did you expect to be able to do? 13 | label: Overview 14 | type: textarea 15 | validations: 16 | required: true 17 | - attributes: 18 | description: Any additional info you'd like to provide. 19 | label: Additional Info 20 | type: textarea 21 | 22 | description: Request that a new feature be added or an existing feature improved 23 | 24 | labels: 25 | - "type: feature" 26 | 27 | name: 🚀 Feature 28 | 29 | title: "🚀 Feature: " 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/04-tooling.yml: -------------------------------------------------------------------------------- 1 | body: 2 | - attributes: 3 | description: If any of these required steps are not taken, we may not be able to review your issue. Help us to help you! 4 | label: Tooling Report Checklist 5 | options: 6 | - label: I have tried restarting my IDE and the issue persists. 7 | required: true 8 | - label: I have pulled the latest `main` branch of the repository. 9 | required: true 10 | - label: I have [searched for related issues](https://github.com/JoshuaKGoldberg/cached-factory/issues?q=is%3Aissue) and found none that matched my issue. 11 | required: true 12 | type: checkboxes 13 | - attributes: 14 | description: What did you expect to be able to do? 15 | label: Overview 16 | type: textarea 17 | validations: 18 | required: true 19 | - attributes: 20 | description: Any additional info you'd like to provide. 21 | label: Additional Info 22 | type: textarea 23 | 24 | description: Report a bug or request an enhancement in repository tooling 25 | 26 | labels: 27 | - "area: tooling" 28 | 29 | name: 🛠 Tooling 30 | 31 | title: "🛠 Tooling: " 32 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 4 | 5 | ## PR Checklist 6 | 7 | - [ ] Addresses an existing open issue: fixes #000 8 | - [ ] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/cached-factory/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) 9 | - [ ] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/cached-factory/blob/main/.github/CONTRIBUTING.md) were taken 10 | 11 | ## Overview 12 | 13 | 14 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | We take all security vulnerabilities seriously. 4 | If you have a vulnerability or other security issues to disclose: 5 | 6 | - Thank you very much, please do! 7 | - Please send them to us by emailing `github@joshuakgoldberg.com` 8 | 9 | We appreciate your efforts and responsible disclosure and will make every effort to acknowledge your contributions. 10 | -------------------------------------------------------------------------------- /.github/actions/prepare/action.yml: -------------------------------------------------------------------------------- 1 | description: Prepares the repo for a typical CI job 2 | 3 | name: Prepare 4 | 5 | runs: 6 | steps: 7 | - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4 8 | - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 9 | with: 10 | cache: pnpm 11 | node-version: 22.16.0 12 | - run: pnpm install --frozen-lockfile 13 | shell: bash 14 | using: composite 15 | -------------------------------------------------------------------------------- /.github/actions/transition/action.yml: -------------------------------------------------------------------------------- 1 | description: Runs create-typescript-app in transition mode 2 | 3 | name: Transition 4 | 5 | runs: 6 | steps: 7 | - uses: ./.github/actions/prepare 8 | - run: npx create-typescript-app 9 | shell: bash 10 | - id: auto-commit-action 11 | uses: stefanzweifel/git-auto-commit-action@b863ae1933cb653a53c021fe36dbb774e1fb9403 # v5 12 | with: 13 | commit_author: The Friendly Bingo Bot 14 | commit_message: Check in changes from re-running npx create-typescript-app 15 | commit_user_email: bot@create.bingo 16 | commit_user_name: The Friendly Bingo Bot 17 | - if: steps.auto-commit-action.outputs.changes_detected == 'true' 18 | uses: mshick/add-pr-comment@b8f338c590a895d50bcbfa6c5859251edc8952fc # v2 19 | with: 20 | issue: ${{ github.event.pull_request.number }} 21 | message: |- 22 | 🤖 Beep boop! I ran `npx create-typescript-app` and it updated some files. 23 | 24 | I went ahead and checked those changes into this PR for you. Please review the latest commit to see if you want to merge it. 25 | 26 | Cheers! 27 | — _The Friendly Bingo Bot_ 💝 28 | 29 | > ℹ️ These automatic commits keep your repository up-to-date with new versions of [create-typescript-app](https://github.com/JoshuaKGoldberg/create-typescript-app). If you want to opt out, delete your `.github/workflows/cta-transitions.yml` file. 30 | using: composite 31 | -------------------------------------------------------------------------------- /.github/renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "automerge": true, 4 | "extends": [ 5 | ":preserveSemverRanges", 6 | "config:best-practices", 7 | "replacements:all" 8 | ], 9 | "ignoreDeps": ["codecov/codecov-action"], 10 | "labels": ["dependencies"], 11 | "minimumReleaseAge": "7 days", 12 | "patch": { "enabled": false }, 13 | "postUpdateOptions": ["pnpmDedupe"] 14 | } 15 | -------------------------------------------------------------------------------- /.github/workflows/accessibility-alt-text-bot.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | accessibility_alt_text_bot: 3 | if: ${{ !endsWith(github.actor, '[bot]') }} 4 | runs-on: ubuntu-latest 5 | steps: 6 | - uses: github/accessibility-alt-text-bot@531a7430c6930a39cae5a0aab2d165284468060f # v1.7.1 7 | 8 | name: Accessibility Alt Text Bot 9 | 10 | on: 11 | issue_comment: 12 | types: 13 | - created 14 | - edited 15 | issues: 16 | types: 17 | - edited 18 | - opened 19 | pull_request: 20 | types: 21 | - edited 22 | - opened 23 | 24 | permissions: 25 | issues: write 26 | pull-requests: write 27 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | build: 3 | name: Build 4 | runs-on: ubuntu-latest 5 | steps: 6 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 7 | - uses: ./.github/actions/prepare 8 | - run: pnpm build 9 | - run: node lib/index.js 10 | lint: 11 | name: Lint 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 15 | - uses: ./.github/actions/prepare 16 | - run: pnpm lint 17 | lint_knip: 18 | name: Lint Knip 19 | runs-on: ubuntu-latest 20 | steps: 21 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 22 | - uses: ./.github/actions/prepare 23 | - run: pnpm lint:knip 24 | lint_markdown: 25 | name: Lint Markdown 26 | runs-on: ubuntu-latest 27 | steps: 28 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 29 | - uses: ./.github/actions/prepare 30 | - run: pnpm lint:md 31 | lint_packages: 32 | name: Lint Packages 33 | runs-on: ubuntu-latest 34 | steps: 35 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 36 | - uses: ./.github/actions/prepare 37 | - run: pnpm lint:packages 38 | lint_spelling: 39 | name: Lint Spelling 40 | runs-on: ubuntu-latest 41 | steps: 42 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 43 | - uses: ./.github/actions/prepare 44 | - run: pnpm lint:spelling 45 | prettier: 46 | name: Prettier 47 | runs-on: ubuntu-latest 48 | steps: 49 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 50 | - uses: ./.github/actions/prepare 51 | - run: pnpm format --list-different 52 | test: 53 | name: Test 54 | runs-on: ubuntu-latest 55 | steps: 56 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 57 | - uses: ./.github/actions/prepare 58 | - run: pnpm run test --coverage 59 | - if: always() 60 | uses: codecov/codecov-action@v3 61 | type_check: 62 | name: Type Check 63 | runs-on: ubuntu-latest 64 | steps: 65 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 66 | - uses: ./.github/actions/prepare 67 | - run: pnpm tsc 68 | 69 | name: CI 70 | 71 | on: 72 | pull_request: ~ 73 | push: 74 | branches: 75 | - main 76 | -------------------------------------------------------------------------------- /.github/workflows/contributors.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | contributors: 3 | runs-on: ubuntu-latest 4 | steps: 5 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 6 | with: 7 | fetch-depth: 0 8 | - uses: ./.github/actions/prepare 9 | - env: 10 | GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }} 11 | uses: JoshuaKGoldberg/all-contributors-auto-action@944abe4387e751b5bbb38616cb25cf4a4ca998f2 # v0.5.0 12 | 13 | name: Contributors 14 | 15 | on: 16 | push: 17 | branches: 18 | - main 19 | -------------------------------------------------------------------------------- /.github/workflows/cta.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | transition: 3 | name: Transition 4 | runs-on: ubuntu-latest 5 | steps: 6 | - id: checkout 7 | if: (github.actor == 'JoshuaKGoldberg' || github.actor == 'renovate[bot]') && startsWith(github.head_ref, 'renovate/') && contains(github.event.pull_request.title, 'create-typescript-app') 8 | uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 9 | with: 10 | fetch-depth: 0 11 | ref: ${{github.event.pull_request.head.ref}} 12 | repository: ${{github.event.pull_request.head.repo.full_name}} 13 | token: ${{ secrets.ACCESS_TOKEN }} 14 | - if: steps.checkout.outcome != 'skipped' 15 | uses: ./.github/actions/transition 16 | - if: steps.checkout.outcome == 'skipped' 17 | run: echo 'Skipping transition mode because the PR does not appear to be an automated or owner-created update to create-typescript-app.' 18 | 19 | name: CTA 20 | 21 | on: 22 | pull_request: 23 | branches: 24 | - main 25 | 26 | permissions: 27 | pull-requests: write 28 | -------------------------------------------------------------------------------- /.github/workflows/octoguide.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | octoguide: 3 | if: ${{ !endsWith(github.actor, '[bot]') }} 4 | runs-on: ubuntu-latest 5 | steps: 6 | - uses: JoshuaKGoldberg/octoguide@c715c04e9b9a30f2a12a487d40cd1049e64549d9 # 0.11.1 7 | with: 8 | config: strict 9 | github-token: ${{ secrets.GITHUB_TOKEN }} 10 | 11 | name: OctoGuide 12 | 13 | on: 14 | discussion: 15 | types: 16 | - created 17 | - edited 18 | discussion_comment: 19 | types: 20 | - created 21 | - deleted 22 | - edited 23 | issue_comment: 24 | types: 25 | - created 26 | - deleted 27 | - edited 28 | issues: 29 | types: 30 | - edited 31 | - opened 32 | pull_request_review_comment: 33 | types: 34 | - created 35 | - deleted 36 | - edited 37 | pull_request_target: 38 | types: 39 | - edited 40 | - opened 41 | 42 | permissions: 43 | discussions: write 44 | issues: write 45 | pull-requests: write 46 | -------------------------------------------------------------------------------- /.github/workflows/post-release.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | post_release: 3 | runs-on: ubuntu-latest 4 | steps: 5 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 6 | with: 7 | fetch-depth: 0 8 | - run: echo "npm_version=$(npm pkg get version | tr -d '"')" >> "$GITHUB_ENV" 9 | - uses: apexskier/github-release-commenter@3bd413ad5e1d603bfe2282f9f06f2bdcec079327 # v1 10 | with: 11 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 12 | comment-template: | 13 | :tada: This is included in version {release_link} :tada: 14 | 15 | The release is available on: 16 | 17 | * [GitHub releases](https://github.com/JoshuaKGoldberg/cached-factory/releases/tag/{release_tag}) 18 | * [npm package (@latest dist-tag)](https://www.npmjs.com/package/cached-factory/v/${{ env.npm_version }}) 19 | 20 | Cheers! 📦🚀 21 | 22 | name: Post Release 23 | 24 | on: 25 | release: 26 | types: 27 | - published 28 | 29 | permissions: 30 | issues: write 31 | pull-requests: write 32 | -------------------------------------------------------------------------------- /.github/workflows/pr-review-requested.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | pr_review_requested: 3 | runs-on: ubuntu-latest 4 | steps: 5 | - uses: actions-ecosystem/action-remove-labels@2ce5d41b4b6aa8503e285553f75ed56e0a40bae0 # v1 6 | with: 7 | labels: "status: waiting for author" 8 | - if: failure() 9 | run: | 10 | echo "Don't worry if the previous step failed." 11 | echo "See https://github.com/actions-ecosystem/action-remove-labels/issues/221." 12 | 13 | name: PR Review Requested 14 | 15 | on: 16 | pull_request_target: 17 | types: 18 | - review_requested 19 | 20 | permissions: 21 | pull-requests: write 22 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | concurrency: 2 | group: ${{ github.workflow }} 3 | 4 | jobs: 5 | release: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 9 | with: 10 | fetch-depth: 0 11 | ref: main 12 | token: ${{ secrets.ACCESS_TOKEN }} 13 | - uses: ./.github/actions/prepare 14 | - run: pnpm build 15 | - env: 16 | GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }} 17 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 18 | uses: JoshuaKGoldberg/release-it-action@dc71f396c291f62f9a17701cfc4d4a3e7c263020 # v0.3.2 19 | 20 | name: Release 21 | 22 | on: 23 | push: 24 | branches: 25 | - main 26 | 27 | permissions: 28 | contents: write 29 | id-token: write 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /coverage 2 | /lib 3 | /node_modules 4 | -------------------------------------------------------------------------------- /.husky/.gitignore: -------------------------------------------------------------------------------- 1 | _ 2 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | npx lint-staged 2 | -------------------------------------------------------------------------------- /.markdownlint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "markdownlint/style/prettier", 3 | "first-line-h1": false, 4 | "no-inline-html": false 5 | } 6 | -------------------------------------------------------------------------------- /.markdownlintignore: -------------------------------------------------------------------------------- 1 | .github/CODE_OF_CONDUCT.md 2 | CHANGELOG.md 3 | lib/ 4 | node_modules/ 5 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 22.16.0 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | /.all-contributorsrc 2 | /.husky 3 | /coverage 4 | /lib 5 | /pnpm-lock.yaml 6 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/prettierrc", 3 | "overrides": [{ "files": ".nvmrc", "options": { "parser": "yaml" } }], 4 | "plugins": [ 5 | "prettier-plugin-curly", 6 | "prettier-plugin-packagejson", 7 | "prettier-plugin-sh" 8 | ], 9 | "useTabs": true 10 | } 11 | -------------------------------------------------------------------------------- /.release-it.json: -------------------------------------------------------------------------------- 1 | { 2 | "git": { 3 | "commitMessage": "chore: release v${version}", 4 | "requireCommits": true 5 | }, 6 | "github": { "release": true, "releaseName": "v${version}" }, 7 | "npm": { "publishArgs": ["--access public", "--provenance"] }, 8 | "plugins": { 9 | "@release-it/conventional-changelog": { 10 | "infile": "CHANGELOG.md", 11 | "preset": "angular", 12 | "types": [ 13 | { "section": "Features", "type": "feat" }, 14 | { "section": "Bug Fixes", "type": "fix" }, 15 | { "section": "Performance Improvements", "type": "perf" }, 16 | { "hidden": true, "type": "build" }, 17 | { "hidden": true, "type": "chore" }, 18 | { "hidden": true, "type": "ci" }, 19 | { "hidden": true, "type": "docs" }, 20 | { "hidden": true, "type": "refactor" }, 21 | { "hidden": true, "type": "style" }, 22 | { "hidden": true, "type": "test" } 23 | ] 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "DavidAnson.vscode-markdownlint", 4 | "dbaeumer.vscode-eslint", 5 | "esbenp.prettier-vscode", 6 | "streetsidesoftware.code-spell-checker", 7 | "vitest.explorer" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "args": ["run", "${relativeFile}"], 5 | "autoAttachChildProcesses": true, 6 | "console": "integratedTerminal", 7 | "name": "Debug Current Test File", 8 | "program": "${workspaceRoot}/node_modules/vitest/vitest.mjs", 9 | "request": "launch", 10 | "skipFiles": ["/**", "**/node_modules/**"], 11 | "smartStep": true, 12 | "type": "node" 13 | } 14 | ], 15 | "version": "0.2.0" 16 | } 17 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" }, 3 | "editor.defaultFormatter": "esbenp.prettier-vscode", 4 | "editor.formatOnSave": true, 5 | "editor.rulers": [80], 6 | "eslint.probe": [ 7 | "javascript", 8 | "javascriptreact", 9 | "json", 10 | "jsonc", 11 | "markdown", 12 | "typescript", 13 | "typescriptreact", 14 | "yaml" 15 | ], 16 | "eslint.rules.customizations": [{ "rule": "*", "severity": "warn" }], 17 | "typescript.tsdk": "node_modules/typescript/lib" 18 | } 19 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "detail": "Build the project", 5 | "label": "build", 6 | "script": "build", 7 | "type": "npm" 8 | } 9 | ], 10 | "version": "2.0.0" 11 | } 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

cached-factory

2 | 3 |

4 | Creates and caches values under keys. 5 | 🏭 6 |

7 | 8 |

9 | 10 | 11 | 👪 All Contributors: 1 12 | 13 | 14 | 🤝 Code of Conduct: Kept 15 | 🧪 Coverage 16 | 📝 License: MIT 17 | 📦 npm version 18 | 💪 TypeScript: Strict 19 |

20 | 21 | ## Usage 22 | 23 | ```shell 24 | npm i cached-factory 25 | ``` 26 | 27 | ```ts 28 | import { greet } from "cached-factory"; 29 | 30 | greet("Hello, world! 🏭"); 31 | ``` 32 | 33 | ## Development 34 | 35 | See [`.github/CONTRIBUTING.md`](./.github/CONTRIBUTING.md), then [`.github/DEVELOPMENT.md`](./.github/DEVELOPMENT.md). 36 | Thanks! 🏭 37 | 38 | ## Contributors 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 |
Josh Goldberg ✨
Josh Goldberg ✨

💻 🖋 📖 🤔 🚇 🚧 📆 🔧
51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | > 💝 This package was templated with [`create-typescript-app`](https://github.com/JoshuaKGoldberg/create-typescript-app) using the [Bingo framework](https://create.bingo). 59 | -------------------------------------------------------------------------------- /cspell.json: -------------------------------------------------------------------------------- 1 | { 2 | "dictionaries": ["npm", "node", "typescript"], 3 | "ignorePaths": [ 4 | ".all-contributorsrc", 5 | ".github", 6 | "CHANGELOG.md", 7 | "coverage", 8 | "lib", 9 | "node_modules", 10 | "pnpm-lock.yaml" 11 | ], 12 | "words": [ 13 | "Codecov", 14 | "apexskier", 15 | "automerge", 16 | "commitlint", 17 | "contributorsrc", 18 | "conventionalcommits", 19 | "joshuakgoldberg", 20 | "knip", 21 | "lcov", 22 | "markdownlintignore", 23 | "npmpackagejsonlintrc", 24 | "outro", 25 | "packagejson", 26 | "tseslint", 27 | "tsup" 28 | ] 29 | } 30 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | import comments from "@eslint-community/eslint-plugin-eslint-comments/configs"; 2 | import eslint from "@eslint/js"; 3 | import vitest from "@vitest/eslint-plugin"; 4 | import jsdoc from "eslint-plugin-jsdoc"; 5 | import jsonc from "eslint-plugin-jsonc"; 6 | import markdown from "eslint-plugin-markdown"; 7 | import n from "eslint-plugin-n"; 8 | import packageJson from "eslint-plugin-package-json"; 9 | import perfectionist from "eslint-plugin-perfectionist"; 10 | import * as regexp from "eslint-plugin-regexp"; 11 | import yml from "eslint-plugin-yml"; 12 | import tseslint from "typescript-eslint"; 13 | 14 | export default tseslint.config( 15 | { 16 | ignores: ["**/*.snap", "coverage", "lib", "node_modules", "pnpm-lock.yaml"], 17 | }, 18 | { linterOptions: { reportUnusedDisableDirectives: "error" } }, 19 | eslint.configs.recommended, 20 | comments.recommended, 21 | jsdoc.configs["flat/contents-typescript-error"], 22 | jsdoc.configs["flat/logical-typescript-error"], 23 | jsdoc.configs["flat/stylistic-typescript-error"], 24 | jsonc.configs["flat/recommended-with-json"], 25 | markdown.configs.recommended, 26 | n.configs["flat/recommended"], 27 | packageJson.configs.recommended, 28 | perfectionist.configs["recommended-natural"], 29 | regexp.configs["flat/recommended"], 30 | { 31 | extends: [ 32 | tseslint.configs.strictTypeChecked, 33 | tseslint.configs.stylisticTypeChecked, 34 | ], 35 | files: ["**/*.{js,ts}"], 36 | languageOptions: { 37 | parserOptions: { 38 | projectService: { allowDefaultProject: ["*.config.*s"] }, 39 | tsconfigRootDir: import.meta.dirname, 40 | }, 41 | }, 42 | rules: { 43 | // Stylistic concerns that don't interfere with Prettier 44 | "logical-assignment-operators": [ 45 | "error", 46 | "always", 47 | { enforceForIfStatements: true }, 48 | ], 49 | "no-useless-rename": "error", 50 | "object-shorthand": "error", 51 | "operator-assignment": "error", 52 | }, 53 | settings: { 54 | perfectionist: { partitionByComment: true, type: "natural" }, 55 | vitest: { typecheck: true }, 56 | }, 57 | }, 58 | { 59 | extends: [tseslint.configs.disableTypeChecked], 60 | files: ["**/*.md/*.ts"], 61 | rules: { "n/no-missing-import": "off" }, 62 | }, 63 | { 64 | extends: [vitest.configs.recommended], 65 | files: ["**/*.test.*"], 66 | rules: { "@typescript-eslint/no-unsafe-assignment": "off" }, 67 | }, 68 | { 69 | extends: [yml.configs["flat/standard"], yml.configs["flat/prettier"]], 70 | files: ["**/*.{yml,yaml}"], 71 | rules: { 72 | "yml/file-extension": ["error", { extension: "yml" }], 73 | "yml/sort-keys": [ 74 | "error", 75 | { order: { type: "asc" }, pathPattern: "^.*$" }, 76 | ], 77 | "yml/sort-sequence-values": [ 78 | "error", 79 | { order: { type: "asc" }, pathPattern: "^.*$" }, 80 | ], 81 | }, 82 | }, 83 | ); 84 | -------------------------------------------------------------------------------- /knip.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/knip@5.46.0/schema.json", 3 | "entry": ["src/**/*.test.*", "src/index.ts"], 4 | "ignoreExportsUsedInFile": { "interface": true, "type": true }, 5 | "project": ["src/**/*.ts"] 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cached-factory", 3 | "version": "0.1.0", 4 | "description": "Creates and caches values under keys. 🏭", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+https://github.com/JoshuaKGoldberg/cached-factory.git" 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 | "LICENSE.md", 18 | "README.md", 19 | "lib/", 20 | "package.json" 21 | ], 22 | "scripts": { 23 | "build": "tsup", 24 | "format": "prettier .", 25 | "format:write": "pnpm format --write", 26 | "lint": "eslint . --max-warnings 0", 27 | "lint:knip": "knip", 28 | "lint:md": "markdownlint \"**/*.md\" \".github/**/*.md\" --rules sentences-per-line", 29 | "lint:packages": "pnpm dedupe --check", 30 | "lint:spelling": "cspell \"**\" \".github/**/*\"", 31 | "prepare": "husky", 32 | "test": "vitest", 33 | "tsc": "tsc" 34 | }, 35 | "lint-staged": { 36 | "*": "prettier --ignore-unknown --write" 37 | }, 38 | "devDependencies": { 39 | "@eslint-community/eslint-plugin-eslint-comments": "4.5.0", 40 | "@eslint/js": "9.28.0", 41 | "@release-it/conventional-changelog": "10.0.0", 42 | "@types/eslint-plugin-markdown": "2.0.2", 43 | "@types/node": "22.15.0", 44 | "@vitest/coverage-v8": "3.1.1", 45 | "@vitest/eslint-plugin": "1.2.0", 46 | "console-fail-test": "0.5.0", 47 | "create-typescript-app": "2.42.0", 48 | "cspell": "9.0.0", 49 | "eslint": "9.27.0", 50 | "eslint-plugin-jsdoc": "50.6.8", 51 | "eslint-plugin-jsonc": "2.20.0", 52 | "eslint-plugin-markdown": "5.1.0", 53 | "eslint-plugin-n": "17.16.2", 54 | "eslint-plugin-package-json": "0.31.0", 55 | "eslint-plugin-perfectionist": "4.13.0", 56 | "eslint-plugin-regexp": "2.7.0", 57 | "eslint-plugin-yml": "1.18.0", 58 | "husky": "9.1.7", 59 | "knip": "5.59.0", 60 | "lint-staged": "16.1.0", 61 | "markdownlint": "0.38.0", 62 | "markdownlint-cli": "0.45.0", 63 | "prettier": "3.5.3", 64 | "prettier-plugin-curly": "0.3.1", 65 | "prettier-plugin-packagejson": "2.5.10", 66 | "prettier-plugin-sh": "0.17.0", 67 | "release-it": "19.0.1", 68 | "sentences-per-line": "0.3.0", 69 | "tsup": "8.5.0", 70 | "typescript": "5.8.2", 71 | "typescript-eslint": "8.33.0", 72 | "vitest": "3.1.1" 73 | }, 74 | "packageManager": "pnpm@10.11.0", 75 | "engines": { 76 | "node": ">=18" 77 | }, 78 | "publishConfig": { 79 | "provenance": true 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/CachedFactory.test.ts: -------------------------------------------------------------------------------- 1 | import { describe, expect, it, vi } from "vitest"; 2 | 3 | import { CachedFactory } from "./index.js"; 4 | 5 | describe("CachedFactory", () => { 6 | it("creates a new value under a key when the value doesn't yet exist", () => { 7 | const value = "value-first"; 8 | const getter = vi.fn().mockReturnValue(value); 9 | const cachedFactory = new CachedFactory(getter); 10 | 11 | const actual = cachedFactory.get("key-first"); 12 | 13 | expect(actual).toEqual(value); 14 | }); 15 | 16 | it("reuses an existing value under a key when the value doesn't yet exist", () => { 17 | const value = "value-first"; 18 | const getter = vi.fn().mockReturnValueOnce(value).mockReturnValue("second"); 19 | const cachedFactory = new CachedFactory(getter); 20 | 21 | const actual = cachedFactory.get("key-first"); 22 | 23 | expect(actual).toEqual(value); 24 | }); 25 | 26 | it("caches a values under provided keys when a key is repeated", () => { 27 | const getter = vi.fn().mockImplementation((key: string) => key); 28 | const cachedFactory = new CachedFactory(getter); 29 | 30 | expect(cachedFactory.get("key-first")).toEqual("key-first"); 31 | expect(cachedFactory.get("key-first")).toEqual("key-first"); 32 | 33 | expect(getter.mock.calls).toEqual([["key-first"]]); 34 | }); 35 | 36 | it("caches values under provided keys when multiple keys are used", () => { 37 | const getter = vi.fn().mockImplementation((key: string) => key); 38 | const cachedFactory = new CachedFactory(getter); 39 | 40 | expect(cachedFactory.get("key-first")).toEqual("key-first"); 41 | expect(cachedFactory.get("key-second")).toEqual("key-second"); 42 | expect(cachedFactory.get("key-first")).toEqual("key-first"); 43 | expect(cachedFactory.get("key-second")).toEqual("key-second"); 44 | expect(cachedFactory.get("key-second")).toEqual("key-second"); 45 | expect(cachedFactory.get("key-first")).toEqual("key-first"); 46 | expect(cachedFactory.get("key-first")).toEqual("key-first"); 47 | 48 | expect(getter.mock.calls).toEqual([["key-first"], ["key-second"]]); 49 | }); 50 | 51 | it("creates a new value under a key when .get() is used after .clear()", () => { 52 | const secondValue = "second"; 53 | const getter = vi 54 | .fn() 55 | .mockReturnValueOnce("first") 56 | .mockReturnValueOnce(secondValue); 57 | 58 | const cachedFactory = new CachedFactory(getter); 59 | 60 | cachedFactory.get("key"); 61 | cachedFactory.clear(); 62 | 63 | const second = cachedFactory.get("key"); 64 | expect(second).toEqual(secondValue); 65 | }); 66 | 67 | it("returns entries when .entries() is called", () => { 68 | const value = "value"; 69 | const getter = vi.fn().mockReturnValueOnce(value).mockReturnValue("second"); 70 | const cachedFactory = new CachedFactory(getter); 71 | 72 | const key = "key"; 73 | cachedFactory.get(key); 74 | 75 | const actual = cachedFactory.entries(); 76 | 77 | expect(Array.from(actual)).toEqual([[key, value]]); 78 | }); 79 | }); 80 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export type Factory = (key: Key) => Value; 2 | 3 | export class CachedFactory { 4 | #cache = new Map(); 5 | #getter: Factory; 6 | 7 | constructor(factory: Factory) { 8 | this.#getter = factory; 9 | } 10 | 11 | clear() { 12 | this.#cache.clear(); 13 | } 14 | 15 | entries() { 16 | return this.#cache.entries(); 17 | } 18 | 19 | get(key: Key) { 20 | const existing = this.#cache.get(key); 21 | if (existing) { 22 | return existing; 23 | } 24 | 25 | const value = this.#getter(key); 26 | this.#cache.set(key, value); 27 | return value; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tsconfig.eslint.json: -------------------------------------------------------------------------------- 1 | { "extends": "./tsconfig.json", "include": ["."] } 2 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "declarationMap": true, 5 | "esModuleInterop": true, 6 | "module": "NodeNext", 7 | "moduleResolution": "NodeNext", 8 | "noEmit": true, 9 | "resolveJsonModule": true, 10 | "skipLibCheck": true, 11 | "strict": true, 12 | "target": "ES2022" 13 | }, 14 | "include": ["src"] 15 | } 16 | -------------------------------------------------------------------------------- /tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "tsup"; 2 | 3 | export default defineConfig({ 4 | bundle: false, 5 | clean: true, 6 | dts: true, 7 | entry: ["src/**/*.ts", "!src/**/*.test.*"], 8 | format: "esm", 9 | outDir: "lib", 10 | }); 11 | -------------------------------------------------------------------------------- /vitest.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "vitest/config"; 2 | 3 | export default defineConfig({ 4 | test: { 5 | clearMocks: true, 6 | coverage: { 7 | all: true, 8 | include: ["src"], 9 | reporter: ["html", "lcov"], 10 | }, 11 | exclude: ["lib", "node_modules"], 12 | setupFiles: ["console-fail-test/setup"], 13 | }, 14 | }); 15 | --------------------------------------------------------------------------------