Skip to content

Commit a217a1f

Browse files
committed
better pattern matching
1 parent ae80038 commit a217a1f

File tree

1 file changed

+28
-51
lines changed

1 file changed

+28
-51
lines changed

src/components/shapes/index.tsx

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import shuffle from 'lodash/shuffle'
22
import uniqueId from 'lodash/uniqueId'
33
import { FC, useMemo } from 'react'
4+
import { match } from 'ts-pattern'
45
import ShapeDrawer, { Coordinates } from '../ShapeDrawer'
56
import I, { COLOR_I } from './I'
67
import J, { COLOR_J } from './J'
@@ -12,38 +13,24 @@ import Z, { COLOR_Z } from './Z'
1213

1314
export type Direction = 'UP' | 'DOWN' | 'LEFT' | 'RIGHT'
1415

15-
export const directionToRotation = (direction: Direction) => {
16-
switch (direction) {
17-
case 'UP':
18-
return 270
19-
case 'LEFT':
20-
return 180
21-
case 'DOWN':
22-
return 90
23-
case 'RIGHT':
24-
return 0
25-
default:
26-
// @ts-expect-error - exhaustive check
27-
throw new TypeError(`Unknown direction: ${direction.toString()}`)
28-
}
29-
}
16+
export const directionToRotation = (direction: Direction): number =>
17+
match(direction)
18+
.returnType<number>()
19+
.with('UP', () => 270)
20+
.with('LEFT', () => 180)
21+
.with('DOWN', () => 90)
22+
.with('RIGHT', () => 0)
23+
.exhaustive()
3024

3125
/** Returns the new direction based on a current direction. */
32-
export const nextDirection = (direction: Direction): Direction => {
33-
switch (direction) {
34-
case 'UP':
35-
return 'RIGHT'
36-
case 'RIGHT':
37-
return 'DOWN'
38-
case 'DOWN':
39-
return 'LEFT'
40-
case 'LEFT':
41-
return 'UP'
42-
default:
43-
// @ts-expect-error - exhaustive check
44-
throw new TypeError(`Unknown direction: ${direction.toString()}`)
45-
}
46-
}
26+
export const nextDirection = (direction: Direction): Direction =>
27+
match(direction)
28+
.returnType<Direction>()
29+
.with('UP', () => 'RIGHT')
30+
.with('RIGHT', () => 'DOWN')
31+
.with('DOWN', () => 'LEFT')
32+
.with('LEFT', () => 'UP')
33+
.exhaustive()
4734

4835
export interface ShapeProps {
4936
x: number
@@ -54,27 +41,17 @@ export interface ShapeProps {
5441
export type Shape = 'I' | 'J' | 'L' | 'O' | 'S' | 'T' | 'Z'
5542
export const SHAPES = Object.freeze(['I', 'J', 'L', 'O', 'S', 'T', 'Z'] as Shape[])
5643

57-
export const calculateCoordinates = (shape: Shape, shapeProps: ShapeProps): Coordinates => {
58-
switch (shape) {
59-
case 'I':
60-
return I(shapeProps)
61-
case 'J':
62-
return J(shapeProps)
63-
case 'L':
64-
return L(shapeProps)
65-
case 'O':
66-
return O(shapeProps)
67-
case 'S':
68-
return S(shapeProps)
69-
case 'T':
70-
return T(shapeProps)
71-
case 'Z':
72-
return Z(shapeProps)
73-
default:
74-
// @ts-expect-error - exhaustive check
75-
throw new TypeError(`Unknown shape: ${shape.toString()}`)
76-
}
77-
}
44+
export const calculateCoordinates = (shape: Shape, shapeProps: ShapeProps): Coordinates =>
45+
match(shape)
46+
.returnType<Coordinates>()
47+
.with('I', () => I(shapeProps))
48+
.with('J', () => J(shapeProps))
49+
.with('L', () => L(shapeProps))
50+
.with('O', () => O(shapeProps))
51+
.with('S', () => S(shapeProps))
52+
.with('T', () => T(shapeProps))
53+
.with('Z', () => Z(shapeProps))
54+
.exhaustive()
7855

7956
export const colorFromShape = (shape: Shape): string => colorMap[shape]
8057

0 commit comments

Comments
 (0)