-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notation.ts
40 lines (36 loc) · 1.06 KB
/
Notation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* eslint-disable prefer-destructuring */
import { Square } from 'chess.js'
import { Dimensions } from 'react-native'
import { Vector } from 'react-native-redash'
const { width } = Dimensions.get('window')
export const SIZE = width / 8
export const toTranslation = (to: Square) => {
'worklet'
// worklet don't support destructuring yet
const tokens = to.split('')
const col = tokens[0]
const row = tokens[1]
if (!col || !row) {
throw new Error('Invalid notation: ' + to)
}
const indexes = {
x: col.charCodeAt(0) - 'a'.charCodeAt(0),
y: parseInt(row, 10) - 1,
}
return {
x: indexes.x * SIZE,
y: 7 * SIZE - indexes.y * SIZE,
}
}
export const toPosition = ({ x, y }: Vector) => {
'worklet'
const col = String.fromCharCode(97 + Math.round(x / SIZE))
const row = `${8 - Math.round(y / SIZE)}`
return `${col}${row}` as Square
}
export const coordsToPosition = ({ x, y }: Vector) => {
'worklet'
const col = String.fromCharCode(97 + Math.floor(x / SIZE))
const row = `${8 - Math.floor(y / SIZE)}`
return `${col}${row}` as Square
}