-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathenum.ts
More file actions
86 lines (74 loc) · 2.84 KB
/
enum.ts
File metadata and controls
86 lines (74 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* The standard enum type
*/
export type StandardEnum<T> = {
[id: string]: T | string;
[nu: number]: string;
};
/**
* Returns the name of an enum from its value
* @param enumVariable The enum for which you want to get the name
* @param enumValue The value of the enum for which you want to get the name
* @returns A string containing the name of the enum
*/
export function getEnumNameFromValue<T>(enumVariable: StandardEnum<T>, enumValue: T): string {
let result = enumVariable[enumValue as keyof StandardEnum<T>] as string;
if (result === undefined && isEnumString(enumVariable)) {
result = Object.keys(enumVariable)[Object.values(enumVariable).indexOf(enumValue)];
}
return result;
}
/**
* Returns the value of an enum from its name
* @param enumVariable The enum for which you want to get the value
* @param enumName The name of the enum for which you want to get the value
* @returns A string containing the value of the enum
*/
export function getEnumValueFromName<T>(enumVariable: StandardEnum<T>, enumName: string): T {
return enumVariable[enumName as keyof StandardEnum<T>] as T;
}
/**
* Returns all the names of an enum
* @param enumVariable The enum for which you want to get the names
* @returns A string array containing the names of the enum
*/
export function getEnumNames<T>(enumVariable: StandardEnum<T>) {
const names = Object.values(enumVariable);
// If enum is with values integer, object.values returns a list of [names, values].
return isEnumString(enumVariable) ? Object.keys(enumVariable) : names.slice(0, names.length / 2);
}
/**
* Returns all the values of an enum
* @param enumVariable The enum for which you want to get the values
* @returns A string or number array containing the values of the enum
*/
export function getEnumValues<T>(enumVariable: StandardEnum<T>): T[] {
const keys = Object.keys(enumVariable);
// If enum is with values integer, object.keys returns a list of [values, names].
return (
isEnumString(enumVariable) ? Object.values(enumVariable) : keys.slice(0, keys.length / 2).map((value) => Number.parseInt(value))
) as T[];
}
/**
* Parses a string to an enum value
* @param enumVariable The enum for which you want to get the values
* @param value The string to parse
* @returns The enum value corresponding to the string, or undefined if not found
*/
export function parseEnum<T>(enumVariable: StandardEnum<T>, value: string): T | undefined {
if (!isEnumString(enumVariable) && Number.isNaN(Number(value))) {
return undefined;
}
return getEnumValueFromName(enumVariable, getEnumNameFromValue(enumVariable, value as T));
}
/**
* Returns if it's a enum with string value
* @param enumVariable The enum
* @returns A bool
*/
function isEnumString<T>(enumVariable: StandardEnum<T>) {
for (const key in enumVariable) {
return !/^\d+$/.test(key);
}
return false;
}