{"id":13455138,"url":"https://github.com/sindresorhus/onetime","last_synced_at":"2025-05-15T20:03:11.792Z","repository":{"id":12236013,"uuid":"14847357","full_name":"sindresorhus/onetime","owner":"sindresorhus","description":"Ensure a function is only called once","archived":false,"fork":false,"pushed_at":"2023-11-05T20:42:05.000Z","size":40,"stargazers_count":162,"open_issues_count":1,"forks_count":14,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-07T09:28:16.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2013-12-01T22:19:23.000Z","updated_at":"2024-08-24T11:48:20.000Z","dependencies_parsed_at":"2022-09-05T17:00:38.721Z","dependency_job_id":null,"html_url":"https://github.com/sindresorhus/onetime","commit_stats":{"total_commits":47,"total_committers":9,"mean_commits":5.222222222222222,"dds":"0.23404255319148937","last_synced_commit":"660d06d232ab9f18fce7f47d60d4eeb7e885a35e"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fonetime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fonetime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fonetime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fonetime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/onetime/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253426194,"owners_count":21906501,"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":[],"created_at":"2024-07-31T08:01:01.686Z","updated_at":"2025-05-15T20:03:10.348Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","readme":"# onetime\n\n\u003e Ensure a function is only called once\n\nWhen called multiple times it will return the return value from the first call.\n\n*Unlike the module [once](https://github.com/isaacs/once), this one isn't naughty and extending `Function.prototype`.*\n\n## Install\n\n```sh\nnpm install onetime\n```\n\n## Usage\n\n```js\nimport onetime from 'onetime';\n\nlet index = 0;\n\nconst foo = onetime(() =\u003e ++index);\n\nfoo(); //=\u003e 1\nfoo(); //=\u003e 1\nfoo(); //=\u003e 1\n\nonetime.callCount(foo); //=\u003e 3\n```\n\n```js\nimport onetime from 'onetime';\n\nconst foo = onetime(() =\u003e {}, {throw: true});\n\nfoo();\n\nfoo();\n//=\u003e Error: Function `foo` can only be called once\n```\n\n## API\n\n### onetime(fn, options?)\n\nReturns a function that only calls `fn` once.\n\n#### fn\n\nType: `Function`\n\nThe function that should only be called once.\n\n#### options\n\nType: `object`\n\n##### throw\n\nType: `boolean`\\\nDefault: `false`\n\nThrow an error when called more than once.\n\n### onetime.callCount(fn)\n\nReturns a number representing how many times `fn` has been called.\n\nNote: It throws an error if you pass in a function that is not wrapped by `onetime`.\n\n```js\nimport onetime from 'onetime';\n\nconst foo = onetime(() =\u003e {});\n\nfoo();\nfoo();\nfoo();\n\nconsole.log(onetime.callCount(foo));\n//=\u003e 3\n```\n\n#### fn\n\nType: `Function`\n\nThe function to get call count from.\n","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://sindresorhus.com/donate"],"categories":["Packages","JavaScript","包","Miscellaneous","Number"],"sub_categories":["Miscellaneous","杂项","其他"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fonetime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fonetime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fonetime/lists"}