chess-moments 0.14.7 → 0.14.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ const { getMoveNumber } = require('../helpers');
2
+
1
3
  /**
2
4
  * Always returns an array because there can be more than one next moment
3
5
  */
@@ -10,21 +12,42 @@ const getNextMoments = (moments, current) => {
10
12
  return [moments[1]];
11
13
  }
12
14
 
13
- // If there is no next moment, return empty array
15
+ // Get next moments for further processing
14
16
  const nextIndex = moments.indexOf(current) + 1;
15
17
  const nextMoment = moments[nextIndex];
18
+ const nextNextMoment = moments[nextIndex + 1];
19
+
20
+ // Early returns for edge cases
16
21
  if (!nextMoment) {
17
22
  return [];
18
23
  }
19
24
 
25
+ // Last move of a sideline should not have any next moves
26
+ if (
27
+ !nextMoment?.move &&
28
+ current.depth >= nextMoment.depth &&
29
+ getMoveNumber(nextMoment.fen) <= getMoveNumber(current.fen)
30
+ ) {
31
+ return [];
32
+ }
33
+
20
34
  // Add the next moment if it has a move and is at the same depth
21
35
  if (nextMoment.move && current.depth === nextMoment.depth) {
22
36
  next.push(nextMoment);
23
37
  }
24
38
 
25
- // If the next next moment has a move, we don't have any other sidelines
26
- const nextNextMoment = moments[nextIndex + 1];
27
- if (nextMoment?.move && nextNextMoment?.move) {
39
+ // Return early if we have consecutive moves or no next next moment
40
+ if (!nextNextMoment || (nextMoment?.move && nextNextMoment?.move)) {
41
+ return next;
42
+ }
43
+
44
+ // Second to last move of a sideline should not have any next moves
45
+ if (
46
+ nextMoment?.move &&
47
+ !nextNextMoment?.move &&
48
+ current.depth >= nextNextMoment.depth &&
49
+ getMoveNumber(nextNextMoment.fen) <= getMoveNumber(current.fen)
50
+ ) {
28
51
  return next;
29
52
  }
30
53
 
@@ -0,0 +1,8 @@
1
+ const getMoveNumber = (fen) => {
2
+ // FEN format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
3
+ // The fullmove number is the sixth part of the FEN string
4
+ const parts = fen.split(' ');
5
+ return Number(parts[5]);
6
+ };
7
+
8
+ module.exports = getMoveNumber;
@@ -1,7 +1,9 @@
1
1
  const getBrushCode = require('./get-brush-code');
2
2
  const getBrushColor = require('./get-brush-color');
3
+ const getMoveNumber = require('./get-move-number');
3
4
 
4
5
  module.exports = {
5
6
  getBrushCode,
6
7
  getBrushColor,
8
+ getMoveNumber,
7
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chess-moments",
3
- "version": "0.14.7",
3
+ "version": "0.14.9",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "dist/index.js",
6
6
  "files": [