{"id":13672846,"url":"https://github.com/beliefgp/ts-enum-object","last_synced_at":"2025-08-31T09:39:15.666Z","repository":{"id":57380710,"uuid":"328587490","full_name":"beliefgp/ts-enum-object","owner":"beliefgp","description":"解决 TypeScript 下枚举值无法为对象的一些应用场景","archived":false,"fork":false,"pushed_at":"2022-12-22T02:55:53.000Z","size":27,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-20T01:49:31.003Z","etag":null,"topics":["enums","objectvalue","typescript"],"latest_commit_sha":null,"homepage":"","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/beliefgp.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":"2021-01-11T07:50:44.000Z","updated_at":"2024-06-12T09:35:40.000Z","dependencies_parsed_at":"2023-01-30T05:15:21.957Z","dependency_job_id":null,"html_url":"https://github.com/beliefgp/ts-enum-object","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/beliefgp/ts-enum-object","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beliefgp%2Fts-enum-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beliefgp%2Fts-enum-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beliefgp%2Fts-enum-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beliefgp%2Fts-enum-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beliefgp","download_url":"https://codeload.github.com/beliefgp/ts-enum-object/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beliefgp%2Fts-enum-object/sbom","scorecard":{"id":230829,"data":{"date":"2025-08-11","repo":{"name":"github.com/beliefgp/ts-enum-object","commit":"87f2bcf3e810fe1ca1bba15ddb86429eae5811b0"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/14 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-17T04:49:30.386Z","repository_id":57380710,"created_at":"2025-08-17T04:49:30.387Z","updated_at":"2025-08-17T04:49:30.387Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272965440,"owners_count":25023067,"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-31T02:00:09.071Z","response_time":79,"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":["enums","objectvalue","typescript"],"created_at":"2024-08-02T09:01:51.604Z","updated_at":"2025-08-31T09:39:15.641Z","avatar_url":"https://github.com/beliefgp.png","language":"TypeScript","readme":"# ts-enum-object\n\n解决 TypeScript 下枚举值无法为对象，以及保证枚举值有序性的一些应用场景，并提供了完整的 TypeScript 类型映射。\n\n## 安装\n\n```bash\n$ npm i ts-enum-object --save\n```\n\n## API\n\n* createEnumObject(items: Array\u003c{ name: string; value: any; label?: string; ...other }\u003e)  \n  创建一个枚举对象，`name` 与 `value` 为必填，`name` 代表枚举对象的 key，其他字段可以按业务需求扩展。\n\n  **TypeScript 下入参的数组【务必】【务必】【务必】加 `as const`，否则无法正常推导出 Key 的类型**\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.A // 1\n  TestEnum.B // 2\n  TestEnum.C // 3\n  ```\n\n* .keys()  \n  获取枚举所有 `name`。\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.keys() // ['A', 'B', 'C']\n  ```\n\n* .values()  \n  获取枚举所有 `value`。\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.values() // [1, 2, 3]\n\n* .items()  \n  获取枚举列表，也就是 createEnumObject 的入参。\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.items()\n  // [\n  //   { name: 'A', value: 1, label: 'AA', },\n  //   { name: 'B', value: 2, label: 'BB', },\n  //   { name: 'C', value: 3, label: 'CC', },\n  // ]\n  ```\n* .getItemBy(key, valueOfKey)   \n  根据枚举配置对象中某个字段的名字及其值，获取对应的枚举配置项(如果会出现重复，只返回第一个)。\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.getItemBy('label', 'AA') // { name: 'A', value: 1, label: 'AA', }\n  ```\n\n* .getItemByName(valueOfName)   \n  根据枚举配置对象中字段 `name` 的值，获取对应的枚举配置项。\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.getItemByName('A') // { name: 'A', value: 1, label: 'AA', }\n  ```\n\n* .getItemByValue(valueOfValue)   \n  根据枚举配置对象中字段 `value` 的值，获取对应的枚举配置项。\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.getItemByValue(1) // { name: 'A', value: 1, label: 'AA', }\n  ```\n\n* .getLabel(valueOfNameOrValue)   \n  根据枚举配置对象中字段 `name` 或者 `value` 的值，获取对应的 `label`。\n\n  ```ts\n  import { createEnumObject } from 'ts-enum-object';\n\n  const TestEnum = createEnumObject([\n    { name: 'A', value: 1, label: 'AA', },\n    { name: 'B', value: 2, label: 'BB', },\n    { name: 'C', value: 3, label: 'CC', },\n  ] as const); // as const is required\n\n  TestEnum.getLabel('A') // 'AA'\n  TestEnum.getLabel(1) // 'AA'\n  ```\n\n* EnumObjectNamesType\\\u003cEnumObjectType\\\u003e   \n  Typescript 类型方法，获取枚举对象的所有 Name 类型。\n\n  ```ts\n  type Names = EnumObjectNamesType\u003ctypeof TestEnum\u003e; // A | B | C\n  ```\n\n* EnumObjectValuesType\\\u003cEnumObjectType\\\u003e    \n  Typescript 类型方法，获取枚举对象的所有 Name 类型。\n\n  ```ts\n  type Values = EnumObjectValuesType\u003ctypeof TestEnum\u003e; // 1 | 2 | 3\n  ```\n\n* EnumObjectFieldValueType\\\u003cEnumObjectType, FiledNameType\\\u003e    \n  Typescript 类型方法，获取枚举对象的所有 Name 类型。\n\n  ```ts\n  type Names = EnumObjectFieldValueType\u003ctypeof TestEnum, 'name'\u003e; // A | B | C\n  ```\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeliefgp%2Fts-enum-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeliefgp%2Fts-enum-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeliefgp%2Fts-enum-object/lists"}