pgn-manager
Version:
Libraray built on top of chess.js and pgn-parser to load and process PGN files in typescript.
69 lines (68 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.regeneratePGN = regeneratePGN;
/**
* Regenerates a PGN string from a ParsedPGN object
*/
function regeneratePGN(parsedPGN, moveColor) {
const lines = [];
// Add comments above header
if (parsedPGN.comments_above_header) {
parsedPGN.comments_above_header.forEach((comment) => {
lines.push(`{${comment.text}}`);
});
lines.push("");
}
// Add headers
if (parsedPGN.headers) {
parsedPGN.headers.forEach((header) => {
lines.push(`[${header.name} "${header.value}"]`);
});
lines.push("");
}
// Add comments between headers and moves
if (parsedPGN.comments) {
parsedPGN.comments.forEach((comment) => {
lines.push(`{${comment.text}}`);
});
lines.push("");
}
// Add moves
const movesText = formatMoves(parsedPGN.moves, moveColor);
if (movesText) {
lines.push(movesText);
}
// Add result
lines.push(parsedPGN.result);
return lines.join("\n");
}
/**
* Formats moves array into PGN move notation
*/
function formatMoves(moves, moveColor) {
const parts = [];
moves.forEach((move) => {
// Add move number for white moves
if (move.move_number !== undefined) {
parts.push(`${move.move_number}.`);
if (moveColor.get(move) === "b") {
parts[parts.length - 1] += "..";
}
}
// Add the move
parts.push(move.move);
// Add comments after the move
move.comments.forEach((comment) => {
parts.push(`{${comment}}`);
});
// Add variations (RAVs)
if (move.ravs) {
move.ravs.forEach((rav) => {
const ravMoves = formatMoves(rav.moves, moveColor);
const ravResult = rav.result ? ` ${rav.result}` : "";
parts.push(`(${ravMoves}${ravResult})`);
});
}
});
return parts.join(" ");
}