Bridge Training

Format

Display formatting for contracts, contract outcomes, auction cells, form hands and the opening lead, plus engine.format.

Formatting turns domain values into display strings or display-ready shapes. Each file is importable as @workspace/bridge/format/<file>.

contract

Import from @workspace/bridge/format/contract.

FunctionSignatureDescription
formatContract(contract: DisplayContract | null) => string"<level><suit>" followed by X or XX when doubled. Empty string for null.
format.ts
formatContract({ level: 3, suit: "NT", doubled: "none" }); // => "3NT"
formatContract({ level: 4, suit: "S", doubled: "X" }); // => "4SX"
formatContract({ level: 6, suit: "H", doubled: "XX" }); // => "6HXX"
formatContract(null); // => ""

Prop

Type

contract-outcome

Import from @workspace/bridge/format/contract-outcome.

FunctionSignatureDescription
formatContractOutcome(level: number, denomination: Denomination, actualDeclarerTricks: number | null) => stringContract plus its result relative to the book: = when made exactly, +N for overtricks, -N when down. null tricks returns the bare contract.
format.ts
formatContractOutcome(4, "S", 10); // => "4S="
formatContractOutcome(4, "S", 11); // => "4S+1"
formatContractOutcome(4, "S", 8); // => "4S-2"
formatContractOutcome(4, "S", null); // => "4S"

auction

Import from @workspace/bridge/format/auction.

FunctionSignatureDescription
auctionMovesToBidCells<TComment>(moves: AuctionMove<TComment>[], dealer: Player) => AuctionBidCell<TComment>[]Attaches the player (clockwise from the dealer) and the index to each move, for auction table components.
format.ts
const auction = new AuctionBuilder().fromCompact("N / 1NT pass 3NT AP").build();
const cells = auctionMovesToBidCells(auction.moves, auction.first);
cells[0]?.player; // => "N" (the dealer)
cells[0]?.bid; // => "1NT"
cells[1]?.player; // => "E"

hands

Import from @workspace/bridge/format/hands. Bridges the form representation (plain string arrays) and HandsData.

ExportSignatureDescription
StringHandDataRecord<Suit, string[]>One hand as string arrays.
StringHandsDataRecord<Player, StringHandData>Four hands as string arrays.
parseFormHands(hands: StringHandsData) => HandsDataValidates every entry with isRank; throws Error("Invalid rank: …") on the first invalid value.
handsToStringArrays(hands: HandsData) => StringHandsDataCopies the arrays back to the form shape.
format.ts
const formHands: StringHandsData = {
  N: { S: ["A", "K"], H: ["Q"], D: ["T", "9"], C: ["2"] },
  E: { S: ["J"], H: ["A", "K"], D: ["8"], C: ["A", "K"] },
  S: { S: ["Q", "T"], H: ["J"], D: ["A"], C: ["Q", "J"] },
  W: { S: ["9"], H: ["T", "9"], D: ["K", "Q"], C: ["T"] },
};

const hands: HandsData = parseFormHands(formHands);
handsToStringArrays(hands); // same shape, plain string arrays

play

Import from @workspace/bridge/format/play.

FunctionSignatureDescription
getLeadCard(moves: PlayMove[]) => Card | nullFirst card of the play, or null when nothing has been played.
format.ts
getLeadCard(new PlayBuilder().fromCompact("W / DA DK D9 D3").build().moves); // => "DA"
getLeadCard([]); // => null

utils/contract

Import from @workspace/bridge/utils/contract.

FunctionSignatureDescription
formatContractForIndex(contract: DisplayContract | null | undefined) => string | nullformatContract that yields null instead of "" for a missing contract, for indexed or searchable fields.
getTrumpFromContract(contract: string | null) => Suit | nullParses a contract string and returns its suit; null for No Trump or an invalid string.
format.ts
formatContractForIndex({ level: 3, suit: "NT", doubled: "none" }); // => "3NT"
formatContractForIndex(null); // => null

getTrumpFromContract("4S"); // => "S"
getTrumpFromContract("2HXX"); // => "H"
getTrumpFromContract("3NT"); // => null

engine.format

engine.format has no configuration; it exposes one formatter.

MemberSignatureDescription
formatContract(contract: DisplayContract | null) => stringSame as above.
format.ts
const engine = createBridgeEngine(
  createBridgeContext({ teachingMode: "auction", locale: "en" }),
);

engine.format.formatContract({ level: 4, suit: "S", doubled: "X" }); // => "4SX"