Skip to content

Commit 8c202b6

Browse files
committed
refactor: introduce StringifyOptions, use it in stringify() instead of ParseOptions
1 parent 7337379 commit 8c202b6

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type {
1010
ParseOptions,
1111
ParseWarning,
1212
PGN,
13+
StringifyOptions,
1314
Piece,
1415
Result,
1516
SquareAnnotation,

src/stringify.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { RESULT_TO_STR } from './parse.js';
22

3-
import type { Eval, Meta, Move, MoveList, PGN, ParseOptions } from './types.js';
3+
import type {
4+
Eval,
5+
Meta,
6+
Move,
7+
MoveList,
8+
PGN,
9+
StringifyOptions,
10+
} from './types.js';
411

512
const STR_TAG_ORDER = [
613
'Event',
@@ -52,7 +59,7 @@ function applyIndicators(san: string, move: Move): string {
5259
return san;
5360
}
5461

55-
function stringifySAN(move: Move, options?: ParseOptions): string {
62+
function stringifySAN(move: Move, options?: StringifyOptions): string {
5663
if (move.castling) {
5764
if (KINGSIDE_SQUARES.has(move.to)) {
5865
return applyIndicators('O-O', move);
@@ -132,7 +139,7 @@ function stringifyEval(evaluation: Eval): string {
132139
return `[%eval ${evaluation.value.toFixed(2)}${depth}]`;
133140
}
134141

135-
function stringifyComment(move: Move, options?: ParseOptions): string {
142+
function stringifyComment(move: Move, options?: StringifyOptions): string {
136143
const parts: string[] = [];
137144

138145
if (move.arrows && move.arrows.length > 0) {
@@ -188,7 +195,7 @@ function hasAnnotation(move: Move): boolean {
188195
);
189196
}
190197

191-
function stringifyMoveList(moves: MoveList, options?: ParseOptions): string {
198+
function stringifyMoveList(moves: MoveList, options?: StringifyOptions): string {
192199
const tokens: string[] = [];
193200

194201
for (const pair of moves) {
@@ -252,7 +259,7 @@ function stringifyMoveList(moves: MoveList, options?: ParseOptions): string {
252259

253260
// ─── Public API ───────────────────────────────────────────────────────────────
254261

255-
function stringifyOne(game: PGN, options?: ParseOptions): string {
262+
function stringifyOne(game: PGN, options?: StringifyOptions): string {
256263
const tags = stringifyTags(game.meta);
257264
const movetext = stringifyMoveList(game.moves, options);
258265
const result = RESULT_TO_STR[String(game.result)] ?? '*';
@@ -261,7 +268,7 @@ function stringifyOne(game: PGN, options?: ParseOptions): string {
261268
return `${header}${movetext}${separator}${result}\n`;
262269
}
263270

264-
export function stringify(input: PGN | PGN[], options?: ParseOptions): string {
271+
export function stringify(input: PGN | PGN[], options?: StringifyOptions): string {
265272
if (Array.isArray(input)) {
266273
return input.map((game) => stringifyOne(game, options)).join('\n');
267274
}

src/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,14 @@ export interface ParseError {
6363
offset: number;
6464
}
6565

66-
export interface ParseOptions {
67-
onError?: (error: ParseError) => void;
66+
export interface StringifyOptions {
6867
onWarning?: (warning: ParseWarning) => void;
6968
}
7069

70+
export interface ParseOptions extends StringifyOptions {
71+
onError?: (error: ParseError) => void;
72+
}
73+
7174
export interface ParseWarning {
7275
column: number;
7376
line: number;

0 commit comments

Comments
 (0)