chess-moments 1.1.0 → 1.1.2
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,4 +1,4 @@
|
|
|
1
|
-
const { flatten } = require('lodash');
|
|
1
|
+
const { flatten, cloneDeep } = require('lodash');
|
|
2
2
|
const { insertMomentIntoTree } = require('../helpers');
|
|
3
3
|
|
|
4
4
|
const addMomentToTree = (tree, newMoment) => {
|
|
@@ -12,13 +12,16 @@ const addMomentToTree = (tree, newMoment) => {
|
|
|
12
12
|
return tree;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
// Deep clone chess tree to avoid mutating the original
|
|
16
|
+
const clonedTree = cloneDeep(tree);
|
|
17
|
+
|
|
15
18
|
// Find the appropriate position to insert the moment
|
|
16
19
|
let point = null;
|
|
17
20
|
|
|
18
21
|
// Look for the position where this moment should be inserted
|
|
19
22
|
// by finding the moment with matching "before" FEN at any depth
|
|
20
|
-
for (let lineIndex = 0; lineIndex <
|
|
21
|
-
const line =
|
|
23
|
+
for (let lineIndex = 0; lineIndex < clonedTree.length; lineIndex++) {
|
|
24
|
+
const line = clonedTree[lineIndex];
|
|
22
25
|
|
|
23
26
|
for (let momentIndex = 0; momentIndex < line.length; momentIndex++) {
|
|
24
27
|
const existingMoment = line[momentIndex];
|
|
@@ -50,7 +53,7 @@ const addMomentToTree = (tree, newMoment) => {
|
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
// Insert the moment into the tree
|
|
53
|
-
const newTree = insertMomentIntoTree(
|
|
56
|
+
const newTree = insertMomentIntoTree(clonedTree, point, newMoment);
|
|
54
57
|
|
|
55
58
|
// Reindex all moments
|
|
56
59
|
let globalIndex = 0;
|
|
@@ -77,7 +77,8 @@ const getNextMoments = (moments, current) => {
|
|
|
77
77
|
|
|
78
78
|
for (const moment of slim) {
|
|
79
79
|
// Only process main line and immediate variations
|
|
80
|
-
|
|
80
|
+
// Ignore next moments with depth lower than the current moment
|
|
81
|
+
if (moment.depth > current.depth + 1 || moment.depth < current.depth) {
|
|
81
82
|
continue;
|
|
82
83
|
}
|
|
83
84
|
|