chess-moments 0.15.2 → 1.0.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/functions/flat.js CHANGED
@@ -1,18 +1,22 @@
1
1
  const makeMoments = require('./make-moments');
2
2
 
3
3
  const flat = (sloppyPgn) => {
4
- const moments = makeMoments(sloppyPgn);
4
+ try {
5
+ const moments = makeMoments(sloppyPgn);
5
6
 
6
- // flatten array and add index for every moment
7
- const flatten = [].concat.apply([], moments);
7
+ // flatten array and add index for every moment
8
+ const flatten = [].concat.apply([], moments);
8
9
 
9
- let index = 0;
10
- for (const moment of flatten) {
11
- moment.index = index;
12
- index++;
13
- }
10
+ let index = 0;
11
+ for (const moment of flatten) {
12
+ moment.index = index;
13
+ index++;
14
+ }
14
15
 
15
- return flatten;
16
+ return flatten;
17
+ } catch {
18
+ return [];
19
+ }
16
20
  };
17
21
 
18
22
  module.exports = flat;
@@ -1,4 +1,4 @@
1
- const Chess = require('../chess');
1
+ const { Chess } = require('chess.js');
2
2
  const fen = require('./fen');
3
3
  const parser = require('./parser');
4
4
  const pgn = require('./pgn');
@@ -8,12 +8,12 @@ const make = (sloppyPgn) => {
8
8
  const normalizedPgn = pgn.normalize(sloppyPgn);
9
9
 
10
10
  // load PGN and check headers for existing FEN
11
- const chess = new Chess();
12
- chess.load_pgn(normalizedPgn);
13
- const header = chess.header();
11
+ const chess = new Chess(); // can throw if PGN is invalid
12
+ chess.loadPgn(normalizedPgn);
13
+ const headers = chess.getHeaders();
14
14
 
15
15
  const store = {
16
- fen: fen.normalize(header.FEN) || fen.initial, // current FEN
16
+ fen: fen.normalize(headers.FEN) || fen.initial, // current FEN
17
17
  depth: 1, // current depth
18
18
  };
19
19
 
@@ -26,7 +26,7 @@ const make = (sloppyPgn) => {
26
26
  store.depth = depth;
27
27
 
28
28
  // parse current moves with the computed FEN
29
- const moments = parser(chess, moves, store.fen, depth);
29
+ const moments = parser(moves, store.fen, depth);
30
30
 
31
31
  // set history for the current depth
32
32
  history.set(depth, fen.history(moments, history, depth));
@@ -16,5 +16,8 @@ module.exports = (pgn) => {
16
16
  return match.replace(/\n/g, '\\n');
17
17
  });
18
18
 
19
+ // Merge adjacent comments delimited by {}
20
+ processedPgn = processedPgn.replace(/(\}\s*\{)+/g, ' ');
21
+
19
22
  return processedPgn;
20
23
  };
@@ -1,19 +1,18 @@
1
+ const { Chess } = require('chess.js');
1
2
  const { initial } = require('./fen');
2
3
  const pgn = require('./pgn');
3
4
  const moment = require('./moment');
4
5
 
5
- module.exports = (chess, moves, fen = initial, depth = 1) => {
6
- const loaded = chess.load_pgn(pgn.build(moves, fen));
7
- if (!loaded) {
8
- return [];
9
- }
6
+ module.exports = (moves, fen = initial, depth = 1) => {
7
+ const chess = new Chess();
8
+ chess.loadPgn(pgn.build(moves, fen)); // can throw if PGN is invalid
10
9
 
11
10
  const history = chess.history({ verbose: true });
12
11
  while (chess.undo()) {
13
12
  chess.undo();
14
13
  }
15
14
 
16
- const comment = chess.get_comment();
15
+ const comment = chess.getComment();
17
16
  const first = moment.build({ depth, comment, fen: chess.fen() });
18
17
 
19
18
  const moments = history.map((item) => {
@@ -24,14 +23,14 @@ module.exports = (chess, moves, fen = initial, depth = 1) => {
24
23
  move: item.san,
25
24
  from: item.from,
26
25
  to: item.to,
27
- comment: chess.get_comment(),
26
+ comment: chess.getComment(),
28
27
  fen: chess.fen(),
29
28
  });
30
29
  });
31
30
 
32
31
  // finally, add the first chess "moment" when needed
33
- const header = chess.header();
34
- if (header.FEN || fen === initial || first.comment || first.shapes) {
32
+ const headers = chess.getHeaders();
33
+ if (headers.FEN || fen === initial || first.comment || first.shapes) {
35
34
  moments.unshift(first);
36
35
  }
37
36
 
@@ -20,21 +20,24 @@ const previousFen = (history, moves, currentDepth, previousDepth) => {
20
20
  return history.get(previousDepth)[0];
21
21
  }
22
22
 
23
- // try the first candidate from the current depth
24
- const candidate = history.get(currentDepth)[0];
25
- if (matchesFen(moves, candidate)) {
26
- return candidate;
27
- }
23
+ const currentHistory = history.get(currentDepth);
24
+ if (Array.isArray(currentHistory) && currentHistory.length > 0) {
25
+ // try the first candidate from the current depth
26
+ const candidate = currentHistory[0];
27
+ if (matchesFen(moves, candidate)) {
28
+ return candidate;
29
+ }
28
30
 
29
- // try the second candidate from the current depth
30
- const second = history.get(currentDepth)[1];
31
- if (matchesFen(moves, second)) {
32
- return second;
31
+ // try the second candidate from the current depth
32
+ const second = currentHistory[1];
33
+ if (matchesFen(moves, second)) {
34
+ return second;
35
+ }
33
36
  }
34
37
 
35
38
  return previousFen(history, moves, currentDepth - 1, previousDepth);
36
39
  } catch (err) {
37
- return false;
40
+ return '';
38
41
  }
39
42
  };
40
43
 
package/functions/tree.js CHANGED
@@ -1,17 +1,21 @@
1
1
  const makeMoments = require('./make-moments');
2
2
 
3
3
  const tree = (sloppyPgn) => {
4
- const moments = makeMoments(sloppyPgn);
4
+ try {
5
+ const moments = makeMoments(sloppyPgn);
5
6
 
6
- let index = 0;
7
- for (const lines of moments) {
8
- for (const moment of lines) {
9
- moment.index = index;
10
- index++;
7
+ let index = 0;
8
+ for (const lines of moments) {
9
+ for (const moment of lines) {
10
+ moment.index = index;
11
+ index++;
12
+ }
11
13
  }
12
- }
13
14
 
14
- return moments;
15
+ return moments;
16
+ } catch {
17
+ return [];
18
+ }
15
19
  };
16
20
 
17
21
  module.exports = tree;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chess-moments",
3
- "version": "0.15.2",
3
+ "version": "1.0.0",
4
4
  "description": "PGN parser that transforms PGN files into chess \"moments\"",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
- "chess.js": "^0.11.0"
27
+ "chess.js": "^1.4.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "chai": "^4.3.7",
package/chess.js DELETED
@@ -1,15 +0,0 @@
1
- const isBrowser =
2
- typeof window !== 'undefined' && typeof window.document !== 'undefined';
3
- const isNode = !isBrowser;
4
-
5
- let Chess;
6
-
7
- if (isNode) {
8
- const chess = require('chess.js');
9
- Chess = chess.Chess;
10
- } else {
11
- Chess = require('chess.js');
12
- }
13
-
14
- module.exports = Chess;
15
- module.exports.Chess = Chess;