{"id":16423360,"url":"https://github.com/ngekoding/enum","last_synced_at":"2025-07-14T05:33:22.789Z","repository":{"id":184076951,"uuid":"657628596","full_name":"ngekoding/enum","owner":"ngekoding","description":"Utility library for working with enumerated types (enums)","archived":false,"fork":false,"pushed_at":"2024-03-18T06:19:46.000Z","size":264,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-07T14:30:41.500Z","etag":null,"topics":["enum","enum-js","enumeration","enumify","javascript","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@ngekoding/enum","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ngekoding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2023-06-23T13:28:33.000Z","updated_at":"2024-01-23T08:36:29.000Z","dependencies_parsed_at":"2024-11-10T12:16:25.223Z","dependency_job_id":null,"html_url":"https://github.com/ngekoding/enum","commit_stats":null,"previous_names":["ngekoding/enum"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngekoding%2Fenum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngekoding%2Fenum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngekoding%2Fenum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngekoding%2Fenum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ngekoding","download_url":"https://codeload.github.com/ngekoding/enum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240587517,"owners_count":19825003,"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":["enum","enum-js","enumeration","enumify","javascript","typescript"],"created_at":"2024-10-11T07:39:35.435Z","updated_at":"2025-02-25T01:40:24.532Z","avatar_url":"https://github.com/ngekoding.png","language":"TypeScript","readme":"# Enum\n\nA utility library for working with enumerated types (enums). Simplify the management of constant values by creating strongly-typed enums with custom labels and values. Provides convenience methods for enum conversion, retrieval, and equality checks.\n\nInspired by [PHP Enum](https://github.com/spatie/enum).\n\n## Installation\n\n```sh\nnpm install @ngekoding/enum\n```\n\n## Usage\n\nThis is how an enum can be defined.\n\n```js\nimport Enum from '@ngekoding/enum'\n\nclass UserLevel extends Enum {\n  static Basic = new UserLevel(1, 'Basic')\n  static Pro = new UserLevel(2, 'Pro')\n  static Premium = new UserLevel(3, 'Premium')\n}\n```\n\nWhere the first parameter is the `value` and the second parameter is the `label`.\n\n### Accessing enum values or labels\n\n```js\nconsole.log(UserLevel.Basic.value) // Output: 1\nconsole.log(UserLevel.Basic.label) // Output: Basic\n```\n\n### Creating an enum from a value\n\n```js\nconst userLevel = UserLevel.fromValue(2)\n```\n\nWhen an enum value doesn't exist, you'll get an `Error` exception. If you would prefer not catching an exception, you can use:\n\n```js\nconst userLevel = UserLevel.tryFromValue(2)\n```\n\nWhen an enum value doesn't exist in this case, `userLevel` will have value `null` and label `Unknown`.\n\n### Comparing enums\n\nEnums can be compared using the `equals` method:\n\n```js\nconsole.log(userLevel.equals(UserLevel.Pro)) // Output: true\n```\n\nYou can pass several enums to the `equals` method, it will return `true` if the current enum equals one of the given values.\n\n```js\nconsole.log(userLevel.equals(UserLevel.Pro, UserLevel.Premium)) // Output: true\n```\n\nYou can safely comparing enum even there is no enum value when using `tryFromValue`.\n\n```js\nconsole.log(UserLevel.tryFromValue(0).equals(UserLevel.Basic)) // Output: false\n```\n\n### Get an array of enum instances\n\nYou can get an array containing all the enum instances defined within the enum class. This is allows easy access to the entire set of enum values.\n\n```js\nconst values = UserLevel.values()\nconsole.log(values) // Output: [UserLevel.Basic, UserLevel.Pro, UserLevel.Premium]\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngekoding%2Fenum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fngekoding%2Fenum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngekoding%2Fenum/lists"}