UNPKG

shogiops

Version:
34 lines (28 loc) 1.05 kB
import type { MoveOrDrop, Square } from '../types.js'; import { defined } from '../util.js'; import type { Position } from '../variant/position.js'; import { makeJapaneseMoveOrDrop } from './japanese.js'; const DIZHI = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥']; export function makeYorozuyaMoveOrDrop( pos: Position, md: MoveOrDrop, lastDest?: Square, ): string | undefined { const jpMove = makeJapaneseMoveOrDrop(pos, md, lastDest); return defined(jpMove) ? convertJapaneseToYorozuya(jpMove) : jpMove; } export function convertJapaneseToYorozuya(jp: string): string { return ( jp .replace('不成', '') .replace(/成$/, 'ナル') // matches full width numbers .replace(/[\d\uFF10-\uFF19]+/g, (match) => { const normalized = match.replace(/[\uFF10-\uFF19]/g, (char) => String.fromCharCode(char.charCodeAt(0) - 0xff10 + 48), ); const index = Number.parseInt(normalized, 10) - 1; return DIZHI[index] || match; }) ); }