@echecs/pgn 1.0.0 → 2.1.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.
@@ -0,0 +1,11 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: npm
4
+ directory: '/'
5
+ schedule:
6
+ interval: daily
7
+ time: '13:00'
8
+ open-pull-requests-limit: 10
9
+ commit-message:
10
+ prefix: 'chore'
11
+ include: 'scope'
@@ -0,0 +1,34 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ name: Release
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - name: Checkout code
14
+ uses: actions/checkout@v3
15
+ - name: Setup
16
+ uses: actions/setup-node@v3
17
+ with:
18
+ node-version: node
19
+ - name: Cache NPM Modules
20
+ id: cache-node
21
+ uses: actions/cache@v3
22
+ with:
23
+ path: '**/node_modules'
24
+ key: ${{ runner.os }}-yarn-${{ hashFiles('**/package-lock.json') }}
25
+ - name: Install dependencies
26
+ if: steps.cache-node.outputs.cache-hit != 'true'
27
+ run: npm ci
28
+ - name: Verify
29
+ run: npm run test
30
+ - name: Release to NPM
31
+ uses: JS-DevTools/npm-publish@v1
32
+ with:
33
+ access: public
34
+ token: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env sh
2
+ . "$(dirname -- "$0")/_/husky.sh"
3
+
4
+ npx lint-staged
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ npm run format && npm run lint && npm run test
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Adrian de la Rosa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,344 +1,236 @@
1
1
  # PGN
2
2
 
3
- PGN is part of the project @echecs (chess). It parses a PGN File following the
4
- specification provided in:
5
- [Specification](http://www.saremba.de/chessgml/standards/pgn/pgn-complete.htm)
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).
6
5
 
7
- ## Use
6
+ ## Installation
8
7
 
9
- We just need to provide with a PGN to the parser. It always return an array
10
- because PGN files could contain several games.
8
+ ```bash
9
+ npm install --save-dev @echecs/pgn
10
+ ```
11
+
12
+ ```bash
13
+ yarn add @echecs/pgn
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ `parse(input: string): PGN[]`
19
+
20
+ **PGN** format:
21
+
22
+ ```json
23
+ {
24
+ meta: Meta,
25
+ moves: Moves,
26
+ result: 1-0 // 1-0 | 0-1 | 1/2-1/2 | ?
27
+ }
28
+ ```
29
+
30
+ **Meta** format:
31
+
32
+ ```json
33
+ {
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
43
+ [key]: "string"
44
+ }
45
+ ```
46
+
47
+ **Moves** is an _array_ of:
48
+
49
+ ```json
50
+ // move number, white move, black move
51
+ [5, Move, Move]
52
+ ```
53
+
54
+ Notice that half move are available for variations of if the last move of the game was white.
55
+
56
+ **Move** format:
57
+
58
+ ```json
59
+ {
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
71
+ }
72
+ ```
73
+
74
+ **Example**
11
75
 
12
76
  ```js
13
- const parser = require('@echecs/pgn');
77
+ import { readFileSync } from 'fs';
78
+ import parse from '@echecs/pgn';
14
79
 
15
- parser(string);
80
+ function readFile(path: string): string {
81
+ const filename = require.resolve(path);
16
82
 
83
+ return readFileSync(filename, 'utf8');
84
+ }
85
+
86
+ const pgn = parse(readFile('./games/file.pgn'));
17
87
  // [
18
88
  // {
19
89
  // "meta": {
20
- // "Site": "?",
90
+ // "Black": "Cordts, Ingo",
91
+ // "BlackElo": "2222",
21
92
  // "Date": "2000.10.29",
93
+ // "ECO": "A56",
94
+ // "Result": "0-1",
22
95
  // "Round": "?",
96
+ // "Site": "?",
23
97
  // "White": "Carlsen, Magnus",
24
- // "Black": "Cordts, Ingo",
25
- // "ECO": "A56",
26
98
  // "WhiteElo": "0",
27
- // "BlackElo": "2222",
28
- // "Result": "0-1"
29
99
  // },
30
100
  // "moves": [
31
101
  // [
102
+ // 1,
32
103
  // {
33
104
  // "piece": "P",
34
- // "to": "d4"
105
+ // "to": "d4",
35
106
  // },
36
107
  // {
37
108
  // "piece": "N",
38
- // "to": "f6"
39
- // }
109
+ // "to": "f6",
110
+ // },
40
111
  // ],
41
112
  // [
113
+ // 2,
42
114
  // {
43
115
  // "piece": "P",
44
- // "to": "c4"
116
+ // "to": "c4",
45
117
  // },
46
118
  // {
47
119
  // "piece": "P",
48
- // "to": "c5"
49
- // }
50
- // ],
51
- // [
52
- // {
53
- // "piece": "N",
54
- // "to": "f3"
120
+ // "to": "c5",
55
121
  // },
56
- // {
57
- // "from": "c",
58
- // "piece": "P",
59
- // "to": "d4"
60
- // }
61
122
  // ],
62
123
  // [
124
+ // 3,
63
125
  // {
64
126
  // "piece": "N",
65
- // "to": "d4"
127
+ // "to": "f3",
66
128
  // },
67
129
  // {
130
+ // "capture": true,
131
+ // "from": "c",
68
132
  // "piece": "P",
69
- // "to": "e5"
70
- // }
71
- // ],
72
- // [
73
- // {
74
- // "piece": "N",
75
- // "to": "b5"
133
+ // "to": "d4",
76
134
  // },
77
- // {
78
- // "piece": "P",
79
- // "to": "d5"
80
- // }
81
135
  // ],
136
+ // ...
82
137
  // [
138
+ // 6,
83
139
  // {
140
+ // "capture": true,
84
141
  // "from": "c",
85
142
  // "piece": "P",
86
- // "to": "d5"
143
+ // "to": "d5",
87
144
  // },
88
145
  // {
89
146
  // "piece": "B",
90
- // "to": "c5"
91
- // }
147
+ // "to": "c5",
148
+ // },
92
149
  // ],
93
150
  // [
151
+ // 7,
94
152
  // {
95
153
  // "from": "5",
96
154
  // "piece": "N",
97
- // "to": "c3"
155
+ // "to": "c3",
98
156
  // },
99
157
  // {
100
158
  // "castling": true,
101
159
  // "piece": "K",
102
- // "to": "c8"
103
- // }
104
- // ],
105
- // [
106
- // {
107
- // "piece": "P",
108
- // "to": "e3"
109
- // },
110
- // {
111
- // "piece": "P",
112
- // "to": "e4"
113
- // }
114
- // ],
115
- // [
116
- // {
117
- // "piece": "P",
118
- // "to": "h3"
119
- // },
120
- // {
121
- // "piece": "R",
122
- // "to": "e8"
123
- // }
124
- // ],
125
- // [
126
- // {
127
- // "piece": "P",
128
- // "to": "g4"
129
- // },
130
- // {
131
- // "piece": "R",
132
- // "to": "e5"
133
- // }
134
- // ],
135
- // [
136
- // {
137
- // "piece": "B",
138
- // "to": "c4"
160
+ // "to": "g8",
139
161
  // },
140
- // {
141
- // "from": "b",
142
- // "piece": "N",
143
- // "to": "d7"
144
- // }
145
162
  // ],
163
+ // ...
146
164
  // [
165
+ // 21,
147
166
  // {
148
- // "piece": "Q",
149
- // "to": "b3"
150
- // },
151
- // {
167
+ // "capture": true,
152
168
  // "piece": "N",
153
- // "to": "e8"
154
- // }
155
- // ],
156
- // [
157
- // {
158
- // "piece": "N",
159
- // "to": "d2"
169
+ // "to": "e4",
160
170
  // },
161
171
  // {
162
- // "piece": "N",
163
- // "to": "d6"
164
- // }
165
- // ],
166
- // [
167
- // {
168
172
  // "piece": "B",
169
- // "to": "e2"
173
+ // "to": "b7",
170
174
  // },
171
- // {
172
- // "piece": "Q",
173
- // "to": "h4"
174
- // }
175
- // ],
176
- // [
177
- // {
178
- // "piece": "N",
179
- // "to": "c4"
180
- // },
181
- // {
182
- // "piece": "N",
183
- // "to": "c4"
184
- // }
185
- // ],
186
- // [
187
- // {
188
- // "piece": "Q",
189
- // "to": "c4"
190
- // },
191
- // {
192
- // "piece": "P",
193
- // "to": "b5"
194
- // }
195
- // ],
196
- // [
197
- // {
198
- // "piece": "Q",
199
- // "to": "b5"
200
- // },
201
- // {
202
- // "piece": "R",
203
- // "to": "b8"
204
- // }
205
175
  // ],
206
176
  // [
177
+ // 22,
207
178
  // {
179
+ // "capture": true,
208
180
  // "piece": "Q",
209
- // "to": "a4"
210
- // },
211
- // {
212
- // "piece": "N",
213
- // "to": "f6"
214
- // }
215
- // ],
216
- // [
217
- // {
218
- // "piece": "Q",
219
- // "to": "c6"
220
- // },
221
- // {
222
- // "piece": "N",
223
- // "to": "d7"
224
- // }
225
- // ],
226
- // [
227
- // {
228
- // "piece": "P",
229
- // "to": "d6"
230
- // },
231
- // {
232
- // "piece": "R",
233
- // "to": "e6"
234
- // }
235
- // ],
236
- // [
237
- // {
238
- // "piece": "N",
239
- // "to": "e4"
181
+ // "to": "d7",
240
182
  // },
241
183
  // {
184
+ // "capture": true,
242
185
  // "piece": "B",
243
- // "to": "b7"
244
- // }
245
- // ],
246
- // [
247
- // {
248
- // "piece": "Q",
249
- // "to": "d7"
186
+ // "to": "e4",
250
187
  // },
251
- // {
252
- // "piece": "B",
253
- // "to": "e4"
254
- // }
255
188
  // ],
256
189
  // [
190
+ // 23,
257
191
  // {
258
192
  // "piece": "R",
259
- // "to": "h2"
193
+ // "to": "h2",
260
194
  // },
261
195
  // {
196
+ // "capture": true,
262
197
  // "piece": "B",
263
- // "to": "d6"
264
- // }
265
- // ],
266
- // [
267
- // {
268
- // "piece": "B",
269
- // "to": "c4"
198
+ // "to": "d6",
270
199
  // },
271
- // {
272
- // "piece": "R",
273
- // "to": "d8"
274
- // }
275
200
  // ],
201
+ // ...
276
202
  // [
203
+ // 29,
277
204
  // {
205
+ // "capture": true,
206
+ // "check": true,
278
207
  // "piece": "Q",
279
- // "to": "a7"
208
+ // "to": "e6",
280
209
  // },
281
210
  // {
282
- // "piece": "B",
283
- // "to": "h2"
284
- // }
285
- // ],
286
- // [
287
- // {
288
- // "piece": "B",
289
- // "to": "e6"
211
+ // "piece": "K",
212
+ // "to": "h8",
290
213
  // },
291
- // {
292
- // "from": "f",
293
- // "piece": "P",
294
- // "to": "e6"
295
- // }
296
214
  // ],
297
215
  // [
216
+ // 30,
298
217
  // {
299
218
  // "piece": "Q",
300
- // "to": "a6"
219
+ // "to": "e7",
301
220
  // },
302
221
  // {
303
222
  // "piece": "B",
304
- // "to": "f3"
305
- // }
306
- // ],
307
- // [
308
- // {
309
- // "piece": "B",
310
- // "to": "d2"
311
- // },
312
- // {
313
- // "piece": "Q",
314
- // "to": "h3"
315
- // }
316
- // ],
317
- // [
318
- // {
319
- // "annotations": [
320
- // "check"
321
- // ],
322
- // "piece": "Q",
323
- // "to": "e6"
223
+ // "to": "c7",
324
224
  // },
325
- // {
326
- // "piece": "K",
327
- // "to": "h8"
328
- // }
329
225
  // ],
330
- // [
331
- // {
332
- // "piece": "Q",
333
- // "to": "e7"
334
- // },
335
- // {
336
- // "piece": "B",
337
- // "to": "c7"
338
- // }
339
- // ]
340
226
  // ],
341
- // "result": 0
342
- // }
343
- // ]
227
+ // "result": 0,
228
+ // },
229
+ // ];
344
230
  ```
231
+
232
+ ## Warning
233
+
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.
@@ -0,0 +1,35 @@
1
+ export const Lexer: undefined;
2
+ export const ParserRules: ({
3
+ name: string;
4
+ symbols: (string | RegExp)[];
5
+ postprocess: (d: any) => any;
6
+ } | {
7
+ name: string;
8
+ symbols: (string | {
9
+ literal: string;
10
+ })[];
11
+ postprocess?: undefined;
12
+ } | {
13
+ name: string;
14
+ symbols: (string | RegExp)[];
15
+ postprocess?: undefined;
16
+ } | {
17
+ name: string;
18
+ symbols: (string | {
19
+ literal: string;
20
+ })[];
21
+ postprocess: (d: any) => any;
22
+ } | {
23
+ name: string;
24
+ symbols: (RegExp | {
25
+ literal: string;
26
+ })[];
27
+ postprocess: (d: any) => any;
28
+ } | {
29
+ name: string;
30
+ symbols: (RegExp | {
31
+ literal: string;
32
+ })[];
33
+ postprocess?: undefined;
34
+ })[];
35
+ export const ParserStart: string;