Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RoleChar type #189

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading