MCPcopy Create free account
hub / github.com/code100x/chess / useReversePiecePosition

Function useReversePiecePosition

apps/native/components/chessboard/notation.ts:7–42  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import type { Vector } from './types';
6
7const useReversePiecePosition = () => {
8 const { pieceSize } = useChessboardProps();
9
10 const toTranslation = useCallback(
11 (to: Square) => {
12 'worklet';
13 const tokens = to.split('');
14 const col = tokens[0];
15 const row = tokens[1];
16 if (!col || !row) {
17 throw new Error('Invalid notation: ' + to);
18 }
19 const indexes = {
20 x: col.charCodeAt(0) - 'a'.charCodeAt(0),
21 y: parseInt(row, 10) - 1,
22 };
23 return {
24 x: indexes.x * pieceSize,
25 y: 7 * pieceSize - indexes.y * pieceSize,
26 };
27 },
28 [pieceSize],
29 );
30
31 const toPosition = useCallback(
32 ({ x, y }: Vector) => {
33 'worklet';
34 const col = String.fromCharCode(97 + Math.round(x / pieceSize));
35 const row = `${8 - Math.round(y / pieceSize)}`;
36 return `${col}${row}` as Square;
37 },
38 [pieceSize],
39 );
40
41 return { toPosition, toTranslation };
42};
43
44export { useReversePiecePosition };

Callers 5

pieces.tsxFile · 0.90
PlaceholderDot.tsxFile · 0.90
index.tsxFile · 0.90
index.tsxFile · 0.90
index.tsxFile · 0.90

Calls 1

useChessboardPropsFunction · 0.90

Tested by

no test coverage detected