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.
| Function | Signature | Description |
|---|---|---|
formatContract | (contract: DisplayContract | null) => string | "<level><suit>" followed by X or XX when doubled. Empty string for null. |
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.
| Function | Signature | Description |
|---|---|---|
formatContractOutcome | (level: number, denomination: Denomination, actualDeclarerTricks: number | null) => string | Contract plus its result relative to the book: = when made exactly, +N for overtricks, -N when down. null tricks returns the bare contract. |
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.
| Function | Signature | Description |
|---|---|---|
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. |
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.
| Export | Signature | Description |
|---|---|---|
StringHandData | Record<Suit, string[]> | One hand as string arrays. |
StringHandsData | Record<Player, StringHandData> | Four hands as string arrays. |
parseFormHands | (hands: StringHandsData) => HandsData | Validates every entry with isRank; throws Error("Invalid rank: …") on the first invalid value. |
handsToStringArrays | (hands: HandsData) => StringHandsData | Copies the arrays back to the form shape. |
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 arraysplay
Import from @workspace/bridge/format/play.
| Function | Signature | Description |
|---|---|---|
getLeadCard | (moves: PlayMove[]) => Card | null | First card of the play, or null when nothing has been played. |
getLeadCard(new PlayBuilder().fromCompact("W / DA DK D9 D3").build().moves); // => "DA"
getLeadCard([]); // => nullutils/contract
Import from @workspace/bridge/utils/contract.
| Function | Signature | Description |
|---|---|---|
formatContractForIndex | (contract: DisplayContract | null | undefined) => string | null | formatContract that yields null instead of "" for a missing contract, for indexed or searchable fields. |
getTrumpFromContract | (contract: string | null) => Suit | null | Parses a contract string and returns its suit; null for No Trump or an invalid string. |
formatContractForIndex({ level: 3, suit: "NT", doubled: "none" }); // => "3NT"
formatContractForIndex(null); // => null
getTrumpFromContract("4S"); // => "S"
getTrumpFromContract("2HXX"); // => "H"
getTrumpFromContract("3NT"); // => nullengine.format
engine.format has no configuration; it exposes one formatter.
| Member | Signature | Description |
|---|---|---|
formatContract | (contract: DisplayContract | null) => string | Same as above. |
const engine = createBridgeEngine(
createBridgeContext({ teachingMode: "auction", locale: "en" }),
);
engine.format.formatContract({ level: 4, suit: "S", doubled: "X" }); // => "4SX"