Skip to content

Commit b51b44e

Browse files
committed
Fix a bug in validate_fen.
When the move number field is empty (trailing space at the end), the validation returned valid since isNaN('') is false and (parseInt('') <= 0) is NaN <= 0, which is false. Added the new (previously failing) test case.
1 parent 426e7ed commit b51b44e

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

__tests__/chess.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,10 @@ describe('Validate FEN', () => {
13711371
fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 x',
13721372
error_number: 2,
13731373
},
1374+
{
1375+
fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 ',
1376+
error_number: 2,
1377+
},
13741378
{
13751379
fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0',
13761380
error_number: 2,

chess.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,12 @@ export const Chess = function (fen) {
432432
}
433433

434434
/* 2nd criterion: move number field is a integer value > 0? */
435-
if (isNaN(tokens[5]) || parseInt(tokens[5], 10) <= 0) {
435+
if (isNaN(parseInt(tokens[5])) || parseInt(tokens[5], 10) <= 0) {
436436
return { valid: false, error_number: 2, error: errors[2] }
437437
}
438438

439439
/* 3rd criterion: half move counter is an integer >= 0? */
440-
if (isNaN(tokens[4]) || parseInt(tokens[4], 10) < 0) {
440+
if (isNaN(parseInt(tokens[4])) || parseInt(tokens[4], 10) < 0) {
441441
return { valid: false, error_number: 3, error: errors[3] }
442442
}
443443

0 commit comments

Comments
 (0)