Skip to content

Commit

Permalink
Return undefined when get/remove called on empty square
Browse files Browse the repository at this point in the history
  • Loading branch information
jhlywa committed Oct 26, 2024
1 parent 4d4928d commit 85c049e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ chess.fen()

### .get(square)

Returns the piece on the square:
Returns the piece on the square. Returns `undefined` if the square is empty.

```ts
chess.put({ type: PAWN, color: BLACK }, 'a5') // put a black pawn on a5

chess.get('a5')
// -> { type: 'p', color: 'b' },
chess.get('a6')
// -> null
// -> undefined
```

### .getCastlingRights(color)
Expand Down Expand Up @@ -844,7 +844,8 @@ chess.put({ type: 'k', color: 'w' }, 'h1') // fail - two kings

### .remove(square)

Remove and return the piece on _square_.
Remove and return the piece on _square_. Returns `undefined` if the square is
already empty.

```ts
chess.clear()
Expand All @@ -856,7 +857,7 @@ chess.remove('a5')
chess.remove('h1')
// -> { type: 'k', color: 'w' },
chess.remove('e1')
// -> null
// -> undefined
```

### .reset()
Expand Down
8 changes: 4 additions & 4 deletions __tests__/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ test('get', () => {
// expect(chess.get('A7' as Square)).toEqual({ type: PAWN, color: BLACK })
// })

test('get - returns false for empty square', () => {
test('get - returns undefined for empty square', () => {
const chess = new Chess()
expect(chess.get('a4')).toEqual(false)
expect(chess.get('a4')).toEqual(undefined)
})

test('get - returns false for invalid square', () => {
test('get - returns undefined for invalid square', () => {
const chess = new Chess()
expect(chess.get('bad-square' as Square)).toEqual(false)
expect(chess.get('bad-square' as Square)).toEqual(undefined)
})
10 changes: 5 additions & 5 deletions __tests__/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import {
test('remove - returns piece', () => {
const chess = new Chess()
expect(chess.remove('d1')).toEqual({ type: QUEEN, color: WHITE })
expect(chess.get('d1')).toEqual(false)
expect(chess.get('d1')).toEqual(undefined)
})

test('remove - returns false for empty square', () => {
test('remove - returns undefined for empty square', () => {
const chess = new Chess()
expect(chess.remove('e4')).toEqual(false)
expect(chess.remove('e4')).toEqual(undefined)
})

test('remove - returns false for invalid square', () => {
test('remove - returns undefined for invalid square', () => {
const chess = new Chess()
expect(chess.remove('bad_square' as Square)).toEqual(false)
expect(chess.remove('bad_square' as Square)).toEqual(undefined)
})

test('remove - removing white kingside rook loses castling right', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,8 @@ export class Chess {
this.load(DEFAULT_POSITION)
}

get(square: Square) {
return this._board[Ox88[square]] || false
get(square: Square): Piece | undefined {
return this._board[Ox88[square]]
}

put({ type, color }: { type: PieceSymbol; color: Color }, square: Square) {
Expand Down Expand Up @@ -822,7 +822,7 @@ export class Chess {
return true
}

remove(square: Square) {
remove(square: Square): Piece | undefined {
const piece = this.get(square)
delete this._board[Ox88[square]]
if (piece && piece.type === KING) {
Expand Down

0 comments on commit 85c049e

Please sign in to comment.