lila/ui/round/src/util.ts

27 lines
852 B
TypeScript
Raw Permalink Normal View History

2021-02-06 06:26:05 -07:00
import * as cg from 'chessground/types';
2021-08-21 06:27:23 -06:00
import { VNodeData } from 'snabbdom';
import { Dests, EncodedDests } from './interfaces';
export { bind, onInsert } from 'common/snabbdom';
2017-04-25 08:10:14 -06:00
2020-10-02 14:28:42 -06:00
export const justIcon = (icon: string): VNodeData => ({
2021-02-06 06:26:05 -07:00
attrs: { 'data-icon': icon },
2020-10-02 14:28:42 -06:00
});
2017-05-23 02:34:28 -06:00
2020-10-02 14:28:42 -06:00
export const uci2move = (uci: string): cg.Key[] | undefined => {
2017-04-25 05:16:04 -06:00
if (!uci) return undefined;
2017-04-25 08:10:14 -06:00
if (uci[1] === '@') return [uci.slice(2, 4) as cg.Key];
return [uci.slice(0, 2), uci.slice(2, 4)] as cg.Key[];
2021-02-06 06:26:05 -07:00
};
2017-07-09 04:23:43 -06:00
export function parsePossibleMoves(dests?: EncodedDests): Dests {
const dec = new Map();
if (!dests) return dec;
if (typeof dests == 'string')
for (const ds of dests.split(' ')) {
2020-10-02 14:28:42 -06:00
dec.set(ds.slice(0, 2), ds.slice(2).match(/.{2}/g) as cg.Key[]);
}
else for (const k in dests) dec.set(k, dests[k].match(/.{2}/g) as cg.Key[]);
2017-07-12 07:25:30 -06:00
return dec;
2017-07-09 04:23:43 -06:00
}