Skip to content

Commit

Permalink
Merge pull request #189 from allanjoseph98/rolechar
Browse files Browse the repository at this point in the history
add `RoleChar` type
  • Loading branch information
niklasf authored Dec 14, 2024
2 parents bd1449c + 70bfa4e commit edd0823
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/fen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const parsePiece = (str: string): Piece | undefined => {
};

export const makePiece = (piece: Piece): string => {
let r = roleToChar(piece.role);
let r: string = roleToChar(piece.role);
if (piece.color === 'white') r = r.toUpperCase();
if (piece.promoted) r += '~';
return r;
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export type Square = number;

export type SquareName = `${FileName}${RankName}`;

export const ROLE_CHARS = ['q', 'n', 'r', 'b', 'p', 'k'] as const;

export type LowerCaseRoleChar = (typeof ROLE_CHARS)[number];

export type RoleChar = LowerCaseRoleChar | Uppercase<LowerCaseRoleChar>;

/**
* Indexable by square indices.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
FILE_NAMES,
isDrop,
isNormal,
type LowerCaseRoleChar,
Move,
RANK_NAMES,
Role,
type RoleChar,
Square,
SquareName,
} from './types.js';
Expand All @@ -22,7 +24,7 @@ export const squareFile = (square: Square): number => square & 0x7;
export const squareFromCoords = (file: number, rank: number): Square | undefined =>
0 <= file && file < 8 && 0 <= rank && rank < 8 ? file + 8 * rank : undefined;

export const roleToChar = (role: Role): string => {
export const roleToChar = (role: Role): LowerCaseRoleChar => {
switch (role) {
case 'pawn':
return 'p';
Expand All @@ -39,7 +41,7 @@ export const roleToChar = (role: Role): string => {
}
};

export function charToRole(ch: 'p' | 'n' | 'b' | 'r' | 'q' | 'k' | 'P' | 'N' | 'B' | 'R' | 'Q' | 'K'): Role;
export function charToRole(ch: RoleChar): Role;
export function charToRole(ch: string): Role | undefined;
export function charToRole(ch: string): Role | undefined {
switch (ch.toLowerCase()) {
Expand Down

0 comments on commit edd0823

Please sign in to comment.