poker-hand-utils v1.0.8
Poker Hand Utils
This utility library for evaluating poker hands an upgrade version of https://github.com/codeKonami/poker-hand with adding support for TypeScript.
Evaluate the score (the lower the better) of a poker hand as described in this post blog : http://suffe.cool/poker/evaluator.html. You can compare two poker hands to find out who wins or if it's a tie.
Installation
You can install the library using npm:
npm install poker-hand-utilsor with yarn:
yarn add poker-hand-utilsUsage
Importing the Library
First, import the necessary functions from the library:
import { createPokerHand, getDescription, handToString, getEvaluatedScore, getRank, updateHand, compareTo } from 'poker-hand-utils';Functions
createPokerHand
Creates a poker hand from a string.
Parameters:
handString: A string representing the poker hand (e.g., "AS KS QS JS TS").
Returns:
- An object representing the poker hand, including the hand, score, and rank.
Example:
const handString = "AS KS QS JS TS";
const pokerHand = createPokerHand(handString);
console.log(pokerHand);getDescription
Returns a description of the poker hand.
Parameters:
pokerHand: APokerHandobject.
Returns:
- An object containing the hand, score, and rank.
Example:
const description = getDescription(pokerHand);
console.log(description);handToString
Returns the poker hand as a string.
Parameters:
pokerHand: APokerHandobject.
Returns:
- A string representing the poker hand.
Example:
const handStr = handToString(pokerHand);
console.log(handStr); // Output: "AS KS QS JS TS"getEvaluatedScore
Returns the evaluated score of the poker hand.
Parameters:
pokerHand: APokerHandobject.
Returns:
- A number representing the score of the poker hand.
Example:
const score = getEvaluatedScore(pokerHand);
console.log(score); // Output: (some numeric score)getRank
Returns the rank of the poker hand.
Parameters:
pokerHand: APokerHandobject.
Returns:
- A string representing the rank of the poker hand.
Example:
const rank = getRank(pokerHand);
console.log(rank); // Output: (rank string based on score)updateHand
Updates the poker hand with a new hand string.
Parameters:
pokerHand: APokerHandobject.handString: A new hand string.
Returns:
- An updated
PokerHandobject.
Example:
const newHandString = "2D 3D 4D 5D 6D";
const updatedPokerHand = updateHand(pokerHand, newHandString);
console.log(updatedPokerHand);compareTo
Compares two poker hands.
Parameters:
hand1: APokerHandobject.hand2: APokerHandobject.
Returns:
1ifhand1wins,2ifhand2wins,3if it's a draw.
Example:
const anotherHandString = "2D 3D 4D 5D 6D";
const anotherPokerHand = createPokerHand(anotherHandString);
const result = compareTo(pokerHand, anotherPokerHand);
if (result === 1) {
console.log('Hand 1 wins');
} else if (result === 2) {
console.log('Hand 2 wins');
} else {
console.log('It's a draw');
}Contributing
Contributions are welcome! Please open an issue or submit a pull request for any changes.
License
This project is licensed under the MIT License. See the LICENSE file for details.