npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phe

v0.6.0

Published

Poker hand evaluator.

Downloads

195

Readme

phe build status

Poker hand evaluator, a port of PokerHandEvaluator.

It's super fast even though still 3x slower than the C version it was ported from, but hey you can use this directly with Node.js or even in the browser.

It's fast, cause it's using a hash algorithm relying on masking and tables. However the table data isn't huge like I've seen in other projects. This is possible because flushes are considered separately from other rankings

I highly recommend reading the informative writeup on how this all works.

const { rankBoard, rankDescription } = require('phe')

const board = 'As Ks 4h Ad Kd'
const rank = rankBoard(board)
const name = rankDescription[rank]

console.log('%s is a %s', board, name)
As Ks 4h Ad Kd is a Two Pair

Installation

npm install phe

Table of Contents generated with DocToc

API

evaluateCardCodes

Evaluates the 5 - 7 card codes to arrive at a number representing the hand strength, smaller is better.

Parameters

  • cards Array<Number> the cards, i.e. [ 49, 36, 4, 48, 41 ]

Returns Number the strength of the hand comprised by the card codes

evaluateCards

Evaluates the 5 - 7 cards to arrive at a number representing the hand strength, smaller is better.

Parameters

  • cards Array<String> the cards, i.e. [ 'Ah', 'Ks', 'Td', '3c', 'Ad' ]

Returns Number the strength of the hand comprised by the cards

evaluateCardsFast

Same as evaluateCards but skips cards argument type check to be more performant.

Parameters

  • cards

evaluateBoard

Evaluates the given board of 5 to 7 cards provided as part of the board to arrive at a number representing the hand strength, smaller is better.

Parameters

  • board String the board, i.e. 'Ah Ks Td 3c Ad'

Returns Number the strength of the hand comprised by the cards of the board

rankCards

Evaluates the 5 - 7 cards and then calculates the hand rank.

Parameters

  • cards Array<String> the cards, i.e. [ 'Ah', 'Ks', 'Td', '3c', 'Ad' ]

Returns Number the rank of the hand comprised by the cards, i.e. 1 for FOUR_OF_A_KIND (enumerated in ranks)

rankCardsFast

Same as rankCards but skips cards argument type check to be more performant.

Parameters

  • cards

rankCardCodes

Evaluates the 5 - 7 card codes and then calculates the hand rank.

Parameters

  • cardCodes Array<Number> the card codes whose ranking to determine

Returns Number the rank of the hand comprised by the card codes, i.e. 1 for FOUR_OF_A_KIND (enumerated in ranks)

rankBoard

Evaluates the given board of 5 to 7 cards provided as part of the board to and then calculates the hand rank.

Parameters

  • board String the board, i.e. 'Ah Ks Td 3c Ad'

Returns Number the rank of the hand comprised by the cards, i.e. 1 for FOUR_OF_A_KIND (enumerated in ranks)

setCardCodes

Converts a set of cards to card codes.

Parameters

  • set Set<String> card strings set, i.e. Set({'Ah', 'Ks', 'Td', '3c, 'Ad'})

Returns Set<Number> card code set

setStringifyCardCodes

Converts a set of card codes to their string representations.

Parameters

Returns Set<String> set with string representations of the card codes, i.e. Set({'Ah', 'Ks', 'Td', '3c, 'Ad'})

ranks

Enumeration of possible hand ranks, each rank is a number from 0-8.

STRAIGHT_FLUSH
FOUR_OF_A_KIND
FULL_HOUSE
FLUSH
STRAIGHT
THREE_OF_A_KIND
TWO_PAIR
ONE_PAIR
HIGH_CARD

rankDescription

Provides a description of a hand rank number. It's an {Array} which can be indexed into with the hand rank in order to retrieve the matching description.

Example: rankDescription[rank.FULL_HOUSE] === 'Full House'

handRank

Converts a hand strength number into a hand rank number 0 - 8 for STRAIGHT_FLUSH - HIGH_CARD.

Parameters

  • val Number hand strength (result of an evaluate function)

Returns Number the hand rank

rankCodes

The ranks of the cards sorted highest to lowest.

  • 2 = 0
  • 3 = 1
  • 4 = 2
  • 5 = 2
  • 6 = 4
  • 7 = 5
  • 8 = 6
  • 9 = 7
  • T = 8
  • J = 9
  • Q = 10
  • K = 11
  • A = 12

6 bits each.

Returns Object the ranks indexed as described above

suitCodes

The suitCodes

  • s = 0
  • h = 1
  • d = 2
  • c = 3

Returns Object the suits indexed as described above

stringifyCardCode

Converts the given card code into a string presentation.

Parameters

  • code Number the card code, i.e. obtained via cardCode(rank, suit).

Returns String a string representation of the card in question, i.e. Ah

stringifyRank

Converts the given rank index into a rank.

Parameters

  • rank Number the rank to stringify, i.e. 0b000100

Returns String the string of the rank, i.e. '2'

stringifySuit

Converts the given suit index to a suit.

Parameters

  • suit Number the suit to stringify, i.e. 0b1

Returns String the string of the sut, i.e. 'c' (clubs)

cardCode

Determines the code for the given hand.

Parameters

  • rank String the rank of the hand, i.e. A
  • suit String the suit of the hand, i.e. h

Returns Number the card code that can be used to further evaluate the hand

cardCodes

Determines the code for the given hands.

Parameters

  • cards Array<String> the cards to convert into card codes, i.e. [ 'Ah', 'Kd' ]

Returns Array<Number> the card codes that can be used to further evaluate the hands

boardCodes

Determines the code for the given hands.

Parameters

  • board String the board to convert into card codes, i.e. 'Ah Kd Qh'

Returns Array<Number> the card codes that can be used to further evaluate the hands

License

MIT