chess-moments 0.14.6 → 0.14.8

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,32 +12,49 @@ const getNextMoments = (moments, current) => {
10
12
  return [moments[1]];
11
13
  }
12
14
 
13
- // Split moments after the current index
14
- const currentIndex = moments.indexOf(current);
15
- let moves = moments.slice(currentIndex + 1).filter((m) => m.move);
15
+ // If there is no next moment, return empty array
16
+ const nextIndex = moments.indexOf(current) + 1;
17
+ const nextMoment = moments[nextIndex];
18
+ if (!nextMoment) {
19
+ return [];
20
+ }
21
+ // Last move of a sideline should not have any next moves
22
+ if (
23
+ !nextMoment?.move &&
24
+ current.depth >= nextMoment.depth &&
25
+ getMoveNumber(nextMoment.fen) <= getMoveNumber(current.fen)
26
+ ) {
27
+ return [];
28
+ }
16
29
 
17
- // If the immediate next moment has increased depth, filter out all moments until depth returns to current
18
- if (moves.length > 0 && moves[0].depth > current.depth) {
19
- let foundReturnToCurrentDepth = false;
20
- moves = moves.filter((moment) => {
21
- if (foundReturnToCurrentDepth) {
22
- return true; // Keep all moments after we return to current depth
23
- }
24
- if (moment.depth === current.depth) {
25
- foundReturnToCurrentDepth = true;
26
- return true; // Keep this moment and all following
27
- }
28
- return false; // Skip moments with increased depth
29
- });
30
+ // Add the next moment if it has a move and is at the same depth
31
+ if (nextMoment.move && current.depth === nextMoment.depth) {
32
+ next.push(nextMoment);
33
+ }
34
+
35
+ // If the next next moment has a move, we don't have any other sidelines
36
+ const nextNextMoment = moments[nextIndex + 1];
37
+ if (nextMoment?.move && nextNextMoment?.move) {
38
+ return next;
30
39
  }
31
40
 
32
41
  // Add fullmove number to the current moment
33
42
  current.fullmove = Number(current.fen.split(' ')[5]);
34
43
 
44
+ // Keep only slim moments
45
+ const slim = moments.filter((moment) => {
46
+ const fullmove = Number(moment.fen.split(' ')[5]);
47
+ return (
48
+ moment.index > current.index + 1 &&
49
+ fullmove <= current.fullmove + 1 &&
50
+ moment.move // Also filter out moments without a move
51
+ );
52
+ });
53
+
35
54
  // Active color after current move (who moves next)
36
55
  const activeColorAfterCurrent = current.fen.split(' ')[1];
37
56
 
38
- for (const moment of moves) {
57
+ for (const moment of slim) {
39
58
  // Only process main line and immediate variations
40
59
  if (moment.depth > current.depth + 1) {
41
60
  continue;
@@ -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.6",
3
+ "version": "0.14.8",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "dist/index.js",
6
6
  "files": [