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

fen-reader

v1.0.0-beta

Published

chess FEN position reader/converters/scanner/checkers

Downloads

4

Readme

fen-reader

fen-reader CI

chess FEN reader, converters, scanner and check position validity.

installation

> npm install --save fen-reader

or with yarn

> yarn add fen-reader

converters

Converters can transform FEN position format:

  • string to array fen2array(fen: string): Array<string[]>
  • string to json fen2json(fen: string): {[keyname: string]: string}
  • array to json array2json(fen: Array<string[]>): {[keyname: string]: string}
  • array to string array2fen(fen: Array<string[]>): string
  • json to array json2array(fen: {[keyname: string]: string}): Array<string[]>
  • json to string json2fen(fen: {[keyname: string]: string}): string

for extract FEN position of full FEN format see normalizer

e.g FEN position: rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR e.g full FEN: rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 1 2

fen2array


const {converters} = require('fen-reader');

const fenArray = converters.fen2array('rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR');

console.log(fenArray);
/*
[
  ["r","n","b","q","k","b","","r"],
  ["p","p","p","p","p","p","p","p"],
  ["","","","","","n","",""],
  ["","","","","","","",""],
  ["","","","","P","","",""],
  ["","","","","","","",""],
  ["P","P","P","P","","P","P","P"],
  ["R","N","B","Q","K","B","N","R"]
]
*/

black parts in lower case and white parts upper case array fen is readable as look by white player.

fen2json


const {converters} = require('fen-reader');

const fenJson = converters.fen2json('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR');

console.log(fenJson);
/*
{
  "a1": "rw",
  "b1": "nw",
  "c1": "bw",
  "d1": "qw",
  "e1": "kw",
  "f1": "bw",
  "g1": "nw",
  "h1": "rw",
  "a2": "pw",
  "b2": "pw",
  "c2": "pw",
  "d2": "pw",
  "e2": "pw",
  "f2": "pw",
  "g2": "pw",
  "h2": "pw",
  "a8": "rb",
  "b8": "nb",
  "c8": "bb",
  "d8": "qb",
  "e8": "kb",
  "f8": "bb",
  "g8": "nb",
  "h8": "rb",
  "a7": "pb",
  "b7": "pb",
  "c7": "pb",
  "d7": "pb",
  "e7": "pb",
  "f7": "pb",
  "g7": "pb",
  "h7": "pb"
}
*/

can use arg2 options for configure output json fen

interface Fen2JsonOptions {

  /**
   * @describe json output contains only squares with part while true
   * @default true
   */
  isRemoveEmptySquare: boolean,

  /**
   * @describe parts notation use lower/upper as color depending while true
   * @default false
   */

  isImplicitColor: boolean,

  /**
   * @describe value from empty square while `isRemoveEmptySquare` is false
   * @default null
   */
  emptySquare: any

}

reciprocal

The other converters are reciprocal functions from above functions

  • array to json array2json(fen: Array<string[]>): {[keyname: string]: string}
  • array to string array2fen(fen: Array<string[]>): string
  • json to array json2array(fen: {[keyname: string]: string}): Array<string[]>
  • json to string json2fen(fen: {[keyname: string]: string}): string

validity

fen-reader implements checker of position validity. for extract FEN position of full FEN format see normalizer.

const {isValidFenPosition} = require('fen-reader');

const FEN_POSITION = "rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR";

const fenPositionResponse = isValidFenPosition( {
  position: FEN_POSITION,
  options: {
    isRequireKings: true, // default=true,
    separator: "/", // rows separator default "/"
  }
} )

console.log( fenPositionResponse )

The response position have the below sheme:


interface FenPositionResponse {

  isValid: boolean,
  details?: string[],
  warn?: string,

  /**
  * @see StateAttributes
  * @describe contains status of all checkers position
  */
  stats: StateAttributes,

  /**
  * @see ScanResponse
  * @describe contains scan infos of position only if `isValid` is true
  */
  scan?: ScanResponse
}

interface ScanResponse {

  [keyname: string]: number | null,

  countParts: number | null,
  countWhiteParts: number | null,
  countBlackParts: number | null,

  countPawns: number | null,
  countWhitePawns: number | null,
  countBlackPawns: number | null,

  countRooks: number | null,
  countWhiteRooks: number | null,
  countBlackRooks: number | null,

  countBishops: number | null,
  countWhiteBishops: number | null,
  countBlackBishops: number | null,

  countQueens: number | null,
  countWhiteQueens: number | null,
  countBlackQueens: number | null,

  countKnights: number | null,
  countWhiteKnights: number | null,
  countBlackKnights: number | null,
}

interface StateAttributes {

  /**
   * @var isValidNumberRows
   * @describe number valid rows should be 8 as traditional chess game
   */
  isValidNumberRows: boolean,
  isValidChars: boolean,
  isExistsKings: boolean,
  isValidTimesPawns: boolean,
  isValidTimesKings: boolean,
  isValidTimesQueens: boolean,
  isValidTimesRooks: boolean,
  isValidTimesBishops: boolean,
  isValidTimesKnights: boolean,
  isValidNumberCharsBetweenDash: boolean,
  isValidParts: boolean,
  isValidPartsByPlayer: boolean
}

normalizer

normalizer transform a full FEN position to array FEN elements

const {normalizer} = require('fen-reader');

const FEN_POSITION = "rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 1 2";

const fenElements = normalizer(FEN_POSITION);

console.log( fenElements );
/*
[
  "rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR",
  "w",
  "KQkq",
  "-",
  "1",
  "2"
]
*/

if elements missing after position normalizer considerate as null

const {normalizer} = require('fen-reader');

const FEN_POSITION = "rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq";

const fenElements = normalizer(FEN_POSITION);

console.log( fenElements );
/*
[
  "rnbqkb1r/pppppppp/5n2/8/4P3/8/PPPP1PPP/RNBQKBNR",
  "w",
  "KQkq",
  null,
  null,
  null
]
*/

create fen reader

createFenReader is a wrap of FEN converters/checker/scanner position

createFenReader(position?: string): FenReaderInterface

const {createFenReader} = require('fen-reader')

const fenReader = createFenReader()

createFenReader return object implement the above FenReaderInterface

interface FenReaderInterface {

  /**
   * @param position string | Array<string[]> | {[keyname: string]: string}
   * @description change current fen position from multiple format recognize by fen converters
   */
  setPosition( position: string | Array<string[]> | {[keyname: string]: string} ): boolean,

  readonly position: FenReaderPosition,

  /**
   * @var scan
   * @describe null while not position define
   * @see ScanResponse
   */
  readonly scan: ScanResponse | null,

  // status castlings
  readonly whiteCastling: string | null,
  readonly blackCastling: string | null,
}

interface FenReaderPosition {

  // all attributes null while not position define

  readonly fen: string | null,
  readonly array: Array<string[]> | null,
  readonly json: {[keyname: string]: string} | null
}