Skip to content

Commit 940c55f

Browse files
committed
Improve getEnumNameFromValue and getEnumValueFromName performance
1 parent 9983357 commit 940c55f

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Changed
1111

1212
- Split up `CI` workflow into two separate workflows for the comment posting to work for PRs from forks.
13+
- Improved `getEnumNameFromValue` and `getEnumValueFromName` performance
1314

1415
## [1.2.1] - 2025-03-18
1516

src/lib/enum.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,14 @@ describe("enum tests", () => {
9797
const values = getEnumValues<TestEnum>(TestEnum);
9898
expect(values).toStrictEqual([TestEnum.A, TestEnum.B, TestEnum.C]);
9999
});
100+
101+
test("getEnumNames/getEnumValues with empty enum", () => {
102+
enum TestEnum {}
103+
104+
const names = getEnumNames<TestEnum>(TestEnum);
105+
expect(names).toStrictEqual([]);
106+
107+
const values = getEnumValues<TestEnum>(TestEnum);
108+
expect(values).toStrictEqual([]);
109+
});
100110
});

src/lib/enum.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ export type StandardEnum<T> = {
1313
* @returns A string containing the name of the enum
1414
*/
1515
export function getEnumNameFromValue<T>(enumVariable: StandardEnum<T>, enumValue: T): string {
16-
return Object.keys(enumVariable)[Object.values(enumVariable).findIndex((x) => x === enumValue)];
16+
let result = enumVariable[enumValue as keyof StandardEnum<T>] as string;
17+
18+
if (result === undefined && isEnumString(enumVariable)) {
19+
result = Object.keys(enumVariable)[Object.values(enumVariable).indexOf(enumValue)];
20+
}
21+
22+
return result;
1723
}
1824

1925
/**
@@ -23,8 +29,7 @@ export function getEnumNameFromValue<T>(enumVariable: StandardEnum<T>, enumValue
2329
* @returns A string containing the value of the enum
2430
*/
2531
export function getEnumValueFromName<T>(enumVariable: StandardEnum<T>, enumName: string): T {
26-
const value = Object.values(enumVariable)[Object.keys(enumVariable).findIndex((x) => x === enumName)] as string;
27-
return (isEnumString(enumVariable) ? value : Number.parseInt(value)) as T;
32+
return enumVariable[enumName as keyof StandardEnum<T>] as T;
2833
}
2934

3035
/**
@@ -59,7 +64,9 @@ export function getEnumValues<T>(enumVariable: StandardEnum<T>): T[] {
5964
* @returns A bool
6065
*/
6166
function isEnumString<T>(enumVariable: StandardEnum<T>) {
62-
const keys = Object.keys(enumVariable);
67+
for (const key in enumVariable) {
68+
return !/^\d+$/.test(key);
69+
}
6370

64-
return keys.length > 0 && !/^\d+$/.test(keys.at(0) as string);
71+
return false;
6572
}

0 commit comments

Comments
 (0)