A collection of TypeScript types and basic introspection functions pertaining to a deck of cards.
Add @poker-apprentice/types
as a dependency.
- yarn:
yarn add @poker-apprentice/types
- npm:
npm install @poker-apprentice/types --save
Any string that represents a card's rank (i.e.: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", or "A").
import type { Rank } from '@poker-apprentice/types';
const rank: Rank = 'J'; // jack
Any string that represents a card's suit.
- "c" = clubs ♣
- "d" = diamonds ♦
- "h" = hearts ♥
- "s" = spades ♠
import type { Suit } from '@poker-apprentice/types';
const suit: Suit = 'c'; // clubs
A 2-character string representing the rank & suit of a card. (e.g.: "Ts" for the 10 of spades).
import type { Card } from '@poker-apprentice/types';
const card: Card = 'Kd'; // king of diamonds
An array of cards.
import type { Hand } from '@poker-apprentice/types';
const hand: Hand = [
'Ad', // ace of diamonds
'Qh', // queen of hearts
'3s', // three of spades
'5d', // five of diamonds
'Th', // ten of hearts
];
All possible high hand strengths in a standard game of poker.
import { HandStrength } from '@poker-apprentice/types';
console.log(HandStrength.HighCard); // 0
console.log(HandStrength.OnePair); // 1
console.log(HandStrength.TwoPair); // 2
console.log(HandStrength.ThreeOfAKind); // 3
console.log(HandStrength.Straight); // 4
console.log(HandStrength.Flush); // 5
console.log(HandStrength.FullHouse); // 6
console.log(HandStrength.FourOfAKind); // 7
console.log(HandStrength.StraightFlush); // 8
console.log(HandStrength.RoyalFlush); // 9
Determines if a string is a valid representation of a Rank
, acting as a type guard.
console.log(isRank('A')); // true
console.log(isRank('K')); // true
console.log(isRank('a')); // false -- only uppercase "A" accepted as rank
console.log(isRank('P')); // false -- "P" is not a rank
console.log(isRank('hello')); // false
Determines if a string is a valid representation of a Suit
, acting as a type guard.
console.log(isSuit('s')); // true
console.log(isSuit('d')); // true
console.log(isSuit('S')); // false -- only lowercase "s" accepted as rank
console.log(isSuit('p')); // false -- "p" is not a suit
console.log(isSuit('hello')); // false
Determines if a string is a valid representation of a Card
, acting as a type guard.
console.log(isCard('As')); // true
console.log(isCard('Kd')); // true
console.log(isCard('AS')); // false -- only lowercase "s" accepted as suit
console.log(isCard('Kp')); // false -- "p" is not a suit
console.log(isCard('10h')); // false -- rank 10 must be represented as "T"
console.log(isCard('hello')); // false
A more rigid implementation of isCard
, throwing if the provided string value is not a valid representation of a Card
, and acting as an assertion type guard.
const cards = ['As', 'Kd', '5h', '2c'].map(assertCard);
// `cards` is typed as `Card[]` with all values provided
try {
assertCard('10c');
} catch (err) {
console.log(err);
}
// outputs error of type `InvalidCardError` with message: Invalid card: "10c"
Extracts the Rank
type value from a Card
.
console.log(getRank('As')); // "A"
console.log(getRank('Th')); // "T"
console.log(getRank('6d')); // "6"
Extracts the Suit
type value from a Card
.
console.log(getRank('As')); // "s"
console.log(getRank('Th')); // "h"
console.log(getRank('6d')); // "d"
An array of all possible Card
values.
An array of all possible Rank
values.
An array of all possible Suit
values.
MIT
If you'd like to fix a bug, add a feature, improve the documentation, or anything else to better this library, pull requests are welcomed!
- Clone the repository:
git clone git@github.com:poker-apprentice/types.git
- Install dependencies:
yarn install
- Include tests for your changes, and open a pull request.
If you are interested in contributing, but you are stuck or lost at any point in your efforts, please reach out for help!