From 940c55fa19fc24429546f62237e0a2ccd6b52a99 Mon Sep 17 00:00:00 2001 From: Daniele Debernardi Date: Thu, 3 Apr 2025 18:43:36 +0200 Subject: [PATCH 1/2] Improve `getEnumNameFromValue` and `getEnumValueFromName` performance --- CHANGELOG.md | 1 + src/lib/enum.spec.ts | 10 ++++++++++ src/lib/enum.ts | 17 ++++++++++++----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82a5241..59b8f3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Split up `CI` workflow into two separate workflows for the comment posting to work for PRs from forks. +- Improved `getEnumNameFromValue` and `getEnumValueFromName` performance ## [1.2.1] - 2025-03-18 diff --git a/src/lib/enum.spec.ts b/src/lib/enum.spec.ts index 7655c76..45472f0 100644 --- a/src/lib/enum.spec.ts +++ b/src/lib/enum.spec.ts @@ -97,4 +97,14 @@ describe("enum tests", () => { const values = getEnumValues(TestEnum); expect(values).toStrictEqual([TestEnum.A, TestEnum.B, TestEnum.C]); }); + + test("getEnumNames/getEnumValues with empty enum", () => { + enum TestEnum {} + + const names = getEnumNames(TestEnum); + expect(names).toStrictEqual([]); + + const values = getEnumValues(TestEnum); + expect(values).toStrictEqual([]); + }); }); diff --git a/src/lib/enum.ts b/src/lib/enum.ts index a253bff..f5a9f36 100644 --- a/src/lib/enum.ts +++ b/src/lib/enum.ts @@ -13,7 +13,13 @@ export type StandardEnum = { * @returns A string containing the name of the enum */ export function getEnumNameFromValue(enumVariable: StandardEnum, enumValue: T): string { - return Object.keys(enumVariable)[Object.values(enumVariable).findIndex((x) => x === enumValue)]; + let result = enumVariable[enumValue as keyof StandardEnum] as string; + + if (result === undefined && isEnumString(enumVariable)) { + result = Object.keys(enumVariable)[Object.values(enumVariable).indexOf(enumValue)]; + } + + return result; } /** @@ -23,8 +29,7 @@ export function getEnumNameFromValue(enumVariable: StandardEnum, enumValue * @returns A string containing the value of the enum */ export function getEnumValueFromName(enumVariable: StandardEnum, enumName: string): T { - const value = Object.values(enumVariable)[Object.keys(enumVariable).findIndex((x) => x === enumName)] as string; - return (isEnumString(enumVariable) ? value : Number.parseInt(value)) as T; + return enumVariable[enumName as keyof StandardEnum] as T; } /** @@ -59,7 +64,9 @@ export function getEnumValues(enumVariable: StandardEnum): T[] { * @returns A bool */ function isEnumString(enumVariable: StandardEnum) { - const keys = Object.keys(enumVariable); + for (const key in enumVariable) { + return !/^\d+$/.test(key); + } - return keys.length > 0 && !/^\d+$/.test(keys.at(0) as string); + return false; } From bc6264cd157fb442d2b24e43b58a2927b8816e4b Mon Sep 17 00:00:00 2001 From: Daniele Debernardi Date: Fri, 4 Apr 2025 10:08:09 +0200 Subject: [PATCH 2/2] Add "Fixed" section to CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59b8f3a..a183554 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Split up `CI` workflow into two separate workflows for the comment posting to work for PRs from forks. + +### Fixed + - Improved `getEnumNameFromValue` and `getEnumValueFromName` performance ## [1.2.1] - 2025-03-18