Comparing version 1.0.0-beta.6 to 1.0.0-beta.7
@@ -43,11 +43,2 @@ /** | ||
}; | ||
declare type InternalMove = { | ||
color: Color; | ||
from: number; | ||
to: number; | ||
piece: PieceSymbol; | ||
captured?: PieceSymbol; | ||
promotion?: PieceSymbol; | ||
flags: number; | ||
}; | ||
export declare type Move = { | ||
@@ -85,6 +76,12 @@ color: Color; | ||
private _castling; | ||
private _positionCounts; | ||
constructor(fen?: string); | ||
clear(keepHeaders?: boolean): void; | ||
clear({ preserveHeaders }?: { | ||
preserveHeaders?: boolean | undefined; | ||
}): void; | ||
removeHeader(key: string): void; | ||
load(fen: string, keepHeaders?: boolean): void; | ||
load(fen: string, { skipValidation, preserveHeaders }?: { | ||
skipValidation?: boolean | undefined; | ||
preserveHeaders?: boolean | undefined; | ||
}): void; | ||
fen(): string; | ||
@@ -98,6 +95,7 @@ private _updateSetup; | ||
}, square: Square): boolean; | ||
private _put; | ||
remove(square: Square): Piece; | ||
_updateCastlingRights(): void; | ||
_updateEnPassantSquare(): void; | ||
_attacked(color: Color, square: number): boolean; | ||
private _updateCastlingRights; | ||
private _updateEnPassantSquare; | ||
private _attacked; | ||
private _isKingAttacked; | ||
@@ -110,2 +108,3 @@ isAttacked(square: Square, attackedBy: Color): boolean; | ||
isInsufficientMaterial(): boolean; | ||
private _getRepetitionCount; | ||
isThreefoldRepetition(): boolean; | ||
@@ -168,7 +167,3 @@ isDraw(): boolean; | ||
}): Move[]; | ||
_moves({ legal, piece, square, }?: { | ||
legal?: boolean; | ||
piece?: PieceSymbol; | ||
square?: Square; | ||
}): InternalMove[]; | ||
private _moves; | ||
move(move: string | { | ||
@@ -181,3 +176,3 @@ from: string; | ||
}): Move; | ||
_push(move: InternalMove): void; | ||
private _push; | ||
private _makeMove; | ||
@@ -236,2 +231,1 @@ undo(): Move | null; | ||
} | ||
export {}; |
{ | ||
"name": "chess.js", | ||
"version": "1.0.0-beta.6", | ||
"version": "1.0.0-beta.7", | ||
"license": "BSD-2-Clause", | ||
@@ -11,6 +11,9 @@ "main": "dist/cjs/chess.js", | ||
"scripts": { | ||
"test": "jest", | ||
"build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json", | ||
"check": "npm run format:check && npm run lint && npm run test && npm run build", | ||
"clean": "rm -rf ./dist", | ||
"format": "prettier --write .", | ||
"format:check": "prettier --check .", | ||
"lint": "eslint src/ --ext .ts", | ||
"build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json", | ||
"clean": "rm -rf ./dist" | ||
"test": "jest" | ||
}, | ||
@@ -28,2 +31,3 @@ "repository": { | ||
"jest-extended": "^2.0.0", | ||
"prettier": "^3.1.0", | ||
"ts-jest": "^27.0.4", | ||
@@ -30,0 +34,0 @@ "typescript": "^4.6.3" |
122
README.md
# chess.js | ||
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/jhlywa/chess.js/node.js.yml) | ||
![npm](https://img.shields.io/npm/v/chess.js?color=blue) | ||
![npm](https://img.shields.io/npm/dm/chess.js) | ||
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/jhlywa/chess.js/node.js.yml)](https://github.com/jhlywa/chess.js/actions) | ||
[![npm](https://img.shields.io/npm/v/chess.js?color=blue)](https://www.npmjs.com/package/chess.js) | ||
[![npm](https://img.shields.io/npm/dm/chess.js)](https://www.npmjs.com/package/chess.js) | ||
@@ -22,5 +22,5 @@ chess.js is a TypeScript chess library used for chess move | ||
## Import into your project | ||
## Importing | ||
### Modern way (ESM) | ||
### Import (as ESM) | ||
@@ -31,3 +31,3 @@ ```js | ||
If you want to use it in a browser you can import as module like this: | ||
ECMAScript modules (ESM) can be directly imported in a browser: | ||
@@ -40,3 +40,3 @@ ```html | ||
### Old way (CommonJS) | ||
### Import (as CommonJS) | ||
@@ -52,2 +52,4 @@ ```js | ||
```js | ||
import { Chess } from 'chess.js' | ||
const chess = new Chess() | ||
@@ -65,3 +67,3 @@ | ||
By design chess.js is a headless library and does not include user interface | ||
By design, chess.js is a headless library and does not include user interface | ||
elements. Many developers have successfully integrated chess.js with the | ||
@@ -72,14 +74,38 @@ [chessboard.js](http://chessboardjs.com) library. See | ||
## Move & PGN Parsers | ||
## Parsers (permissive / strict) | ||
This library includes two parsers (`permissive` and `strict`) which are used to | ||
parse different forms of chess move notation. The `permissive` parser (the | ||
default) is able to handle many derivates of algebraic notation (e.g. `Nf3`, | ||
`g1f3`, `g1-f3`, `Ng1f3`, `Ng1-f3`, `Ng1xf3`). The `strict` parser only accepts | ||
moves in Standard Algebraic Notation and requires that they strictly adhere to | ||
the specification. The `strict` parser runs slightly faster but is much less | ||
forgiving of non-standard notation. | ||
default) is able to handle many non-standard derivatives of algebraic notation | ||
(e.g. `Nf3`, `g1f3`, `g1-f3`, `Ng1f3`, `Ng1-f3`, `Ng1xf3`). The `strict` parser | ||
only accepts moves in Standard Algebraic Notation and requires that they | ||
strictly adhere to the specification. The `strict` parser runs slightly faster | ||
but will not parse any non-standard notation. | ||
## API | ||
### Constants | ||
The following constants are exported from the top-level module: | ||
```ts | ||
// colors | ||
export const WHITE = 'w' | ||
export const BLACK = 'b' | ||
// pieces | ||
export const PAWN = 'p' | ||
export const KNIGHT = 'n' | ||
export const BISHOP = 'b' | ||
export const ROOK = 'r' | ||
export const QUEEN = 'q' | ||
export const KING = 'k' | ||
// starting position (in FEN) | ||
export const DEFAULT_POSITION = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' | ||
// square list | ||
export const SQUARES = ['a8', 'b8', 'c8', ..., 'f1', 'g1', 'h1'] | ||
``` | ||
### Constructor: Chess([ fen ]) | ||
@@ -93,8 +119,10 @@ | ||
```ts | ||
// board defaults to the starting position when called with no parameters | ||
const chess = new Chess() | ||
import { Chess } from 'chess.js' | ||
// an empty constructor defaults the starting position | ||
let chess = new Chess() | ||
// pass in a FEN string to load a particular position | ||
const chess = new Chess( | ||
'r1k4r/p2nb1p1/2b4p/1p1n1p2/2PP4/3Q1NB1/1P3PPP/R5K1 b - - 0 19' | ||
let chess = new Chess( | ||
'r1k4r/p2nb1p1/2b4p/1p1n1p2/2PP4/3Q1NB1/1P3PPP/R5K1 b - - 0 19', | ||
) | ||
@@ -161,3 +189,3 @@ ``` | ||
### .clear() | ||
### .clear({ preserveHeaders = false } = {}) | ||
@@ -199,3 +227,3 @@ Clears the board. | ||
chess.loadPgn( | ||
"1. e4 e5 {king's pawn opening} 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *" | ||
"1. e4 e5 {king's pawn opening} 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *", | ||
) | ||
@@ -241,4 +269,3 @@ | ||
```ts | ||
chess.clear() | ||
chess.put({ type: chess.PAWN, color: chess.BLACK }, 'a5') // put a black pawn on a5 | ||
chess.put({ type: PAWN, color: BLACK }, 'a5') // put a black pawn on a5 | ||
@@ -286,3 +313,3 @@ chess.get('a5') | ||
chess.loadPgn( | ||
"1. e4 e5 {king's pawn opening} 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *" | ||
"1. e4 e5 {king's pawn opening} 2. Nf3 Nc6 3. Bc4 Bc5 {giuoco piano} *", | ||
) | ||
@@ -399,3 +426,3 @@ | ||
const chess = new Chess( | ||
'rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3' | ||
'rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3', | ||
) | ||
@@ -412,14 +439,14 @@ chess.inCheck() | ||
const chess = new Chess() | ||
chess.isAttacked('f3', Chess.WHITE) | ||
chess.isAttacked('f3', WHITE) | ||
// -> true (we can attack empty squares) | ||
chess.isAttacked('f6', Chess.BLACK) | ||
chess.isAttacked('f6', BLACK) | ||
// -> true (side to move (e.g. the value returned by .turn) is ignored) | ||
chess.load(Chess.DEFAULT_POSITION) | ||
chess.isAttacked('e2', Chess.WHITE) | ||
chess.load(DEFAULT_POSITION) | ||
chess.isAttacked('e2', WHITE) | ||
// -> true (we can attack our own pieces) | ||
chess.load('4k3/4n3/8/8/8/8/4R3/4K3 w - - 0 1') | ||
chess.isAttacked('c6', Chess.BLACK) | ||
chess.isAttacked('c6', BLACK) | ||
// -> true (pieces still attack a square if even they are pinned) | ||
@@ -434,3 +461,3 @@ ``` | ||
const chess = new Chess( | ||
'rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3' | ||
'rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3', | ||
) | ||
@@ -517,7 +544,7 @@ chess.isCheckmate() | ||
### .load(fen) | ||
### .load(fen: string, { skipValidation = false, preserveHeaders = false } = {}) | ||
Clears the board and loads the provided FEN string. The castling rights, en | ||
passant square and move numbers are defaulted to `- - 0 1` if ommitted. Throws | ||
an exception if the FEN is invalid. | ||
passant square and move numbers are defaulted to `- - 0 1` if omitted. Throws an | ||
exception if the FEN is invalid. | ||
@@ -529,7 +556,10 @@ ```ts | ||
try { | ||
chess.load('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45') | ||
chess.load('8/4p3/8/8/8/8/4P3/6K1 w - - 1 45') | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
// -> Error: Invalid FEN: piece data is invalid (invalid piece) | ||
// -> Error: Invalid FEN: missing black king | ||
chess.load('8/4p3/8/8/8/8/4P3/6K1 w - - 1 45', { skipValidation: true }) | ||
// -> Works! | ||
``` | ||
@@ -693,3 +723,3 @@ | ||
### .moves({ piece?: Piece, square?: Square, verbose?: Boolean }?) | ||
### .moves({ piece?: Piece, square?: Square, verbose = false} = {}) | ||
@@ -782,3 +812,3 @@ Returns a list of legal moves from the current position. This function takes an | ||
chess.put({ type: chess.PAWN, color: chess.BLACK }, 'a5') // put a black pawn on a5 | ||
chess.put({ type: PAWN, color: BLACK }, 'a5') // put a black pawn on a5 | ||
// -> true | ||
@@ -809,4 +839,4 @@ chess.put({ type: 'k', color: 'w' }, 'h1') // shorthand | ||
chess.clear() | ||
chess.put({ type: chess.PAWN, color: chess.BLACK }, 'a5') // put a black pawn on a5 | ||
chess.put({ type: chess.KING, color: chess.WHITE }, 'h1') // put a white king on h1 | ||
chess.put({ type: PAWN, color: BLACK }, 'a5') // put a black pawn on a5 | ||
chess.put({ type: KING, color: WHITE }, 'h1') // put a white king on h1 | ||
@@ -834,3 +864,3 @@ chess.remove('a5') | ||
// white can't castle kingside but can castle queenside | ||
chess.setCastlingRights(WHITE, { [chess.KING]: false, [chess.QUEEN]: true }) | ||
chess.setCastlingRights(WHITE, { [KING]: false, [QUEEN]: true }) | ||
``` | ||
@@ -909,14 +939,16 @@ | ||
### .validateFen(fen): | ||
### validateFen(fen): | ||
Returns a validation object specifying validity or the errors found within the | ||
FEN string. | ||
This static function returns a validation object specifying validity or the | ||
errors found within the FEN string. | ||
```ts | ||
chess.validateFen('2n1r3/p1k2pp1/B1p3b1/P7/5bP1/2N1B3/1P2KP2/2R5 b - - 4 25') | ||
import { validateFen } from 'chess.js' | ||
validateFen('2n1r3/p1k2pp1/B1p3b1/P7/5bP1/2N1B3/1P2KP2/2R5 b - - 4 25') | ||
// -> { ok: true } | ||
chess.validateFen('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45') | ||
validateFen('4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45') | ||
// -> { ok: false, | ||
// error: '1st field (piece positions) is invalid [invalid piece].' } | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
367088
6158
934
0
9