Skip to content

Commit 8e81885

Browse files
committed
Split a few things into their own files
1 parent e6a03fb commit 8e81885

4 files changed

Lines changed: 50 additions & 42 deletions

File tree

β€Žsrc/App.tsxβ€Ž

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,12 @@
11
import React from "react";
22

3-
const MOVE_CONFIG = {
4-
rock: {
5-
scissors: "crushes",
6-
lizard: "crushes",
7-
},
8-
paper: {
9-
rock: "covers",
10-
spock: "disproves",
11-
},
12-
scissors: {
13-
paper: "cuts",
14-
lizard: "decapitates",
15-
},
16-
lizard: {
17-
paper: "eats",
18-
Spock: "poisons",
19-
},
20-
Spock: {
21-
scissors: "smashes",
22-
rock: "vaporizes",
23-
},
24-
} as const;
25-
26-
type Move = keyof typeof MOVE_CONFIG;
27-
28-
function objectKeys<T extends Record<PropertyKey, unknown>>(
29-
obj: T,
30-
): (keyof T)[] {
31-
return Object.keys(obj);
32-
}
33-
34-
const MOVES: readonly Move[] = objectKeys(MOVE_CONFIG);
3+
import { MOVE_CONFIG, MOVES, MOVE_EMOJI, type Move } from "./constants";
4+
import getRandomElement from "./util/getRandomElement";
355

366
function getRandomMove(): Move {
37-
const index = Math.floor(Math.random() * MOVES.length);
38-
return MOVES[index];
7+
return getRandomElement(MOVES);
398
}
409

41-
const MOVE_EMOJI = {
42-
rock: "πŸͺ¨",
43-
paper: "πŸ“„",
44-
scissors: "βœ‚οΈ",
45-
lizard: "🦎",
46-
Spock: "πŸ––",
47-
} as const satisfies Record<Move, string>;
48-
4910
function getWinner(moveA: Move, moveB: Move): "a" | "b" | "tie" {
5011
if (moveA === moveB) {
5112
return "tie";

β€Žsrc/constants.tsβ€Ž

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import objectKeys from "./util/objectKeys";
2+
3+
export const MOVE_CONFIG = {
4+
rock: {
5+
scissors: "crushes",
6+
lizard: "crushes",
7+
},
8+
paper: {
9+
rock: "covers",
10+
spock: "disproves",
11+
},
12+
scissors: {
13+
paper: "cuts",
14+
lizard: "decapitates",
15+
},
16+
lizard: {
17+
paper: "eats",
18+
Spock: "poisons",
19+
},
20+
Spock: {
21+
scissors: "smashes",
22+
rock: "vaporizes",
23+
},
24+
} as const;
25+
26+
export type Move = keyof typeof MOVE_CONFIG;
27+
28+
MOVE_CONFIG satisfies Record<Move, Partial<Record<Move, string>>>;
29+
30+
export const MOVES: readonly Move[] = objectKeys(MOVE_CONFIG);
31+
32+
export const MOVE_EMOJI = {
33+
rock: "πŸͺ¨",
34+
paper: "πŸ“„",
35+
scissors: "βœ‚οΈ",
36+
lizard: "🦎",
37+
Spock: "πŸ––",
38+
} as const satisfies Record<Move, string>;

β€Žsrc/util/getRandomElement.tsβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function getRandomElement<T>(array: readonly T[]): T {
2+
const index = Math.floor(Math.random() * array.length);
3+
return array[index];
4+
}

β€Žsrc/util/objectKeys.tsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function objectKeys<T extends Record<PropertyKey, unknown>>(
2+
obj: T,
3+
): (keyof T)[] {
4+
return Object.keys(obj);
5+
}

0 commit comments

Comments
Β (0)