Skip to content

Commit

Permalink
Add skipValidation option to .load()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhlywa committed Dec 17, 2023
1 parent 5e20d86 commit e2ff91c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ chess.isThreefoldRepetition()
// -> true
```

### .load(fen: string, { preserveHeaders = false } = {})
### .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 omitted. Throws an
Expand All @@ -538,11 +538,14 @@ const chess = new Chess()
chess.load('4r3/8/2p2PPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45')

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!
```

### .loadPgn(pgn, [ options ])
Expand Down
19 changes: 19 additions & 0 deletions __tests__/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ test('load - bad piece (X)', () => {
expect(() => chess.load(fen)).toThrow()
})

test('load - missing white king', () => {
const chess = new Chess()
const fen = '1nbqkbn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQ1BN1 b - - 1 2'
expect(() => chess.load(fen)).toThrow()
})

test('load - missing black king', () => {
const chess = new Chess()
const fen = '1nbq1bn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQKBN1 b - - 1 2'
expect(() => chess.load(fen)).toThrow()
})

test('load - bad ep square (e9)', () => {
const chess = new Chess()
const fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e9 0 1'
Expand Down Expand Up @@ -61,3 +73,10 @@ test('load - preserveHeaders = true', () => {
Black: 'Viswanathan Anand',
})
})

test('load - skipValidation = true', () => {
const chess = new Chess()
// missing white king
const fen = '1nbqkbn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQ1BN1 b - - 1 2'
expect(() => chess.load(fen, { skipValidation: true })).not.toThrow()
})
10 changes: 6 additions & 4 deletions src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ export class Chess {
}
}

load(fen: string, { preserveHeaders = false } = {}) {
load(fen: string, { skipValidation = false, preserveHeaders = false } = {}) {
let tokens = fen.split(/\s+/)

// append commonly omitted fen tokens
Expand All @@ -609,9 +609,11 @@ export class Chess {

tokens = fen.split(/\s+/)

const { ok, error } = validateFen(fen)
if (!ok) {
throw new Error(error)
if (!skipValidation) {
const { ok, error } = validateFen(fen)
if (!ok) {
throw new Error(error)
}
}

const position = tokens[0]
Expand Down

0 comments on commit e2ff91c

Please sign in to comment.