Skip to content

Commit 8346ea2

Browse files
authored
Merge pull request #11 from kourge/keys_values_util
Introduce Enum.{keys, values}
2 parents ec9a523 + 0c085cb commit 8346ea2

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

__tests__/test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,23 @@ describe("Enum", () => {
1616
})).toEqual({ BLACK: "black", WHITE: "white" });
1717
});
1818
});
19+
20+
describe("Enum.keys", () => {
21+
it("returns the keys of an enum object", () => {
22+
const e = Enum({
23+
BLACK: "black",
24+
WHITE: "white",
25+
});
26+
expect(Enum.keys(e)).toEqual(expect.arrayContaining([ "WHITE", "BLACK" ]));
27+
});
28+
};
29+
30+
describe("Enum.values", () => {
31+
it("returns the values of an enum object", () => {
32+
const e = Enum({
33+
BLACK: "black",
34+
WHITE: "white",
35+
});
36+
expect(Enum.values(e)).toEqual(expect.arrayContaining([ "white", "black" ]));
37+
});
38+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"jest": "^18.1.0",
3838
"ts-jest": "^18.0.1",
3939
"tslint": "^4.3.1",
40-
"typescript": "^2.1.5"
40+
"typescript": "^2.2.1"
4141
},
4242
"jest": {
4343
"transform": {

src/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export function Enum<
33
T extends { [_: string]: V },
44
V extends string
55
>(definition: T): T;
6-
export function Enum(...values: any[]): any {
6+
export function Enum(...values: any[]): object {
77
if (typeof values[0] === "string") {
88
const result: any = {};
99
for (const value of values) {
@@ -15,4 +15,32 @@ export function Enum(...values: any[]): any {
1515
}
1616
}
1717

18-
export type Enum<T> = T[keyof T];
18+
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+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"interface-name": [
55
true, "never-prefix"
66
],
7+
"no-namespace": false,
78
"object-literal-sort-keys": false
89
}
910
}

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,9 +2472,9 @@ type-check@~0.3.2:
24722472
dependencies:
24732473
prelude-ls "~1.1.2"
24742474

2475-
typescript@^2.1.5:
2476-
version "2.1.5"
2477-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.5.tgz#6fe9479e00e01855247cea216e7561bafcdbcd4a"
2475+
typescript@^2.2.1:
2476+
version "2.2.1"
2477+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.2.1.tgz#4862b662b988a4c8ff691cc7969622d24db76ae9"
24782478

24792479
uglify-js@^2.6:
24802480
version "2.7.5"

0 commit comments

Comments
 (0)