npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chess-notation-parser

v1.0.2

Published

Convert chess algebraic notation into a standard json output

Downloads

10

Readme

Chess notation parser

This package takes a chess algebraic notation and return a standard parsed JSON format. It's a Portable Game Notation (PGN) is a standard plain text format for recording chess games (both the moves and related data), which can be read by humans and is also supported by most chess software. Note this just convert the Algebraic notation (bxc8 Kb4) a crucial part of PGN.

Tests

How to:

npm install chess-notation-parser
const chessNotationParser = require('chess-notation-parser');
// See below response
const parsedRes = chessNotationParser('bxc8=Q+')
//˯ parsedRes
{
  isValid: true,
  // Pawn at b capture something at c8 and promoted to queen with a check
  input: 'bxc8=Q+',
  
  piece: 'pawn',

  from: 'b',
  fromFile: 'b',
  fromRank: null,

  to: 'c8',
  toFile: 'c',
  toRank: '8',

  promotePiece: 'queen',
  castleSide: null,

  isCapture: true,
  isCheck: true,
  isCheckmate: false,
  isCastle: false,
  isPromotion: true,
}

Sample response

type Piece = "pawn" | "king" | "queen" | "knight" | "rook" | "bishop";
interface ParseResponse {
  isValid: boolean; // true
  input: string; // bxc8=Q+
  piece: Piece; // Pawn

  from?: string; // b - either file or rank N5f3 | Ngf3 => 5 | "g"
  fromFile?: string; // b
  fromRank?: string; // null

  to: string; // c8
  toFile: string; // c
  toRank: string; // 8

  promotePiece?: Piece; // queen
  castleSide?: "long" | "short";

  isCapture: boolean; // true
  isCheck: boolean; // true
  isCheckmate: boolean; // false;
  isCastle: boolean; // false
  isPromotion: boolean; // true
}

Unit test run with different variations support

 PASS  src/index.test.js
  ✓ Parser should always be truthy (5 ms)
  ✓ Parser should return whatever the input parameter is (1 ms)
  ✓ Parser should only accept validate input (1 ms)
  ✓ Parser should handle castling (1 ms)
  ✓ Parser should return a valid JSON based on ZOD schema (3 ms)
  ✓ Parser should able to handle checkmate (1 ms)
  ✓ Parser should able to handle check (1 ms)
  ✓ Parser should handle if piece is captured (2 ms)
  ✓ Parser should handle promotions (1 ms)
  ✓ Parser should handle pawn move
  ✓ Parser should handle for pawn move to e6 with checkmate (1 ms)
  ✓ Parser should handle for pawns e file promoted to Rook with check 
  ✓ Parser should handle d pawns captures e6 with promotion to bishop and checkmated  (1 ms)
  ✓ Parser should handle for location dxe8
  ✓ Parser should handle for location Bh7 (1 ms)
  ✓ Parser should handle for location Rab1
  ✓ Parser should handle for location Rab1 (1 ms)
  ✓ Parser should handle for location Nbxd2
  ✓ Parser should handle for location N5f3 (1 ms)

Test Suites: 1 passed, 1 total
Tests:       19 passed, 19 total
Snapshots:   0 total
Time:        0.655 s, estimated 1 s
Ran all test suites related to changed files.

Limitations:

  • This parser doesn't provide if the pawn is capture via en passant or not. Since to know if the pawn could capture via en-passant, we would need to know previous move played by the opponent and position of the complete board. Which is out of scope for this lib.

  • Same as if the input is e6, We don't know if the pawn move from e4 to e6 Double step or e5 to e6 without the complete information about the board.

This limitation are valid for this lib. as the idea is to just parse a chess notation. If this is an issue for your use case suggest looking into fen algorithm