Bridge Core
Card
Card parsing, type guards, and sorting.
Import from @workspace/bridge-core/card.
Type Guards
import { isSuit, isRank, isCard } from '@workspace/bridge-core/card';
isSuit('H'); // true
isSuit('X'); // false
isRank('A'); // true
isRank('1'); // false
isCard('SA'); // true
isCard('XY'); // false| Function | Description |
|---|---|
isSuit(value) | Type guard for Suit — checks if string is S, H, D, or C |
isRank(value) | Type guard for Rank — checks if string is a valid rank |
isCard(value) | Checks if a string is a valid card (suit + rank) |
Parsing
import { parseCardString, parseCard, getSuitFromCard, getRankFromCard } from '@workspace/bridge-core/card';
parseCardString('SA'); // { suit: 'S', rank: 'A' }
parseCardString('XY'); // null
parseCard('SA'); // { suit: 'S', rank: 'A' }
getSuitFromCard('H7'); // 'H'
getRankFromCard('H7'); // '7'| Function | Description |
|---|---|
parseSuit(value) | Parses a string into a Suit or null |
parseRank(value) | Parses a string into a Rank or null |
parseCardString(value) | Parses a card string into suit + rank, or null if invalid |
parseCard(card) | Parses a Card (typed) into its suit and rank components |
getSuitFromCard(card) | Extracts the suit from a card |
getRankFromCard(card) | Extracts the rank from a card |
Sorting
import { sortRanksDefault, createRankSorter, DEFAULT_RANK_ORDER } from '@workspace/bridge-core/card';
sortRanksDefault(['3', 'A', 'K']); // ['A', 'K', '3']
const customSorter = createRankSorter(['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']);
customSorter(['A', '3', 'K']); // ['A', 'K', '3'] (sorted by custom order)| Function | Description |
|---|---|
sortRanksDefault(ranks) | Sorts ranks in standard bridge order (A high) |
createRankSorter(order) | Creates a custom rank sorter from a rank order array |
DEFAULT_RANK_ORDER | The default rank ordering constant |