chess-moments 1.1.2 → 1.2.0

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.
package/README.md CHANGED
@@ -47,6 +47,24 @@ The first level splits between variants and subvariants played in the chess game
47
47
  Returns a two level deep array of chess moments.
48
48
  It is useful for hiding future moves in chess training.
49
49
 
50
+ ## Extras
51
+
52
+ ### .addMomentToTree()
53
+
54
+ Adds a new move to the chess tree
55
+
56
+ ### .getNextMoments()
57
+
58
+ Move forward in the chess tree
59
+
60
+ ### .getPrevMoment()
61
+
62
+ Move back in the chess tree
63
+
64
+ ### .momentsToPgn()
65
+
66
+ Transform the chess tree into PGN
67
+
50
68
  ## JSON output
51
69
 
52
70
  The basic PGN file `1. e4 e5 *` will generated the following chess moments in JSON format:
@@ -1,6 +1,12 @@
1
1
  const { flatten, cloneDeep } = require('lodash');
2
2
  const { insertMomentIntoTree } = require('../helpers');
3
3
 
4
+ /**
5
+ * Adds a new chess moment to the tree structure.
6
+ * @param {*} tree - The current tree structure.
7
+ * @param {*} newMoment - The new moment to add.
8
+ * @returns {*} - The updated tree structure.
9
+ */
4
10
  const addMomentToTree = (tree, newMoment) => {
5
11
  if (!newMoment || !newMoment.san || !newMoment.before) {
6
12
  return tree;
@@ -52,6 +58,15 @@ const addMomentToTree = (tree, newMoment) => {
52
58
  }
53
59
  }
54
60
 
61
+ // Remove all new flags from existing moments
62
+ for (const line of clonedTree) {
63
+ for (const moment of line) {
64
+ if (moment.new) {
65
+ delete moment.new;
66
+ }
67
+ }
68
+ }
69
+
55
70
  // Insert the moment into the tree
56
71
  const newTree = insertMomentIntoTree(clonedTree, point, newMoment);
57
72
 
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Finds the most recently inserted moment in the tree.
3
+ * @param {*} tree - The current tree structure.
4
+ * @returns {Object|null} - The inserted moment or null if none found.
5
+ */
6
+ const findInsertedMoment = (tree) => {
7
+ for (const line of tree) {
8
+ for (const moment of line) {
9
+ if (moment.new) {
10
+ return moment;
11
+ }
12
+ }
13
+ }
14
+ return null;
15
+ };
16
+
17
+ module.exports = findInsertedMoment;
@@ -1,4 +1,5 @@
1
1
  const addMomentToTree = require('./add-moment-to-tree');
2
+ const findInsertedMoment = require('./find-inserted-moment');
2
3
  const getNextMoments = require('./get-next-moments');
3
4
  const getPrevMoment = require('./get-prev-moment');
4
5
  const momentsToPgn = require('./moments-to-pgn');
@@ -6,6 +7,7 @@ const moveTrainer = require('./move-trainer');
6
7
 
7
8
  module.exports = {
8
9
  addMomentToTree,
10
+ findInsertedMoment,
9
11
  getNextMoments,
10
12
  getPrevMoment,
11
13
  momentsToPgn,
@@ -12,6 +12,9 @@ const insertMomentIntoTree = (tree, point, newMoment) => {
12
12
  // Prepare the moment to be inserted with only the properties that match normal moments
13
13
  const momentToInsert = prepareMoment(tree, point, newMoment);
14
14
 
15
+ // Add flag to indicate this is a new moment
16
+ momentToInsert.new = true;
17
+
15
18
  // If no matching position found, always create a new line
16
19
  if (!point) {
17
20
  tree.push([momentToInsert]);
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const { fen, pgn, flat, tree, mainline } = require('./functions');
2
2
  const {
3
3
  addMomentToTree,
4
+ findInsertedMoment,
4
5
  getNextMoments,
5
6
  getPrevMoment,
6
7
  momentsToPgn,
@@ -14,6 +15,7 @@ module.exports = {
14
15
  tree,
15
16
  mainline,
16
17
  addMomentToTree,
18
+ findInsertedMoment,
17
19
  getNextMoments,
18
20
  getPrevMoment,
19
21
  momentsToPgn,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chess-moments",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "index.js",
6
6
  "files": [