Skip to content

Commit 34e1c85

Browse files
committed
adjust some code style details
1 parent 1242c88 commit 34e1c85

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

.eslintrc.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ rules:
102102
no-cond-assign: 'off'
103103
no-console:
104104
- warn
105-
- allow:
106-
- warn
107-
- error
105+
- allow: ["info", "warn", "error"]
108106
no-else-return: 'off'
109107
no-lonely-if: 'off'
110108
no-multi-spaces:

src/chess.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,20 +300,22 @@ const PIECE_NAMES = {
300300
}
301301

302302
/** Generate algebraic notation for a legal move. */
303-
export const toAlgebraic = (move: Move, board: Board): Result<AlgebraicMove, string> => {
304-
const { from, to } = move
303+
export const toAlgebraic = (
304+
{ from, to, promotion, check, mate }: Move,
305+
board: Board,
306+
): Result<AlgebraicMove, string> => {
305307
const colorPiece = board[from]
306308
if (!colorPiece) {
307309
return err(`There is no piece on ${from}.`)
308310
}
309311
const color = colorPiece[0] as Color
310312
const piece = (colorPiece[1] ?? "") as Piece
311-
const check = move.mate ? "#" : move.check ? "+" : ""
313+
const checkSign = mate ? "#" : check ? "+" : ""
312314

313315
// castling
314316
const fileDelta = to.charCodeAt(0) - from.charCodeAt(0)
315317
if (piece === "K" && Math.abs(fileDelta) === 2) {
316-
return ok(`${fileDelta > 0 ? "O-O" : "O-O-O"}${check}` satisfies AlgebraicMove)
318+
return ok(`${fileDelta > 0 ? "O-O" : "O-O-O"}${checkSign}` satisfies AlgebraicMove)
317319
}
318320

319321
const opponentPiece = board[to]
@@ -338,7 +340,7 @@ export const toAlgebraic = (move: Move, board: Board): Result<AlgebraicMove, str
338340
return err(`You have no ${PIECE_NAMES[piece]} on ${from}.`)
339341
}
340342

341-
const suffix = `${opponentPiece ? "x" : ""}${to}${move.promotion ? `=${move.promotion}` : ""}${check}`
343+
const suffix = `${opponentPiece ? "x" : ""}${to}${promotion ? `=${promotion}` : ""}${checkSign}`
342344
if (candidates.length === 1 && !(piece === "" && opponentPiece)) {
343345
return ok(`${piece}${suffix}` as AlgebraicMove)
344346
}
@@ -546,23 +548,27 @@ export const revertToMove = (idx: number, game: Game): Game =>
546548
applyHistory(START_GAME, game.history.slice(0, idx)).unwrap()
547549

548550

549-
export const toPGN = (game: Game): string => {
551+
/**
552+
* Turn game state into Portable Game Notation (PGN)
553+
* @see https://en.wikipedia.org/wiki/Portable_Game_Notation
554+
*/
555+
export const toPGN = ({ history, outcome, termination }: Game): string => {
550556
const date = new Date().toISOString().slice(0, 10).replaceAll("-", ".")
551-
const result = match(game.outcome)
557+
const result = match(outcome)
552558
.with("w", () => "1-0")
553559
.with("b", () => "0-1")
554560
.with("draw", () => "1/2-1/2")
555561
.with(undefined, () => "*")
556562
.exhaustive()
557-
const termination = game.outcome
563+
const term = outcome
558564
? (
559-
match(game.outcome)
565+
match(outcome)
560566
.with("w", () => "White wins")
561567
.with("b", () => "Black wins")
562568
.with("draw", () => "Draw")
563569
.exhaustive()
564570
+ " "
565-
+ match(game.termination)
571+
+ match(termination)
566572
.with("checkmate", () => "by checkmate.")
567573
.with("time", () => "on time.")
568574
.with("stalemate", () => " by stalemate")
@@ -574,10 +580,10 @@ export const toPGN = (game: Game): string => {
574580
)
575581
: "unterminated"
576582
const moves = Array.from(
577-
pairs(game.history.map(move => move.algebraic)),
583+
pairs(history.map(move => move.algebraic)),
578584
(movePair, idx) => `${idx + 1}. ${movePair.join(" ")}`,
579585
)
580-
if (game.outcome) {
586+
if (outcome) {
581587
moves.push(result)
582588
}
583589
return [
@@ -588,7 +594,7 @@ export const toPGN = (game: Game): string => {
588594
`[White "?"]`,
589595
`[Black "?"]`,
590596
`[Result "${result}"]`,
591-
`[Termination "${termination}"]`,
597+
`[Termination "${term}"]`,
592598
// `[TimeControl "120+1"]`,
593599
// `[Time HH:MM:SS]`,
594600
// `[EndTime "3:38:57 PST"]`,

src/result.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ export abstract class Result<T = unknown, E = unknown> implements Promise<T> {
3939
return this.toPromise().then(onFulfilled, onRejected)
4040
}
4141

42-
catch<Y = E>(onRejected?: ((reason: E) => Y | PromiseLike<Y>) | null) {
42+
catch <Y = E>(onRejected?: ((reason: E) => Y | PromiseLike<Y>) | null) {
4343
return this.toPromise().catch(onRejected)
4444
}
4545

46+
// eslint-disable-next-line @stylistic/keyword-spacing
4647
finally(onFinally?: (() => void) | null): Promise<T> {
4748
return this.toPromise().finally(onFinally)
4849
}

src/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export const isTruthy = <T>(x: T | false | null | undefined | 0 | ""): x is T =>
88

99
const sentinel = Symbol("UNSET")
1010

11+
/**
12+
* Collect items into pairs
13+
* e.g. [1, 2, 3, 4, 5] => [[1, 2], [3, 4], [5]]
14+
*/
1115
export function* pairs<T>(xs: Iterable<T>): Iterable<[T, T] | [T]> {
1216
let left: T | typeof sentinel = sentinel
1317
for (const x of xs) {
@@ -28,7 +32,7 @@ export function* pairs<T>(xs: Iterable<T>): Iterable<[T, T] | [T]> {
2832
* to the user.
2933
*/
3034
export function saveTextAs(text:string, filename:string) {
31-
const blob = new Blob([text], {type: "text/plain;charset=utf-8"})
35+
const blob = new Blob([text], { type: "text/plain;charset=utf-8" })
3236
const url = URL.createObjectURL(blob)
3337

3438
const downloadLink = document.createElement("a")

0 commit comments

Comments
 (0)