{"id":30587439,"url":"https://github.com/inhibitor1217/extended-enum","last_synced_at":"2025-08-29T12:39:01.355Z","repository":{"id":37933651,"uuid":"433828646","full_name":"inhibitor1217/extended-enum","owner":"inhibitor1217","description":"An extension for TypeScript enums to grant object-oriented powers","archived":false,"fork":false,"pushed_at":"2025-01-25T12:51:47.000Z","size":1883,"stargazers_count":21,"open_issues_count":8,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-25T13:29:28.427Z","etag":null,"topics":["enum","extend","extended","javascript","method","typescript"],"latest_commit_sha":null,"homepage":"https://inhibitor1217.github.io/extended-enum/","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/inhibitor1217.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-01T13:01:01.000Z","updated_at":"2023-10-21T04:07:19.000Z","dependencies_parsed_at":"2025-01-25T13:27:10.894Z","dependency_job_id":"8b7e9a86-e3f0-4cb2-b11d-018d24093711","html_url":"https://github.com/inhibitor1217/extended-enum","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/inhibitor1217/extended-enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhibitor1217%2Fextended-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhibitor1217%2Fextended-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhibitor1217%2Fextended-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhibitor1217%2Fextended-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inhibitor1217","download_url":"https://codeload.github.com/inhibitor1217/extended-enum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inhibitor1217%2Fextended-enum/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272687372,"owners_count":24976556,"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","status":"online","status_checked_at":"2025-08-29T02:00:10.610Z","response_time":87,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","extend","extended","javascript","method","typescript"],"created_at":"2025-08-29T12:39:00.613Z","updated_at":"2025-08-29T12:39:01.346Z","avatar_url":"https://github.com/inhibitor1217.png","language":"TypeScript","readme":"\u003ca href=\"https://inhibitor1217.github.io/extended-enum\"\u003e\n  \u003ch1 align=\"center\"\u003eextended-enum\u003c/h1\u003e\n\u003c/a\u003e\n\u003ch4 align=\"center\"\u003eAn extension for TypeScript enums to grant object-oriented powers\u003c/h4\u003e\n\n## Installation\n\nInstall via `npm` or `yarn`:\n\n```sh\n# NPM\nnpm i extended-enum@latest\n\n# Yarn\nyarn add extended-enum@latest\n```\n\n### Peer dependencies\n\nThis project requires `typescript@4.1.2` or higher. Install peer dependencies via:\n\n```sh\n# NPM\nnpm i -D typescript@^4.1.2\n\n# Yarn\nyarn add -D typescript@^4.1.2\n```\n\n## How to use it?\n\n`extended-enum` exposes a utility function named `extend`. Simply wrap an enum with `extend`, then you are ready to go!\n\n```typescript\nimport { extend } from 'extended-enum';\n// you may import using CommonJS syntax\n// const { extend } = require('extended-enum');\n\nenum Animal {\n  Bird,\n  Cat,\n  Dog,\n}\n\n// use the utility as follows:\nclass EAnimal extends extend\u003ctypeof Animal, Animal\u003e(Animal) {}\n```\n\nNOTE: you cannot use `extend` with `const enum`s, which are non-objects in runtime.\n\n## What powers does it have?\n\n### Basic functionalities\n\n`extend`ed enumerations preserves all the functionality of TypeScript `enum`s, such as:\n\n- Reference all the enumerated values using the key. The reference equality is preserved.\n\n```typescript\n// access values using keys\nconst pet = EAnimal.Cat;\nconst pets = [EAnimal.Dog];\n\n// reference equality is preserved\nexpect(EAnimal.Cat === EAnimal.Cat).toBe(true);\nexpect(EAnimal.Cat === EAnimal.Dog).toBe(false);\nexpect(EAnimal.Cat !== EAnimal.Dog).toBe(true);\n```\n\n- The enum type itself becomes the union of all the values. The values of enums does not overlap another.\n\n\n\u003e The exhaustive typing of union is not supported yet\n\n```typescript\nfunction f(animal: EAnimal): void {\n  // Produces type error:\n  // This condition will always return 'false' since the types 'EAnimal.Cat' and 'EAnimal.Dog' have no overlap.\n  if (animal.is(EAnimal.Cat) \u0026\u0026 animal.is(EAnimal.Dog)) {\n    type T1 = typeof animal; // never\n  }\n}\n\nfunction g(animal: EAnimal): void {\n  switch (animal) {\n    case EAnimal.Bird:\n      ...\n      break\n    case EAnimal.Cat:\n      ...\n      break\n    case EAnimal.Dog:\n      ...\n      break\n    default:\n      // TypeScript compiler detects it is unreachable here\n      type T2 = typeof animal; // never\n}\n```\n\n- Reverse mapping of values to keys are preserved.\n\nIn native `enum`s, reverse mapping from the values to the keys is supported:\n\n```typescript\nenum Animal {\n  Cat,\n  Dog,\n}\n\nexpect(Animal[Animal.Cat]).toBe('Cat');\nexpect(Animal[Aniaml.Dog]).toBe('Dog');\n```\n\n`extend`ed enum is not an indexable type (in JS, only `number`, `string`, `symbol` types can be used to index an object). Thus, `extend`ed enum provides a similar indexing method to access keys from the values.\n\n```typescript\nenum Animal { Cat, Dog }\n\nclass EAnimal extends extend\u003ctypeof Animal, Animal\u003e(Animal) {}\n\n/* use \"keyOf\" method to access keys */\nexpect(EAnimal.keyOf(EAnimal.Cat)).toBe('Cat');\nexpect(EAnimal.keyOf(EAnimal.Dog)).toBe('Dog');\n```\n\n### Serialization and Deserialization\n\n`from` provides a way to parse a primitive value (`string` or `number`) into one of the enumerated value.\n\nIt is safely typed, so if the given primitive does not match one of the defined primitive, it returns `undefined`.\n\n```typescript\nexpect(EAnimal.from(0)).toBe(EAnimal.Bird);\nexpect(EAnimal.from(1)).toBe(EAnimal.Cat);\n\n// an undefined primitive\nexpect(EAnimal.from(-1)).toBe(undefined);\n```\n\nOr, parse using the fallback value:\n\n```typescript\nexpect(EAnimal.from(-1, Animal.Dog)).toBe(EAnimal.Dog);\n```\n\n### Iterations\n\n`extend`ed enumerations provide iterables of defined keys and values.\n\n```typescript\nfor (const animal of EAnimal) {\n  // animal := EAnimal.Bird, EAnimal.Cat, EAnimal.Dog in each loop \n}\n\nfor (const key of EAnimal.keys()) {\n  // key := 'Bird', 'Cat', 'Dog' in each loop\n}\n\nexpect([...EAnimal.entries()])\n  .toEqual([\n    [ 'Bird', EAnimal.Bird ],\n    [ 'Cat',  EAnimal.Cat  ],\n    [ 'Dog',  EAnimal.Dog  ],\n  ]);\n```\n\n### Matches\n\n`extend`ed enumerations provide a default method named `is`. `is` is aware of the primitives defining the enumeration.\n\n```typescript\nenum Fruit {\n  Apple = 'APPLE',\n  Orange = 'ORANGE',\n}\nclass EFruit extends extend\u003ctypeof Fruit, Fruit\u003e(Fruit) {}\n\n// compare using values\nexpect(EFruit.Apple.is(EFruit.Apple)).toBe(true);\nexpect(EFruit.Apple.is(EFruit.Orange)).toBe(false);\n\n// compare using primitives\nexpect(EFruit.Apple.is('APPLE')).toBe(true);\nexpect(EFruit.Apple.is('apple')).toBe(false);\nexpect(EFruit.Apple.is('ORANGE')).toBe(false);\n```\n\n`match` provides a utility for pattern matching. Specify the mappings as you please (defining patterns with keys, values, primitive values are all supported), then the utility will search for the pattern and return the desired mapping.\n\nMapping multiple patterns to a single value is also supported (see the last example).\n\n```typescript\ndeclare const fruit: EFruit;\n\n// match using values\nfruit.match({\n  APPLE: 0,\n  ORANGE: 1,\n});\n\n// match using keys\nfruit.match({\n  Apple: 0,\n  Orange: 1, \n});\n\n// extended enum values are objects,\n// so it cannot be used as object keys.\n// hence, defining each case as tuple is provided.\nfruit.match([\n  [EFruit.Apple, 0],\n  [EFruit.Orange, 1],\n  [[EFruit.Apple, EFruit.Orange], 2], // matching multiple patterns to a single value\n])\n```\n\n### Inheriting extended enumerations\n\nFurther extending default extended enumeration class is also possible. You may add more methods, or override existing core methods such as `is` or `from` to customize the default behavior.\n\n```typescript\n// define the extended interface\ninterface IPet {\n  readonly walks: boolean\n}\n\n// you may add more methods to extend enumerations\nclass EPets extends extend\u003ctypeof Animal, Animal, IPet\u003e(Animal) {\n\n  get walks(): boolean {\n    return this.isNot(EPets.Bird);\n  }\n\n}\n\nEPets.Cat.walks  // true\nEPets.Bird.walks // false\n```\n\n## API Documentation\n\nSee [website](https://inhibitor1217.github.io/extended-enum/).\n\n## Contribution\n\nFeel free to open issues in GitHub for bug reports or feature requests!\n\n## LICENSE\n\nSee [LICENSE](https://github.com/inhibitor1217/extended-enum/blob/master/LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finhibitor1217%2Fextended-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finhibitor1217%2Fextended-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finhibitor1217%2Fextended-enum/lists"}