()
| 5 | import type { Vector } from './types'; |
| 6 | |
| 7 | const 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 | |
| 44 | export { useReversePiecePosition }; |
no test coverage detected