File tree 3 files changed +31
-7
lines changed
3 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -527,7 +527,7 @@ chess.isThreefoldRepetition()
527
527
// -> true
528
528
```
529
529
530
- ### .load(fen: string, { preserveHeaders = false } = {})
530
+ ### .load(fen: string, { skipValidation = false, preserveHeaders = false } = {})
531
531
532
532
Clears the board and loads the provided FEN string. The castling rights, en
533
533
passant square and move numbers are defaulted to ` - - 0 1 ` if omitted. Throws an
@@ -538,11 +538,14 @@ const chess = new Chess()
538
538
chess .load (' 4r3/8/2p2PPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45' )
539
539
540
540
try {
541
- chess .load (' 4r3/8/X12XPk/1p6/pP2p1R1/P1B5/2P2K2/3r4 w - - 1 45' )
541
+ chess .load (' 8/4p3/8/8/8/8/4P3/6K1 w - - 1 45' )
542
542
} catch (e ) {
543
543
console .log (e )
544
544
}
545
- // -> Error: Invalid FEN: piece data is invalid (invalid piece)
545
+ // -> Error: Invalid FEN: missing black king
546
+
547
+ chess .load (' 8/4p3/8/8/8/8/4P3/6K1 w - - 1 45' , { skipValidation: true })
548
+ // -> Works!
546
549
```
547
550
548
551
### .loadPgn(pgn, [ options ] )
Original file line number Diff line number Diff line change @@ -31,6 +31,18 @@ test('load - bad piece (X)', () => {
31
31
expect ( ( ) => chess . load ( fen ) ) . toThrow ( )
32
32
} )
33
33
34
+ test ( 'load - missing white king' , ( ) => {
35
+ const chess = new Chess ( )
36
+ const fen = '1nbqkbn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQ1BN1 b - - 1 2'
37
+ expect ( ( ) => chess . load ( fen ) ) . toThrow ( )
38
+ } )
39
+
40
+ test ( 'load - missing black king' , ( ) => {
41
+ const chess = new Chess ( )
42
+ const fen = '1nbq1bn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQKBN1 b - - 1 2'
43
+ expect ( ( ) => chess . load ( fen ) ) . toThrow ( )
44
+ } )
45
+
34
46
test ( 'load - bad ep square (e9)' , ( ) => {
35
47
const chess = new Chess ( )
36
48
const fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e9 0 1'
@@ -61,3 +73,10 @@ test('load - preserveHeaders = true', () => {
61
73
Black : 'Viswanathan Anand' ,
62
74
} )
63
75
} )
76
+
77
+ test ( 'load - skipValidation = true' , ( ) => {
78
+ const chess = new Chess ( )
79
+ // missing white king
80
+ const fen = '1nbqkbn1/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/1NBQ1BN1 b - - 1 2'
81
+ expect ( ( ) => chess . load ( fen , { skipValidation : true } ) ) . not . toThrow ( )
82
+ } )
Original file line number Diff line number Diff line change @@ -598,7 +598,7 @@ export class Chess {
598
598
}
599
599
}
600
600
601
- load ( fen : string , { preserveHeaders = false } = { } ) {
601
+ load ( fen : string , { skipValidation = false , preserveHeaders = false } = { } ) {
602
602
let tokens = fen . split ( / \s + / )
603
603
604
604
// append commonly omitted fen tokens
@@ -609,9 +609,11 @@ export class Chess {
609
609
610
610
tokens = fen . split ( / \s + / )
611
611
612
- const { ok, error } = validateFen ( fen )
613
- if ( ! ok ) {
614
- throw new Error ( error )
612
+ if ( ! skipValidation ) {
613
+ const { ok, error } = validateFen ( fen )
614
+ if ( ! ok ) {
615
+ throw new Error ( error )
616
+ }
615
617
}
616
618
617
619
const position = tokens [ 0 ]
You can’t perform that action at this time.
0 commit comments