chess-moments 0.14.2 → 0.14.4

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.
@@ -4,58 +4,63 @@
4
4
  const getNextMoments = (moments, current) => {
5
5
  try {
6
6
  const next = [];
7
- const sameDepth = moments[moments.indexOf(current) + 1];
8
- if (sameDepth.move && sameDepth.depth === current.depth) {
9
- next.push(sameDepth);
10
- }
11
7
 
12
8
  // If it's the first moment without move, add the next moment
13
9
  if (current.index === 0 && !current.move) {
14
- next.push(moments[1]);
10
+ return moments[1];
15
11
  }
16
12
 
17
13
  // Split moments after the current index
18
14
  const currentIndex = moments.indexOf(current);
19
- const moves = moments.slice(currentIndex + 1).filter((m) => m.move);
15
+ let moves = moments.slice(currentIndex + 1).filter((m) => m.move);
16
+
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
+ }
20
31
 
21
32
  // Add fullmove number to the current moment
22
33
  current.fullmove = Number(current.fen.split(' ')[5]);
23
34
 
24
- // Active color is white
25
- if (current.fen.split(' ')[1] === 'w') {
26
- for (const moment of moves) {
27
- const activeColor = moment.fen.split(' ')[1];
28
- const fullmove = Number(moment.fen.split(' ')[5]);
35
+ // Active color after current move (who moves next)
36
+ const activeColorAfterCurrent = current.fen.split(' ')[1];
29
37
 
30
- // Check if the next moment is a valid move for white
31
- if (
32
- moment.depth === current.depth + 1 &&
33
- activeColor === 'b' &&
34
- fullmove === current.fullmove
35
- ) {
36
- next.push(moment);
37
- }
38
+ for (const moment of moves) {
39
+ // Only process main line and immediate variations
40
+ if (moment.depth > current.depth + 1) {
41
+ continue;
38
42
  }
39
- }
40
43
 
41
- // Active color is black
42
- if (current.fen.split(' ')[1] === 'b') {
43
- for (const moment of moves) {
44
- const activeColor = moment.fen.split(' ')[1];
45
- const fullmove = Number(moment.fen.split(' ')[5]);
44
+ const activeColorAfterMoment = moment.fen.split(' ')[1];
45
+ const fullmove = Number(moment.fen.split(' ')[5]);
46
46
 
47
- // Check if the next moment is a valid move for black
48
- if (
49
- moment.depth === current.depth + 1 &&
50
- activeColor === 'w' &&
51
- fullmove === current.fullmove + 1
52
- ) {
53
- next.push(moment);
54
- }
47
+ // Check if this is a valid next move based on color and fullmove
48
+ const isValidWhiteMove =
49
+ activeColorAfterCurrent === 'w' &&
50
+ activeColorAfterMoment === 'b' &&
51
+ fullmove === current.fullmove;
52
+
53
+ const isValidBlackMove =
54
+ activeColorAfterCurrent === 'b' &&
55
+ activeColorAfterMoment === 'w' &&
56
+ fullmove === current.fullmove + 1;
57
+
58
+ // If the move is valid, add it to the next moments
59
+ if (isValidWhiteMove || isValidBlackMove) {
60
+ next.push(moment);
55
61
  }
56
62
  }
57
63
 
58
- // If no next moment is found, return an empty array
59
64
  return next;
60
65
  } catch {
61
66
  return [];
@@ -7,11 +7,16 @@ const { getBrushCode } = require('../helpers');
7
7
  * @returns {String} PGN string
8
8
  */
9
9
  const momentsToPgn = (moments) => {
10
+ let pgn = '';
11
+
12
+ // Return empty PGN if no moments are provided
10
13
  if (!Array.isArray(moments) || moments.length === 0) {
11
- return '';
14
+ pgn += `[SetUp "1"]\n`;
15
+ pgn += `[FEN "${initialMoment.fen}"]\n\n`;
16
+ pgn += '*';
17
+ return pgn;
12
18
  }
13
19
 
14
- let pgn = '';
15
20
  let currentDepth = 1;
16
21
  let variationStack = [];
17
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chess-moments",
3
- "version": "0.14.2",
3
+ "version": "0.14.4",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "dist/index.js",
6
6
  "files": [