1
1
import shuffle from 'lodash/shuffle'
2
2
import uniqueId from 'lodash/uniqueId'
3
3
import { FC , useMemo } from 'react'
4
+ import { match } from 'ts-pattern'
4
5
import ShapeDrawer , { Coordinates } from '../ShapeDrawer'
5
6
import I , { COLOR_I } from './I'
6
7
import J , { COLOR_J } from './J'
@@ -12,38 +13,24 @@ import Z, { COLOR_Z } from './Z'
12
13
13
14
export type Direction = 'UP' | 'DOWN' | 'LEFT' | 'RIGHT'
14
15
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 ( )
30
24
31
25
/** 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 ( )
47
34
48
35
export interface ShapeProps {
49
36
x : number
@@ -54,27 +41,17 @@ export interface ShapeProps {
54
41
export type Shape = 'I' | 'J' | 'L' | 'O' | 'S' | 'T' | 'Z'
55
42
export const SHAPES = Object . freeze ( [ 'I' , 'J' , 'L' , 'O' , 'S' , 'T' , 'Z' ] as Shape [ ] )
56
43
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 ( )
78
55
79
56
export const colorFromShape = ( shape : Shape ) : string => colorMap [ shape ]
80
57
0 commit comments