@echecs/pgn 2.1.4 → 3.0.4

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Adrian de la Rosa
3
+ Copyright (c) 2024 Adrian de la Rosa
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # PGN
2
2
 
3
- **PGN** is part of the **ECHECS** project. **PGN** is a parser of the
4
- [PGN specification](http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm).
3
+ `PGN` is a parser that is part of the **ECHECS** project, designed to interpret
4
+ the
5
+ [PGN (Portable Game Notation) specification](http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm).
5
6
 
6
7
  ## Installation
7
8
 
@@ -9,228 +10,135 @@
9
10
  npm install --save-dev @echecs/pgn
10
11
  ```
11
12
 
12
- ```bash
13
- yarn add @echecs/pgn
13
+ ## Usage
14
+
15
+ The `parse` function takes a PGN formatted string as input and returns an array
16
+ of parsed PGN objects.
17
+
18
+ ```typescript
19
+ parse(input: string): PGN[]
14
20
  ```
15
21
 
16
- ## Usage
22
+ ### PGN Object Format
17
23
 
18
- `parse(input: string): PGN[]`
24
+ Here’s the structure of the `PGN` object:
19
25
 
20
- **PGN** format:
26
+ #### PGN Object
21
27
 
22
- ```
28
+ ```json
23
29
  {
24
- meta: Meta,
25
- moves: Moves,
26
- result: 1-0 // 1-0 | 0-1 | 1/2-1/2 | ?
30
+ "meta": Meta,
31
+ "moves": Moves,
32
+ "result": "1-0" // possible values: "1-0", "0-1", "1/2-1/2", "?"
27
33
  }
28
34
  ```
29
35
 
30
- **Meta** format:
36
+ #### Meta Object
31
37
 
32
- ```
38
+ The `meta` object contains metadata about the chess game.
39
+
40
+ ```json
33
41
  {
34
- // Based on the PGN specification at least the following Tags should be available
35
- Event: "the name of the tournament or match event"
36
- Site: "the location of the event"
37
- Date: "the starting date of the game"
38
- Round: "the playing round ordinal of the game"
39
- White: "the player of the white pieces"
40
- Black: "the player of the black pieces"
41
- Result: "the result of the game"
42
- // plus any other additional tags with `key` string
42
+ "Event": "name of the tournament or match event",
43
+ "Site": "location of the event",
44
+ "Date": "starting date of the game",
45
+ "Round": "playing round ordinal of the game",
46
+ "White": "player of the white pieces",
47
+ "Black": "player of the black pieces",
48
+ "Result": "result of the game",
49
+ // Any other additional tags
43
50
  [key]: "string"
44
51
  }
45
52
  ```
46
53
 
47
- **Moves** is an _array_ of:
54
+ #### Moves Array
48
55
 
49
- ```
50
- // move number, white move, black move
51
- [5, Move, Move]
56
+ `Moves` is an array representing the sequence of moves in the game. Each element
57
+ is an array containing the move number, the white move, and the black move.
58
+
59
+ ```json
60
+ [
61
+ moveNumber,
62
+ Move,
63
+ Move
64
+ ]
52
65
  ```
53
66
 
54
- Notice that half move are available for variations of if the last move of the game was white.
67
+ Note: Half moves are included for variations or in cases where the last move was
68
+ made by white.
55
69
 
56
- **Move** format:
70
+ #### Move Object
57
71
 
58
- ```
72
+ Each move is represented by the following structure:
73
+
74
+ ```json
59
75
  {
60
- annotations: ["!", "$126"], // (optional) all the annotations for the given move
61
- capture: false, // (optional) indicates if the move capture any piece
62
- castling: true, // (optional) indicates if the move was castling
63
- check: false, // (optional) indicates if the move checks the rival king
64
- checkmate: false, // (optional) indicates if it is checkmate
65
- comment: 'Some comment', // (optional) comment of the move
66
- from: 'e', // (optional) Disambiguation of the move
67
- piece: 'K', // (required) P (Pawn) | R (Rook) | N (Knight) | B (Bishop) | Q (Queen) | K (King)
68
- promotion: Piece; // (optional) R (Rook) | N (Knight) | B (Bishop) | Q (Queen)
69
- to: 'g1', // ending square of the piece
70
- variants: [...] // moves following Moves format
76
+ "annotations": ["!", "$126"], // optional, annotations for the move
77
+ "capture": false, // optional, indicates if any piece was captured
78
+ "castling": true, // optional, indicates if the move was castling
79
+ "check": false, // optional, indicates if the move put the rival king in check
80
+ "checkmate": false, // optional, indicates if it is a checkmate
81
+ "comment": "Some comment", // optional, comment about the move
82
+ "from": "e", // optional, disambiguation of the move
83
+ "piece": "K", // required, type of piece (P, R, N, B, Q, K)
84
+ "promotion": "Piece", // optional, promotion piece (R, N, B, Q)
85
+ "to": "g1", // required, ending square of the move
86
+ "variants": [...] // optional, array of moves for variations following Moves format
71
87
  }
72
88
  ```
73
89
 
74
- **Example**
90
+ ### Example
91
+
92
+ Here's a sample usage of the `PGN` parser:
75
93
 
76
94
  ```js
77
95
  import { readFileSync } from 'fs';
78
96
  import parse from '@echecs/pgn';
79
97
 
80
- function readFile(path: string): string {
98
+ function readFile(path) {
81
99
  const filename = require.resolve(path);
82
-
83
100
  return readFileSync(filename, 'utf8');
84
101
  }
85
102
 
86
103
  const pgn = parse(readFile('./games/file.pgn'));
87
- // [
88
- // {
89
- // "meta": {
90
- // "Black": "Cordts, Ingo",
91
- // "BlackElo": "2222",
92
- // "Date": "2000.10.29",
93
- // "ECO": "A56",
94
- // "Result": "0-1",
95
- // "Round": "?",
96
- // "Site": "?",
97
- // "White": "Carlsen, Magnus",
98
- // "WhiteElo": "0",
99
- // },
100
- // "moves": [
101
- // [
102
- // 1,
103
- // {
104
- // "piece": "P",
105
- // "to": "d4",
106
- // },
107
- // {
108
- // "piece": "N",
109
- // "to": "f6",
110
- // },
111
- // ],
112
- // [
113
- // 2,
114
- // {
115
- // "piece": "P",
116
- // "to": "c4",
117
- // },
118
- // {
119
- // "piece": "P",
120
- // "to": "c5",
121
- // },
122
- // ],
123
- // [
124
- // 3,
125
- // {
126
- // "piece": "N",
127
- // "to": "f3",
128
- // },
129
- // {
130
- // "capture": true,
131
- // "from": "c",
132
- // "piece": "P",
133
- // "to": "d4",
134
- // },
135
- // ],
136
- // ...
137
- // [
138
- // 6,
139
- // {
140
- // "capture": true,
141
- // "from": "c",
142
- // "piece": "P",
143
- // "to": "d5",
144
- // },
145
- // {
146
- // "piece": "B",
147
- // "to": "c5",
148
- // },
149
- // ],
150
- // [
151
- // 7,
152
- // {
153
- // "from": "5",
154
- // "piece": "N",
155
- // "to": "c3",
156
- // },
157
- // {
158
- // "castling": true,
159
- // "piece": "K",
160
- // "to": "g8",
161
- // },
162
- // ],
163
- // ...
164
- // [
165
- // 21,
166
- // {
167
- // "capture": true,
168
- // "piece": "N",
169
- // "to": "e4",
170
- // },
171
- // {
172
- // "piece": "B",
173
- // "to": "b7",
174
- // },
175
- // ],
176
- // [
177
- // 22,
178
- // {
179
- // "capture": true,
180
- // "piece": "Q",
181
- // "to": "d7",
182
- // },
183
- // {
184
- // "capture": true,
185
- // "piece": "B",
186
- // "to": "e4",
187
- // },
188
- // ],
189
- // [
190
- // 23,
191
- // {
192
- // "piece": "R",
193
- // "to": "h2",
194
- // },
195
- // {
196
- // "capture": true,
197
- // "piece": "B",
198
- // "to": "d6",
199
- // },
200
- // ],
201
- // ...
202
- // [
203
- // 29,
204
- // {
205
- // "capture": true,
206
- // "check": true,
207
- // "piece": "Q",
208
- // "to": "e6",
209
- // },
210
- // {
211
- // "piece": "K",
212
- // "to": "h8",
213
- // },
214
- // ],
215
- // [
216
- // 30,
217
- // {
218
- // "piece": "Q",
219
- // "to": "e7",
220
- // },
221
- // {
222
- // "piece": "B",
223
- // "to": "c7",
224
- // },
225
- // ],
226
- // ],
227
- // "result": 0,
228
- // },
229
- // ];
104
+
105
+ // Output example of parsed `PGN`
106
+ console.log(pgn);
107
+ /*
108
+ [
109
+ {
110
+ "meta": {
111
+ "Event": "Some Tournament",
112
+ "Site": "Some Location",
113
+ "Date": "2023.10.04",
114
+ "Round": "1",
115
+ "White": "Player1",
116
+ "Black": "Player2",
117
+ "Result": "1-0",
118
+ // additional tags...
119
+ },
120
+ "moves": [
121
+ [
122
+ 1,
123
+ { "piece": "P", "to": "e4" },
124
+ { "piece": "P", "to": "e5" }
125
+ ],
126
+ [
127
+ 2,
128
+ { "piece": "N", "to": "f3" },
129
+ { "piece": "N", "to": "c6" }
130
+ ],
131
+ // more moves...
132
+ ],
133
+ "result": "1-0"
134
+ }
135
+ ];
136
+ */
230
137
  ```
231
138
 
232
- ## Warning
139
+ ## Important Notes
233
140
 
234
- **PGN** does not guarantee PGN games are valid. It does only parse the content.
235
- As part of the **ECHECS** project, it is responsability of **@echecs/game** to
236
- verify the validity of the game.
141
+ - `PGN` is a parser and does not verify the validity of the PGN games. It only
142
+ parses the provided content.
143
+ - For game validation, use **@echecs/game** as it is responsible for verifying
144
+ game correctness as part of the **ECHECS** project.