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 +13 -9
- package/functions/make-moments.js +6 -6
- package/functions/normalize-pgn.js +3 -0
- package/functions/parser.js +8 -9
- package/functions/previous-fen.js +13 -10
- package/functions/tree.js +12 -8
- package/package.json +2 -2
- package/chess.js +0 -15
package/functions/flat.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
const makeMoments = require('./make-moments');
|
|
2
2
|
|
|
3
3
|
const flat = (sloppyPgn) => {
|
|
4
|
-
|
|
4
|
+
try {
|
|
5
|
+
const moments = makeMoments(sloppyPgn);
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
// flatten array and add index for every moment
|
|
8
|
+
const flatten = [].concat.apply([], moments);
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
let index = 0;
|
|
11
|
+
for (const moment of flatten) {
|
|
12
|
+
moment.index = index;
|
|
13
|
+
index++;
|
|
14
|
+
}
|
|
14
15
|
|
|
15
|
-
|
|
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('
|
|
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.
|
|
13
|
-
const
|
|
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(
|
|
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(
|
|
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));
|
package/functions/parser.js
CHANGED
|
@@ -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 = (
|
|
6
|
-
const
|
|
7
|
-
|
|
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.
|
|
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.
|
|
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
|
|
34
|
-
if (
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
|
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
|
-
|
|
4
|
+
try {
|
|
5
|
+
const moments = makeMoments(sloppyPgn);
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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.
|
|
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": "^
|
|
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;
|