Skip to content

Commit 70a9382

Browse files
committed
Introduce Enum.{keys, values}
1 parent 2d12c91 commit 70a9382

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,31 @@ export function Enum(...values: any[]): object {
1616
}
1717

1818
export type Enum<T extends object> = T[keyof T];
19+
20+
export namespace Enum {
21+
function hasOwnProperty(obj: object, prop: string): boolean {
22+
return Object.prototype.hasOwnProperty.call(obj, prop);
23+
}
24+
25+
export function keys<
26+
T extends { [_: string]: any }
27+
>(e: T): Array<keyof T> {
28+
const result: string[] = [];
29+
for (const prop in e) {
30+
if (hasOwnProperty(e, prop)) {
31+
result.push(prop);
32+
}
33+
}
34+
return result as Array<keyof T>;
35+
}
36+
37+
export function values<
38+
T extends { [_: string]: any }
39+
>(e: T): Array<Enum<T>> {
40+
const result: Array<Enum<T>> = [];
41+
for (const key of keys(e)) {
42+
result.push(e[key as string]);
43+
}
44+
return result;
45+
}
46+
}

0 commit comments

Comments
 (0)