cm-chess 3.3.1 → 3.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chess",
3
- "version": "3.3.1",
3
+ "version": "3.3.3",
4
4
  "description": "It's like chess.js, but in ES6 and can handle variations (tree-structured move history)",
5
5
  "main": "./src/Chess.js",
6
6
  "type": "module",
package/src/Chess.js CHANGED
@@ -230,7 +230,7 @@ export class Chess {
230
230
  try {
231
231
  const moveResult = this.pgn.history.addMove(move, previousMove, sloppy)
232
232
  publishEvent(this.observers,
233
- {type: EVENT_TYPE.legalMove, move: move, previousMove: previousMove})
233
+ {type: EVENT_TYPE.legalMove, move: moveResult, previousMove: previousMove})
234
234
  return moveResult
235
235
  } catch (e) {
236
236
  publishEvent(this.observers,
package/src/Fen.js CHANGED
@@ -18,15 +18,25 @@ export class Fen {
18
18
  const fenParts = fen.split(" ")
19
19
  this.position = fenParts[0]
20
20
  this.colorToPlay = fenParts[1]
21
- this.castlings = fenParts[2].split("")
21
+ if(fenParts[2] !== "-") {
22
+ this.castlings = fenParts[2].split("")
23
+ } else {
24
+ this.castlings = []
25
+ }
22
26
  this.enPassantTargetSquare = fenParts[3]
23
27
  this.plyClock = parseInt(fenParts[4], 10)
24
28
  this.moveNumber = parseInt(fenParts[5], 10)
25
29
  }
26
30
 
27
31
  toString() {
28
- return this.position + " " + this.colorToPlay + " " + this.castlings.join("") + " " +
29
- this.enPassantTargetSquare + " " + this.plyClock + " " + this.moveNumber
32
+ let fen = this.position + " " + this.colorToPlay + " "
33
+ if (this.castlings.length > 0) {
34
+ fen += this.castlings.join("")
35
+ } else {
36
+ fen += "-"
37
+ }
38
+ fen += " " + this.enPassantTargetSquare + " " + this.plyClock + " " + this.moveNumber
39
+ return fen
30
40
  }
31
41
 
32
42
  }
package/test/TestChess.js CHANGED
@@ -215,7 +215,7 @@ Kc4 7. Qb4+ Kd5 8. Qc5#) 5. Bc5# 1-0`)
215
215
  assert.true(chess.move("e4"))
216
216
  })
217
217
 
218
- it("should validate Moves", function() {
218
+ it("should validate Moves", function () {
219
219
  const chess = new Chess()
220
220
  assert.notEqual(chess.validateMove("e4"), null)
221
221
  assert.notEqual(chess.validateMove("e3"), null)
@@ -224,11 +224,13 @@ Kc4 7. Qb4+ Kd5 8. Qc5#) 5. Bc5# 1-0`)
224
224
  assert.equal(chess.validateMove("e6"), null)
225
225
  })
226
226
 
227
- it("should publish events", function() {
227
+ it("should publish events", function () {
228
228
  return new Promise((resolve, reject) => {
229
229
  const chess = new Chess()
230
230
  chess.addObserver((event) => {
231
- if(event.type === "legalMove" && event.move === "e4") {
231
+ if (event.type === "legalMove" &&
232
+ event.move.from === "e2" &&
233
+ event.move.to === "e4") {
232
234
  resolve()
233
235
  } else {
234
236
  reject("error event")
@@ -238,18 +240,18 @@ Kc4 7. Qb4+ Kd5 8. Qc5#) 5. Bc5# 1-0`)
238
240
  })
239
241
  })
240
242
 
241
- it("should provide valid moves", function() {
243
+ it("should provide valid moves", function () {
242
244
  const chess = new Chess()
243
245
  assert.equal(chess.moves().length, 20)
244
246
  chess.move("Nc3")
245
247
  assert.equal(chess.moves().length, 20)
246
248
  chess.move("e5")
247
249
  assert.equal(chess.moves().length, 22)
248
- assert.equal(JSON.stringify(chess.moves({ square: "e2"})), '["e3","e4"]')
249
- assert.equal(JSON.stringify(chess.moves({ piece: "n" })), '["Na4","Nb5","Nd5","Ne4","Nb1","Nf3","Nh3"]')
250
+ assert.equal(JSON.stringify(chess.moves({square: "e2"})), '["e3","e4"]')
251
+ assert.equal(JSON.stringify(chess.moves({piece: "n"})), '["Na4","Nb5","Nd5","Ne4","Nb1","Nf3","Nh3"]')
250
252
  })
251
253
 
252
- it("should detect a check in a game without moves", function() {
254
+ it("should detect a check in a game without moves", function () {
253
255
  const chess = new Chess("4k3/1P6/8/b7/6r1/8/pp2PPPP/2R1KBNR w K - 0 1")
254
256
  assert.true(chess.inCheck())
255
257
  })