chess-moments 1.4.1 → 1.4.3

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,6 +4,7 @@ const getNextMoments = require('./get-next-moments');
4
4
  const getPrevMoment = require('./get-prev-moment');
5
5
  const momentsToPgn = require('./moments-to-pgn');
6
6
  const moveTrainer = require('./move-trainer');
7
+ const promoteMainlineTree = require('./promote-mainline-tree');
7
8
  const promoteMainline = require('./promote-mainline');
8
9
 
9
10
  module.exports = {
@@ -13,5 +14,6 @@ module.exports = {
13
14
  getPrevMoment,
14
15
  momentsToPgn,
15
16
  moveTrainer,
17
+ promoteMainlineTree,
16
18
  promoteMainline,
17
19
  };
@@ -0,0 +1,24 @@
1
+ const { flatten } = require('lodash');
2
+ const promoteMainline = require('./promote-mainline');
3
+ const { makeTree } = require('../helpers');
4
+
5
+ const promoteMainlineTree = (moments, current) => {
6
+ try {
7
+ const isNested = moments.length > 0 && Array.isArray(moments[0]);
8
+ if (!isNested) {
9
+ return moments;
10
+ }
11
+
12
+ // Flatten nested array
13
+ const flatMoments = flatten(moments);
14
+ const newMoments = promoteMainline(flatMoments, current);
15
+
16
+ // Group by consecutive depth sequences
17
+ return makeTree(newMoments);
18
+ } catch {
19
+ // In case of any error, return the original moments array
20
+ return moments;
21
+ }
22
+ };
23
+
24
+ module.exports = promoteMainlineTree;
@@ -89,6 +89,15 @@ const promoteMainline = (moments, current) => {
89
89
  result.push(move);
90
90
  }
91
91
 
92
+ // First, add the position marker that separates sideline from mainline continuation
93
+ for (let i = sidelineEndIndex; i < moments.length; i++) {
94
+ if (!demotedIndices.has(i) && !moments[i].move) {
95
+ result.push({ ...moments[i] });
96
+ break; // Only add the first position marker
97
+ }
98
+ }
99
+
100
+ // Then add the promoted sideline moves (the continuation of the new mainline)
92
101
  for (let i = sidelinePositionIndex + 1; i < sidelineEndIndex; i++) {
93
102
  if (i === current.index) continue;
94
103
 
@@ -100,12 +109,18 @@ const promoteMainline = (moments, current) => {
100
109
  }
101
110
  }
102
111
 
112
+ // Finally, add any remaining non-demoted moves
103
113
  for (let i = sidelineEndIndex; i < moments.length; i++) {
104
- if (!demotedIndices.has(i)) {
114
+ if (!demotedIndices.has(i) && moments[i].move) {
105
115
  result.push({ ...moments[i] });
106
116
  }
107
117
  }
108
118
 
119
+ // Remove trailing position markers that don't precede any moves
120
+ while (result.length > 0 && !result[result.length - 1].move) {
121
+ result.pop();
122
+ }
123
+
109
124
  // Reassign indices
110
125
  result.forEach((m, i) => {
111
126
  m.index = i;
@@ -3,6 +3,7 @@ const getBrushColor = require('./get-brush-color');
3
3
  const getMoveNumber = require('./get-move-number');
4
4
  const insertMomentIntoTree = require('./insert-moment-into-tree');
5
5
  const isNextFen = require('./is-next-fen');
6
+ const makeTree = require('./make-tree');
6
7
  const prepareMoment = require('./prepare-moment');
7
8
 
8
9
  module.exports = {
@@ -11,5 +12,6 @@ module.exports = {
11
12
  getMoveNumber,
12
13
  insertMomentIntoTree,
13
14
  isNextFen,
15
+ makeTree,
14
16
  prepareMoment,
15
17
  };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Converts a flat array of moments into a tree structure.
3
+ */
4
+ const makeTree = (moments) => {
5
+ if (!Array.isArray(moments) || moments.length === 0) {
6
+ return [];
7
+ }
8
+
9
+ const tree = [];
10
+ for (const item of moments) {
11
+ // Check if the current object does NOT have the 'move' property.
12
+ const isSplitPoint = !('move' in item);
13
+
14
+ if (isSplitPoint) {
15
+ // If it's a split point, we start a new sub-array.
16
+ tree.push([item]);
17
+ } else {
18
+ if (tree.length === 0) {
19
+ tree.push([]);
20
+ }
21
+ // Add the item to the last sub-array in the result.
22
+ tree[tree.length - 1].push(item);
23
+ }
24
+ }
25
+
26
+ return tree;
27
+ };
28
+
29
+ module.exports = makeTree;
@@ -1,4 +1,4 @@
1
- const { getBrushColor } = require('./helpers');
1
+ const getBrushColor = require('./helpers/get-brush-color');
2
2
 
3
3
  module.exports = (comment) => {
4
4
  try {
package/index.js CHANGED
@@ -6,7 +6,8 @@ const {
6
6
  getPrevMoment,
7
7
  momentsToPgn,
8
8
  moveTrainer,
9
- promoteMainline
9
+ promoteMainlineTree,
10
+ promoteMainline,
10
11
  } = require('./functions/extras');
11
12
 
12
13
  module.exports = {
@@ -21,5 +22,6 @@ module.exports = {
21
22
  getPrevMoment,
22
23
  momentsToPgn,
23
24
  moveTrainer,
24
- promoteMainline
25
+ promoteMainlineTree,
26
+ promoteMainline,
25
27
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chess-moments",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "index.js",
6
6
  "files": [