chess-moments 1.4.1 → 1.4.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.
@@ -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 { groupBy, flatten } = require('lodash');
2
+ const promoteMainline = require('./promote-mainline');
3
+
4
+ const promoteMainlineTree = (moments, current) => {
5
+ try {
6
+ const isNested = moments.length > 0 && Array.isArray(moments[0]);
7
+ if (!isNested) {
8
+ return moments;
9
+ }
10
+
11
+ // Flatten nested array
12
+ const flatMoments = flatten(moments);
13
+ const newMoments = promoteMainline(flatMoments, current);
14
+
15
+ // Regroup by depth into a nested array
16
+ const grouped = groupBy(newMoments, 'depth');
17
+ return Object.values(grouped);
18
+ } catch {
19
+ // In case of any error, return the original moments array
20
+ return moments;
21
+ }
22
+ };
23
+
24
+ module.exports = promoteMainlineTree;
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.2",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "index.js",
6
6
  "files": [