{"id":26709406,"url":"https://github.com/js-bits/enumerate","last_synced_at":"2026-03-07T22:02:49.869Z","repository":{"id":57122323,"uuid":"359197479","full_name":"js-bits/enumerate","owner":"js-bits","description":"Easy to use, Symbol-based enum implementation","archived":false,"fork":false,"pushed_at":"2023-07-19T02:11:54.000Z","size":1509,"stargazers_count":3,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T08:16:44.420Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/js-bits.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":"2021-04-18T16:31:29.000Z","updated_at":"2023-07-03T17:55:20.000Z","dependencies_parsed_at":"2024-06-21T14:23:45.817Z","dependency_job_id":null,"html_url":"https://github.com/js-bits/enumerate","commit_stats":{"total_commits":185,"total_committers":1,"mean_commits":185.0,"dds":0.0,"last_synced_commit":"9eb55d2cea5f28a5755d6e904513597d8c3d61f7"},"previous_names":[],"tags_count":46,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Fenumerate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Fenumerate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Fenumerate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/js-bits%2Fenumerate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/js-bits","download_url":"https://codeload.github.com/js-bits/enumerate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248752372,"owners_count":21156079,"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":"2025-03-27T08:16:48.361Z","updated_at":"2026-03-07T22:02:49.830Z","avatar_url":"https://github.com/js-bits.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Easy to use, Symbol-based enum implementation\n\nJust list keys you need and `enumerate` tag function will create an object with corresponding properties and unique values for your convenience.\n\n## Installation\n\nInstall with npm:\n\n```\nnpm install @js-bits/enumerate\n```\n\nInstall with yarn:\n\n```\nyarn add @js-bits/enumerate\n```\n\nImport where you need it:\n\n```javascript\nimport enumerate from '@js-bits/enumerate';\n```\n\nor require for CommonJS:\n\n```javascript\nconst enumerate = require('@js-bits/enumerate');\n```\n\n## How to use\n\nExample 1:\n\n```javascript\nconst { FOOT, METER } = enumerate`FOOT METER`;\n\nconst convertToFeet = (value, unit = FOOT) =\u003e {\n  if (unit === METER) {\n    return value * 3.281;\n  }\n  return value;\n};\n\nconsole.log(`${convertToFeet(5)} feet`); // 5 feet\nconsole.log(`${convertToFeet(2, METER)} feet`); // 6.562 feet\n```\n\nExample 2:\n\n```javascript\nconst STAR_WARS = enumerate`I II III IV V VI`;\n\nconst getEpisodeName = episode =\u003e {\n  const { I, II, III, IV, V, VI } = STAR_WARS;\n  switch (episode) {\n    case I:\n      return 'The Phantom Menace';\n    case II:\n      return 'Attack of the Clones';\n    case III:\n      return 'Revenge of the Sith';\n    case IV:\n      return 'A New Hope';\n    case V:\n      return 'The Empire Strikes Back';\n    case VI:\n      return 'Return of the Jedi';\n    default:\n      return 'Unknown';\n  }\n};\n\nconsole.log(getEpisodeName(STAR_WARS.III)); // Revenge of the Sith\nconsole.log(getEpisodeName(STAR_WARS.IV)); // A New Hope\nconsole.log(getEpisodeName(STAR_WARS.X)); // Error: Invalid enum key: X\n```\n\n## Primitive enum converters\n\nBy default `enumerate` converts values to [Symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol):\n\n```javascript\nconsole.log(enumerate`FOOT METER`); // Enum { FOOT: Symbol(FOOT), METER: Symbol(METER) }\n```\n\nYou can change this behavior by specifying an appropriate converter:\n\n```javascript\nconsole.log(enumerate(String)`FOOT METER`); // Enum { FOOT: 'FOOT', METER: 'METER' }\nconsole.log(enumerate(Number)`ZERO ONE TWO`); // Enum { ZERO: 0, ONE: 1, TWO: 2 }\n// or\nconst enumString = enumerate(String);\nconst enumNumber = enumerate(Number);\nconsole.log(enumString`FOOT METER`); // Enum { FOOT: 'FOOT', METER: 'METER' }\nconsole.log(enumNumber`ZERO ONE TWO`); // Enum { ZERO: 0, ONE: 1, TWO: 2 }\n```\n\n## Advanced enum converters\n\nThere are several advanced converters also available.\n\n```javascript\nconst { LowerCase, UpperCase, Prefix, Increment } = enumerate;\n\nconsole.log(enumerate(LowerCase)`\nVALUE1\nVALUE2\nVALUE3\n`); // Enum { VALUE1: 'value1', VALUE2: 'value2', VALUE3: 'value3' }\n\nconsole.log(enumerate(UpperCase)`\nvalue1\nvalue2\nvalue3\n`); // Enum { value1: 'VALUE1', value2: 'VALUE2', value3: 'VALUE3' }\n\nconsole.log(enumerate(Prefix('value|'))`x y z`); // Enum { x: 'value|x', y: 'value|y', z: 'value|z' }\n// or as a shortcut\nconsole.log(enumerate('value|')`x y z`); // Enum { x: 'value|x', y: 'value|y', z: 'value|z' }\n\nconsole.log(enumerate(Increment(10))`\nVALUE1\nVALUE2\nVALUE3\n`); // Enum { VALUE1: 10, VALUE2: 20, VALUE3: 30 }\n// or as a shortcut\nconsole.log(enumerate(10)`VALUE1 VALUE2 VALUE3`); // Enum { VALUE1: 10, VALUE2: 20, VALUE3: 30 }\n\n// the second argument here is a start value (equals to the first argument if not specified)\nconsole.log(enumerate(Increment(10, 19))`\nVALUE1\nVALUE2\nVALUE3\n`); // Enum { VALUE1: 19, VALUE2: 29, VALUE3: 39 }\n```\n\n## Customization\n\nOr you can implement your custom converter:\n\n```javascript\nconst customEnum = enumerate((acc, item) =\u003e {\n  acc[`-${item}-`] = `-${(Object.keys(acc).length + 1) * 10}-`;\n  return acc;\n});\n\nconsole.log(customEnum`\n  CODE1\n  CODE2\n  CODE3\n`); // Enum { '-CODE1-': '-10-', '-CODE2-': '-20-', '-CODE3-': '-30-' }\n```\n\nBut remember that only default behavior guarantees global uniqueness of enumerated values.\n\n## enumerate.isEnum()\n\nYou can also check if the given object is a enum or not.\n\n```javascript\nconsole.log(enumerate.isEnum({ a: 1, b: 2, c: 3 })); // false\nconsole.log(enumerate.isEnum(enumerate`a b c`)); // true\n```\n\n## Type-safety and IntelliSense (code completion)\n\nThe package includes a TypeScript Declaration File and supports VS Code IntelliSense features.\n\n\u003cimg src=\"./images/intellisense1.png\" width=\"500\"\u003e\n\nBut there is one caveat. In order to achieve full type safety you have to use a bit different syntax. Unfortunately.\nSo, instead of using `enumerate()` directly as a [tag function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) you can use `enumerate.ts()` function.\n\nCompare\n\n\u003cimg src=\"./images/intellisense2.png\" width=\"350\"\u003e\n\nversus\n\n\u003cimg src=\"./images/intellisense3.png\" _width=\"750\"\u003e\n\nFor example\n\n```javascript\nenumerate.ts('ZERO ONE TWO', Number); // Enum { ZERO: 0, ONE: 1, TWO: 2 }\n```\n\ndoes exactly the same as\n\n```javascript\nenumerate(Number)`ZERO ONE TWO`; // Enum { ZERO: 0, ONE: 1, TWO: 2 }\n```\n\nbut it allows TypeScript to recognize the result type.\n\n\u003cimg src=\"./images/intellisense4.png\" width=\"750\"\u003e\n\nThe reason of why we cannot use `enumerate` directly is that there is a long-standing TypeScript [issue](https://github.com/microsoft/TypeScript/issues/33304) with [TemplateStringArray](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_es5_d_.templatestringsarray.html) being incorrectly typed and, as result, not being able to be parameterized.\n\n## Notes\n\n- Be careful adding new items to an existing numeric enum. Always append them to the end of the list to avoid changing previous item values.\n- Requires TypeScript 4.8+ for best type safety features support.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-bits%2Fenumerate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjs-bits%2Fenumerate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjs-bits%2Fenumerate/lists"}