{"id":15651007,"url":"https://github.com/arkon/ts-guard-decorator","last_synced_at":"2025-09-21T04:31:18.964Z","repository":{"id":57380768,"uuid":"87979197","full_name":"arkon/ts-guard-decorator","owner":"arkon","description":"TypeScript decorator for running a check before running a method.","archived":false,"fork":false,"pushed_at":"2017-04-13T17:51:24.000Z","size":12,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T20:55:31.531Z","etag":null,"topics":["decorator","typescript","typescript-decorators"],"latest_commit_sha":null,"homepage":null,"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/arkon.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}},"created_at":"2017-04-11T21:03:48.000Z","updated_at":"2024-05-18T17:01:44.000Z","dependencies_parsed_at":"2022-09-06T03:32:15.465Z","dependency_job_id":null,"html_url":"https://github.com/arkon/ts-guard-decorator","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkon%2Fts-guard-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkon%2Fts-guard-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkon%2Fts-guard-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arkon%2Fts-guard-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arkon","download_url":"https://codeload.github.com/arkon/ts-guard-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233710787,"owners_count":18717950,"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":["decorator","typescript","typescript-decorators"],"created_at":"2024-10-03T12:36:35.917Z","updated_at":"2025-09-21T04:31:13.644Z","avatar_url":"https://github.com/arkon.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ts-guard-decorator 🛡\n\n\u003e Decorator for running a check before running a method.\n\n[![NPM](https://nodei.co/npm/ts-guard-decorator.png?compact=true)](https://nodei.co/npm/ts-guard-decorator)\n\n\n## Installation\n\n```shell\nnpm install --save ts-guard-decorator\n```\n\n\n## Usage\n\nDecorators are supported in [TypeScript](https://www.typescriptlang.org/docs/handbook/decorators.html) or with [Babel](https://babeljs.io/docs/plugins/transform-decorators/).\n\n```js\nimport guard from 'ts-guard-decorator';\n\nclass MyClass {\n  // Don't run `myFunc` if `window` doesn't exist\n  @guard(typeof window !== 'undefined')\n  myFunc() {\n    // ...\n  }\n}\n```\n\nThis is equivalent to writing:\n\n```js\nclass MyClass {\n  myFunc() {\n    if (typeof window === 'undefined') {\n      return;\n    }\n    // ...\n  }\n}\n```\n\n\n### Arguments\n\nThe guard accepts 2 arguments:\n1. A boolean expression (i.e. something that evaluates to `true` or `false`) indicating whether the method should run.\n2. A optional return value if the method should _not_ run.\n\n```js\nfunction testGuardFunc(arg1, arg2) {\n  return arg1 === arg2;\n}\n\nclass TestClass {\n  @guard(true)\n  guardTrue() {\n    return true;\n  }  //=\u003e true\n\n  @guard(false)\n  guardFalse() {\n    return true;\n  }  //=\u003e undefined\n\n  @guard(true, 'hello')\n  guardTrueRetVal() {\n    return true;\n  }  //=\u003e true\n\n  @guard(false, 'hello')\n  guardFalseRetVal() {\n    return true;\n  }  //=\u003e \"hello\"\n\n  @guard(testGuardFunc(1, 1), 'hello')\n  guardTrueFunc() {\n    return true;\n  }  //=\u003e true\n\n  @guard(testGuardFunc(1, 2), 'hello')\n  guardFalseFunc() {\n    return true;\n  }  //=\u003e \"hello\"\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farkon%2Fts-guard-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farkon%2Fts-guard-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farkon%2Fts-guard-decorator/lists"}