/** 타자 대결: how each letter of the text looks against what is typed (pure, for the screen and its tests). */ import { toKeys } from '@bg/games/typing'; export type Mark = 'ok' | 'bad' | 'comp' | ''; /** How each letter of the target looks against what is typed: right, wrong, or the syllable still being composed. */ export function marks(target: string, typed: string): { marks: Mark[]; extra: number } { const t = [...target]; const y = [...typed.replace(/^\s+/, '')]; const out: Mark[] = t.map(() => ''); for (let i = 0; i < y.length && i < t.length; i++) { if (y[i] === t[i]) out[i] = 'ok'; else if (i === y.length - 1 && toKeys(t.slice(i).join('')).startsWith(toKeys(y[i]!))) out[i] = 'comp'; else out[i] = 'bad'; } return { marks: out, extra: Math.max(0, y.length - t.length) }; }