From 022d52aae704d11febeb338ecc8772df055690ea Mon Sep 17 00:00:00 2001 From: Zac Date: Thu, 12 Mar 2020 21:21:00 -0400 Subject: [PATCH] Add new method to Chess.js (#42869) * [chess.js] add new method * [chess.js] correct method name in test * [chess.js] correct test case * [chess.js] run prettier * [chess.js] ts lint * [chess.js] update package version * Revert "[chess.js] update package version" This reverts commit aeea50bc67ee9f9353507f07a99b2bd60488d737. Co-authored-by: Zachary Svoboda --- types/chess.js/chess.js-tests.ts | 3 + types/chess.js/index.d.ts | 300 +++++++++++++++++++++---------- 2 files changed, 209 insertions(+), 94 deletions(-) diff --git a/types/chess.js/chess.js-tests.ts b/types/chess.js/chess.js-tests.ts index fb78bb5e3f..6fae26aee8 100644 --- a/types/chess.js/chess.js-tests.ts +++ b/types/chess.js/chess.js-tests.ts @@ -200,3 +200,6 @@ chess.history({ verbose: false }); // $ExpectType Move[] chess.history({ verbose: true }); + +// $ExpectType ({ type: PieceType; color: "w" | "b"; } | null)[][] +chess.board(); diff --git a/types/chess.js/index.d.ts b/types/chess.js/index.d.ts index 1e9e7aeca3..4bcfbb6157 100644 --- a/types/chess.js/index.d.ts +++ b/types/chess.js/index.d.ts @@ -1,22 +1,89 @@ // Type definitions for chess.js 0.10 // Project: https://github.com/jhlywa/chess.js // Definitions by: Jacob Fischer +// Zachary Svoboda // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Minimum TypeScript Version: 3.3 /** - * One of the possible sqaures on a chess board in san format, + * One of the possible squares on a chess board in san format, * e.g. "a8" to "h1". */ export type Square = - "a8" | "b8" | "c8" | "d8" | "e8" | "f8" | "g8" | "h8" | - "a7" | "b7" | "c7" | "d7" | "e7" | "f7" | "g7" | "h7" | - "a6" | "b6" | "c6" | "d6" | "e6" | "f6" | "g6" | "h6" | - "a5" | "b5" | "c5" | "d5" | "e5" | "f5" | "g5" | "h5" | - "a4" | "b4" | "c4" | "d4" | "e4" | "f4" | "g4" | "h4" | - "a3" | "b3" | "c3" | "d3" | "e3" | "f3" | "g3" | "h3" | - "a2" | "b2" | "c2" | "d2" | "e2" | "f2" | "g2" | "h2" | - "a1" | "b1" | "c1" | "d1" | "e1" | "f1" | "g1" | "h1" -; + | 'a8' + | 'b8' + | 'c8' + | 'd8' + | 'e8' + | 'f8' + | 'g8' + | 'h8' + | 'a7' + | 'b7' + | 'c7' + | 'd7' + | 'e7' + | 'f7' + | 'g7' + | 'h7' + | 'a6' + | 'b6' + | 'c6' + | 'd6' + | 'e6' + | 'f6' + | 'g6' + | 'h6' + | 'a5' + | 'b5' + | 'c5' + | 'd5' + | 'e5' + | 'f5' + | 'g5' + | 'h5' + | 'a4' + | 'b4' + | 'c4' + | 'd4' + | 'e4' + | 'f4' + | 'g4' + | 'h4' + | 'a3' + | 'b3' + | 'c3' + | 'd3' + | 'e3' + | 'f3' + | 'g3' + | 'h3' + | 'a2' + | 'b2' + | 'c2' + | 'd2' + | 'e2' + | 'f2' + | 'g2' + | 'h2' + | 'a1' + | 'b1' + | 'c1' + | 'd1' + | 'e1' + | 'f1' + | 'g1' + | 'h1'; + +/** + * - "p" for Pawn + * - "n" for Knight + * - "b" for Bishop + * - "r" for Rook + * - "q" for Queen + * - "k" for King + */ +export type PieceType = 'p' | 'n' | 'b' | 'r' | 'q' | 'k'; /** * Partial data about a chess move including the from and to square, and if a @@ -37,12 +104,8 @@ export interface ShortMove { /** * If this move results in a promotion, this will have the unit promotion. - * - "n" for Knight - * - "b" for Bishop - * - "r" for Rook - * - "q" for Queen */ - promotion?: "n" | "b" | "r" | "q"; + promotion?: Exclude; } /** @@ -54,108 +117,147 @@ export interface Move extends ShortMove { * - "b" for Black * - "w" for White */ - color: "b" | "w"; + color: 'b' | 'w'; /** Flags indicating what occurred, combined into one string */ flags: string; /** * The type of the piece that moved - * - "p" for Pawn - * - "n" for Knight - * - "b" for Bishop - * - "r" for Rook - * - "q" for Queen - * - "k" for King */ - piece: "p" | "n" | "b" | "r" | "q" | "k"; + piece: PieceType; /** The Standard Algebraic Notation (SAN) representation of the move */ san: string; /** - * If an enemy piece was captured this is their type. - * - "p" for Pawn - * - "n" for Knight - * - "b" for Bishop - * - "r" for Rook - * - "q" for Queen + * If an enemy piece was captured this is their type */ - captured?: "p" | "n" | "b" | "r" | "q"; + captured?: Exclude; } export interface Piece { /** * The type of the piece to place - * - "p" for Pawn - * - "n" for Knight - * - "b" for Bishop - * - "r" for Rook - * - "q" for Queen - * - "k" for King */ - type: "p" | "n" | "b" | "r" | "q" | "k"; + type: PieceType; /** * The color of the piece * - "b" for Black * - "w" for White */ - color: "b" | "w"; + color: 'b' | 'w'; } export interface ChessInstance { /** The string that represents the White color side */ - readonly WHITE: "w"; + readonly WHITE: 'w'; /** The string that represents the Black color side */ - readonly BLACK: "b"; + readonly BLACK: 'b'; /** The string that represents a Pawn */ - readonly PAWN: "p"; + readonly PAWN: 'p'; /** The string that represents a Knight */ - readonly KNIGHT: "n"; + readonly KNIGHT: 'n'; /** The string that represents a Bishop */ - readonly BISHOP: "b"; + readonly BISHOP: 'b'; /** The string that represents a Rook */ - readonly ROOK: "r"; + readonly ROOK: 'r'; /** The string that represents a Queen */ - readonly QUEEN: "q"; + readonly QUEEN: 'q'; /** The string that represents a King */ - readonly KING: "k"; + readonly KING: 'k'; /** A list of all the squares in the game, from "a1" to "h8" */ readonly SQUARES: [ - "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8", - "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7", - "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6", - "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5", - "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4", - "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3", - "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2", - "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1" + 'a8', + 'b8', + 'c8', + 'd8', + 'e8', + 'f8', + 'g8', + 'h8', + 'a7', + 'b7', + 'c7', + 'd7', + 'e7', + 'f7', + 'g7', + 'h7', + 'a6', + 'b6', + 'c6', + 'd6', + 'e6', + 'f6', + 'g6', + 'h6', + 'a5', + 'b5', + 'c5', + 'd5', + 'e5', + 'f5', + 'g5', + 'h5', + 'a4', + 'b4', + 'c4', + 'd4', + 'e4', + 'f4', + 'g4', + 'h4', + 'a3', + 'b3', + 'c3', + 'd3', + 'e3', + 'f3', + 'g3', + 'h3', + 'a2', + 'b2', + 'c2', + 'd2', + 'e2', + 'f2', + 'g2', + 'h2', + 'a1', + 'b1', + 'c1', + 'd1', + 'e1', + 'f1', + 'g1', + 'h1', ]; /** Flags used to build flag strings for moves */ readonly FLAGS: { /** a non-capture */ - NORMAL: "n", + NORMAL: 'n'; /** a standard capture */ - CAPTURE: "c", + CAPTURE: 'c'; /** a pawn push of two squares */ - BIG_PAWN: "b", + BIG_PAWN: 'b'; /** an en passant capture */ - EP_CAPTURE: "e", + EP_CAPTURE: 'e'; /** a promotion */ - PROMOTION: "p", + PROMOTION: 'p'; /** kingside castling */ - KSIDE_CASTLE: "k", + KSIDE_CASTLE: 'k'; /** queenside castling */ - QSIDE_CASTLE: "q", + QSIDE_CASTLE: 'q'; }; /** @@ -285,7 +387,9 @@ export interface ChessInstance { * within the FEN string. * @param fen the fen formatted string to validate */ - validate_fen(fen: string): { + validate_fen( + fen: string, + ): { /** Indicates if the fen is valid or not. */ valid: boolean; @@ -318,7 +422,7 @@ export interface ChessInstance { */ pgn(options?: { /** the maximum width of a line */ - max_width?: number, + max_width?: number; /** Specific newline character */ newline_char?: string; }): string; @@ -331,28 +435,31 @@ export interface ChessInstance { * @returns The method will return true if the PGN was parsed successfully, * otherwise false. */ - load_pgn(pgn: string, options?: { - /** - * The newline_char is a string representation of a valid RegExp - * fragment and is used to process the PGN. - * It defaults to \r?\n. - * Special characters should not be pre-escaped, but any literal - * special characters should be escaped as is normal for a RegExp. - * Keep in mind that backslashes in JavaScript strings must - * themselves be escaped. - * Avoid using a newline_char that may occur elsewhere in a PGN, - * such as . or x, as this will result in unexpected behavior. - */ - newline_char?: string; + load_pgn( + pgn: string, + options?: { + /** + * The newline_char is a string representation of a valid RegExp + * fragment and is used to process the PGN. + * It defaults to \r?\n. + * Special characters should not be pre-escaped, but any literal + * special characters should be escaped as is normal for a RegExp. + * Keep in mind that backslashes in JavaScript strings must + * themselves be escaped. + * Avoid using a newline_char that may occur elsewhere in a PGN, + * such as . or x, as this will result in unexpected behavior. + */ + newline_char?: string; - /** - * The sloppy flag is a boolean that permits chess.js to parse moves in - * non-standard notations. - * See .move documentation for more information about non-SAN - * notations. - */ - sloppy?: boolean; - }): boolean; + /** + * The sloppy flag is a boolean that permits chess.js to parse moves in + * non-standard notations. + * See .move documentation for more information about non-SAN + * notations. + */ + sloppy?: boolean; + }, + ): boolean; /** * Allows header information to be added to PGN output. @@ -373,7 +480,7 @@ export interface ChessInstance { * Returns the current side to move. * @returns "b" if Black is the side to move, otherwise "w" for White. */ - turn(): "b" | "w"; + turn(): 'b' | 'w'; /** * Attempts to make a move on the board, returning a move object if the @@ -391,13 +498,16 @@ export interface ChessInstance { * and the chess board's state changes. * If the move was invalid, null is returned and the state does not update. */ - move(move: string | ShortMove, options?: { - /** - * An optional sloppy flag can be used to parse a variety of - * non-standard move notations. - */ - sloppy?: boolean; - }): Move | null; + move( + move: string | ShortMove, + options?: { + /** + * An optional sloppy flag can be used to parse a variety of + * non-standard move notations. + */ + sloppy?: boolean; + }, + ): Move | null; /** * Take back the last half-move, returning a move object if successful, @@ -449,7 +559,7 @@ export interface ChessInstance { * @returns "light" if a light square, "dark" if a dark square, or null if * not a valid square. */ - square_color(square: Square): "light" | "dark"; + square_color(square: Square): 'light' | 'dark'; /** * Returns the color of the square ('light' or 'dark'). @@ -457,7 +567,7 @@ export interface ChessInstance { * @returns "light" if a light square, "dark" if a dark square, or null if * not a valid square. */ - square_color(square: string): "light" | "dark" | null; + square_color(square: string): 'light' | 'dark' | null; /** * Returns a list containing the moves of the current game. @@ -506,6 +616,8 @@ export interface ChessInstance { */ verbose?: boolean; }): string[] | Move[]; + + board(): Array>; } /**