Reference
Deck tools
import { … } from "@johnmorrisdotca/toranpu/deck";
1.5 kB minified and gzipped, with everything it imports (measured with esbuild)
The deck on its own: fifty-two cards as objects ({ suit, rank }), a seeded
shuffle, dealing, one-character and two-character codes, and the names a
card is written and spoken by. For a solitaire or any game of your own.
Exports
| Name | Kind | What it is |
|---|---|---|
Card | type | A card as an object: its suit and its rank. |
CARD_ALPHABET | constant | The fifty-two characters a card is written as, in deck order: spades A–K are A–M, hearts N–Z, diamonds a–m, clubs n–z. |
cardAt | function | The card at a place in a fresh deck, 0 to 51. |
cardCode | function | One character for a card (CARD_ALPHABET). |
CardCode | type | A card as ONE character, A–Z then a–z (cardCode): a whole deck is a fifty-two-character string, so a deal travels in a saved game, an address and a move list without any separator to parse. |
cardFromCode | function | The card a character names, or null for one that names none — a kept string is read, never trusted. |
cardFromId | function | The card a two-character id names, or null. |
cardId | function | "QS", "TH", "AC": the two-character id card players write, rank then suit, with T for the ten. |
cardIndex | function | The card's place in a fresh deck, 0 to 51. |
cardName | function | "queen of hearts": the name a screen reader says. |
cardShortName | function | "Q♥": the short name a corner shows. |
colourOf | function | A card's colour: red for hearts and diamonds, black for spades and clubs. |
dealAll | function | Deal the whole deck round the table: the first hands get one more where it does not divide evenly. |
dealRound | function | Deal one card at a time round the table, as a dealer does: hands hands of each cards, dealt from the top (the start of the array), and what is left. |
DECK_SIZE | constant | How many cards a deck holds: fifty-two. |
freshDeck | function | A fresh deck in the order a new pack is sorted: spades ace to king, then hearts, diamonds, clubs. |
isRed | function | Whether a card is a heart or a diamond. |
isWholeDeck | function | Whether a string is a whole deck: fifty-two cards, each exactly once. |
Random | type | A number in [0, 1), like Math.random, from a stream a seed fixes. |
Rank | type | Ace is 1 and King 13. |
RANK_DISPLAY | constant | How each rank is written: short (A, 10, K) and as a word (ace, ten, king). |
RankDisplay | type | How a rank is written: A, 2…10, J, Q, K, and in a sentence. |
RANKS | constant | The ranks in order, ace low: 1 (the ace) to 13 (the king). |
readCards | function | A string of card characters read back, or null if any character names no card. |
sameCard | function | Whether two card objects are the same card. |
seededRandom | function | A stream of numbers in [0, 1) fixed by a seed. |
shuffled | function | A copy of the list in a random order (Fisher–Yates); the list given is left alone. |
shuffledDeck | function | A deck shuffled from a seed: the same seed, the same order, in every browser and every test. |
sortedHand | function | A hand sorted by suit (the fresh-deck order) and then by rank: how a player arranges one. |
Suit | type | One of the four suits, by name. |
SUIT_DISPLAY | constant | How each suit is written and drawn: its symbol, its name and its colour. |
SuitColour | type | A suit's colour, which Klondike's alternating runs and Crazy Eights' matches both read. |
SuitDisplay | type | How a suit is written and drawn. |
SUITS | constant | The suits in the order a fresh deck is sorted and a foundation row is laid out: spades, hearts, diamonds, clubs — black, red, red, black, so no two neighbours on a row of four look alike. |
take | function | Take count from the top: what was taken, and what is left. |
writeCards | function | A run of cards as a string, one character each. |