chess-moments 1.4.6 → 1.4.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.
@@ -8,9 +8,9 @@ const getNextMoments = (moments, current) => {
8
8
  const next = [];
9
9
 
10
10
  // Get next moments for further processing
11
- const nextIndex = moments.indexOf(current) + 1;
12
- const nextMoment = moments[nextIndex];
13
- const nextNextMoment = moments[nextIndex + 1];
11
+ const currentIndex = moments.findIndex((item) => item.fen === current.fen);
12
+ const nextMoment = moments[currentIndex + 1];
13
+ const nextNextMoment = moments[currentIndex + 2];
14
14
 
15
15
  // Early returns for edge cases
16
16
  if (!nextMoment) {
@@ -3,7 +3,9 @@
3
3
  */
4
4
  const getPrevMoment = (moments, current) => {
5
5
  try {
6
- const sameDepth = moments[moments.indexOf(current) - 1];
6
+ const currentIndex = moments.findIndex((item) => item.fen === current.fen);
7
+
8
+ const sameDepth = moments[currentIndex - 1];
7
9
  if (sameDepth.move && sameDepth.depth === current.depth) {
8
10
  return sameDepth;
9
11
  }
@@ -13,16 +15,13 @@ const getPrevMoment = (moments, current) => {
13
15
  return moments[0];
14
16
  }
15
17
 
16
- const prevIndex = moments.indexOf(current) - 1;
17
- const prevMoments = moments[prevIndex];
18
-
19
18
  // If previous moment does not have a move and it's the initial position, return it
19
+ const prevMoments = moments[currentIndex - 1];
20
20
  if (!prevMoments?.move && prevMoments.fen === moments[0].fen) {
21
21
  return moments[0];
22
22
  }
23
23
 
24
24
  // Split moments until the current index
25
- const currentIndex = moments.indexOf(current);
26
25
  const moves = moments.slice(0, currentIndex).filter((m) => m.move);
27
26
 
28
27
  // Add fullmove number to the current moment
@@ -1,3 +1,4 @@
1
+ const { isEmpty } = require('lodash');
1
2
  const fen = require('../fen');
2
3
  const { getBrushCode } = require('../helpers');
3
4
 
@@ -10,7 +11,7 @@ const momentsToPgn = (moments) => {
10
11
  let pgn = '';
11
12
 
12
13
  // Return empty PGN if no moments are provided
13
- if (!Array.isArray(moments) || moments.length === 0) {
14
+ if (isEmpty(moments)) {
14
15
  pgn += `[SetUp "1"]\n`;
15
16
  pgn += `[FEN "${fen.initial}"]\n\n`;
16
17
  pgn += '*';
@@ -20,12 +21,9 @@ const momentsToPgn = (moments) => {
20
21
  let currentDepth = 1;
21
22
  let variationStack = [];
22
23
 
23
- // Check if we need to add FEN headers for non-standard starting position
24
- const initialMoment = moments[0];
25
- if (initialMoment && initialMoment.fen && initialMoment.fen !== fen.initial) {
26
- pgn += `[SetUp "1"]\n`;
27
- pgn += `[FEN "${initialMoment.fen}"]\n\n`;
28
- }
24
+ // Add FEN headers
25
+ pgn += `[SetUp "1"]\n`;
26
+ pgn += `[FEN "${moments[0].fen}"]\n\n`;
29
27
 
30
28
  for (let i = 0; i < moments.length; i++) {
31
29
  const moment = moments[i];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chess-moments",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "index.js",
6
6
  "files": [