diff --git a/src/board-manager.ts b/src/board-manager.ts index eddb820..a9de9ae 100644 --- a/src/board-manager.ts +++ b/src/board-manager.ts @@ -430,12 +430,12 @@ export class BoardManager { applyTheme(): void { const theme = this.settings.boardTheme; const colors = - theme === 'custom' - ? { - light: this.settings.lightSquareColor, - dark: this.settings.darkSquareColor - } - : BOARD_THEMES[theme]; + theme === 'custom' + ? { + light: this.settings.lightSquareColor, + dark: this.settings.darkSquareColor + } + : BOARD_THEMES[theme] ?? BOARD_THEMES['brown']; this.container.style.setProperty('--cv-light', colors.light); this.container.style.setProperty('--cv-dark', colors.dark); diff --git a/src/navigation-controller.ts b/src/navigation-controller.ts index ac407ce..8a3bb21 100644 --- a/src/navigation-controller.ts +++ b/src/navigation-controller.ts @@ -407,8 +407,13 @@ export class NavigationController { } const move = this.moves[this.currentMoveIndex - 1]; - const hasNag = move?.nag; - const hasComment = move?.comment; + if (!move) { + this.commentEl.addClass('hidden'); + return; + } + + const hasNag = !!move.nag; + const hasComment = !!move.comment; if (!hasNag && !hasComment) { this.commentEl.addClass('hidden'); @@ -417,12 +422,12 @@ export class NavigationController { this.commentEl.removeClass('hidden'); - if (hasNag) { - const nagClass = NAG_CLASSES[move.nag!] || ''; + if (hasNag && move.nag) { + const nagClass = NAG_CLASSES[move.nag] || ''; const nagInfo = Object.values(NAG_SYMBOLS).find( (n) => n.symbol === move.nag ); - const label = nagInfo ? nagInfo.label : move.nag!; + const label = nagInfo ? nagInfo.label : move.nag; this.commentEl.createSpan({ cls: `cv-comment-nag ${nagClass}`, @@ -430,10 +435,10 @@ export class NavigationController { }); } - if (hasComment) { + if (hasComment && move.comment) { this.commentEl.createSpan({ cls: 'cv-comment-text', - text: move.comment! + text: move.comment }); } } diff --git a/src/parser.ts b/src/parser.ts index e95b464..dd16e40 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -371,26 +371,26 @@ export function parseMovesFromPgn( try { const result = chess.move(moveStr, { sloppy: true }); - if (result) { - const moveData: MoveData = { - san: result.san, - from: result.from, - to: result.to, - fen: chess.fen(), - comment: pendingComment, - nag: pendingNag, - annotations: pendingAnnotation - }; + if (!result) continue; // change from if (result) { ... } to guard clause + + const moveData: MoveData = { + san: result.san, + from: result.from, + to: result.to, + fen: chess.fen(), + comment: pendingComment, + nag: pendingNag, + annotations: pendingAnnotation + }; - if (inlineNag && NAG_SYMBOLS[inlineNag]) { - moveData.nag = NAG_SYMBOLS[inlineNag].symbol; - } - - moves.push(moveData); - pendingComment = undefined; - pendingAnnotation = undefined; - pendingNag = undefined; + if (inlineNag && NAG_SYMBOLS[inlineNag]) { + moveData.nag = NAG_SYMBOLS[inlineNag].symbol; } + + moves.push(moveData); + pendingComment = undefined; + pendingAnnotation = undefined; + pendingNag = undefined; } catch { if (warnings) { warnings.push( diff --git a/src/puzzle-controller.ts b/src/puzzle-controller.ts index 0fc28b2..8dfd9bf 100644 --- a/src/puzzle-controller.ts +++ b/src/puzzle-controller.ts @@ -349,8 +349,8 @@ export class PuzzleController { if (this.data.fen) { const parts = this.data.fen.split(/\s+/); - if (parts[5]) startMoveNum = parseInt(parts[5]) || 1; - if (parts[1] === 'b') startIsBlack = true; + startMoveNum = parseInt(parts[5] ?? '1') || 1; + startIsBlack = parts[1] === 'b'; } let moveNum = startMoveNum;