yo35 / kokopu

A JavaScript/TypeScript library implementing the chess game rules and providing tools to read/write the standard chess file formats.
https://www.npmjs.com/package/kokopu
GNU Lesser General Public License v3.0
27 stars 5 forks source link

It would be convenient if Game had properties instead of functions #37

Closed JeffML closed 1 year ago

JeffML commented 1 year ago

I can't decompose Game properties wrapped in functions such as playerName, playerElo, etc. For example, I would like to do the following assignments (if properties were data members instead of functions):

const {w, b} = db.players

for (let p of {w, b}) {
  const {elo, playerName, playerColor, playerTitle, playerElo} = p
  // do something with the properties
}
yo35 commented 1 year ago

Game metadata such as playerName(), playerElo(), etc... are encapsulated so that the Game class can enforce some constrainted on them: for instance, playerElo() is guaranteed to be a positive integer, result() is guaranteed to be an element of [ '1-0', '1/2-1/2', '0-1', '*' ]... Enforcing these constraints would not be possible by directly exposing metadata as properties.

Just to mention: the lib provides GamePOJO, which is a pure JavaScript object (i.e. not a class, no methods) that conveys the same information as Game. Conversion back and forth between Game and GamePOJO are achieved by Game.pojo() and Game.fromPOJO().

JeffML commented 1 year ago

I remember seeing that now that you mention it. That will do nicely. Closing.