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

poker-hands

v1.0.2

Published

Quickly evaluate poker hands

Downloads

25

Readme

poker-hands

NPM Version Build Status

Quickly evaluate poker hands.

Rules

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of same suit.
  • Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order:

  • 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

Install

$ npm install poker-hands
var poker = require('poker-hands');

Usage

A hand is represented as a string of 5 pairs of characters. For example:

'5H 5C 6S 7S KD' // is a hand with
// 5 of Hearts
// 5 of Clubs
// 6 of Spades
// 7 of Spades
// King of Diamonds


'2C 3S 8S 8D TD' // is a hand with
// 2 of Clubs
// 3 of Spades
// 8 of Spades
// 8 of Diamonds
// Ten of Diamonds

.judgeWinner([hand, hand])

Given a tuple of two hands, return the index of the stronger hand.

poker.judgeWinner(['5H 5C 6S 7S KD', '2C 3S 8S 8D TD']); // 1
poker.judgeWinner(['5D 8C 9S JS AC', '2C 5C 7D 8S QH']); // 0
poker.judgeWinner(['2D 9C AS AH AC', '3D 6D 7D TD QD']); // 1
poker.judgeWinner(['4D 6S 9H QH QC', '3D 6D 7H QD QS']); // 0
poker.judgeWinner(['2H 2D 4C 4D 4S', '3C 3D 3S 9S 9D']); // 0

.highestCard(hand)

Return an array of the cards' values, sorted strongest to weakest.

poker.highestCards('5D AC JS 8C 9S'); // ['A', 'J', '9', '8', '5']
poker.highestCards('2C 5C 7D 8S QH'); // ['Q', '8', '7', '5', '2']

.hasPair(hand)

If the hand has a pair, return the value of the pair, otherwise return undefined.

poker.hasPair('5H 5C 6S 7S KD'); // '5'
poker.hasPair('2C 3S 8S 8D TD'); // '8'
poker.hasPair('2C 5C 7D 8S QH'); // undefined

.hasTwoPairs(hand)

If the hand has a two pairs, return an array of the values of the pairs with the strongest first, otherwise return undefined.

poker.hasTwoPair('3H 9C 9S 2D 3D'); // ['9', '3']
poker.hasTwoPair('7S 8H 8C KH KS'); // ['K', '8']
poker.hasTwoPair('5H 5C 6S 7S KD'); // undefined

.hasThreeOfAKind(hand)

If the hand has a three of a kind, return the value of the three of a kind, otherwise return undefined.

poker.hasThreeOfAKind('3H 9C 9S 2D 9D'); // '9'
poker.hasThreeOfAKind('7S 8H 8C KH KS'); // undefined

.hasStraight(hand)

If the hand has a straight, return the highest value in the straight, otherwise return undefined.

poker.hasStraight('8D 6C 5S 7H 4S'); // '8'
poker.hasStraight('7S 8H 8C KH KS'); // undefined

.hasFlush(hand)

If the hand has a flush, return the suit of the flush, otherwise return undefined.

poker.hasFlush('3D 6D 7D TD QD'); // 'D'
poker.hasFlush('7S 8H 8C KH KS'); // undefined

.hasFullHouse(hand)

If the hand has a full house, return the value of the three of a kind, otherwise return undefined.

poker.hasFullHouse('2H 2D 4C 4D 4S'); // '4'
poker.hasFullHouse('3C 3D 3S 9S 9D'); // '3'

.hasFourOfAKind(hand)

If the hand has a four of a kind, return the value of the four of a kind, otherwise return undefined.

poker.hasFourOfAKind('9D 9S KH 9H 9C'); // '9'
poker.hasFourOfAKind('3H 7C 7S 2D 7D'); // undefined

.hasStraightFlush(hand)

If the hand has a straight flush, return the highest value in the straight, otherwise return undefined.

poker.hasStraightFlush('4H 3H 6H 5H 7H'); // '7'
poker.hasStraightFlush('9D 9S KH 9H 9C'); // undefined

.hasRoyalFlush(hand)

Return a boolean for if the hand has a royal flush.

poker.hasRoyalFlush('AD KD JD QD TD'); // true
poker.hasRoyalFlush('4D 7D KD QD 2D'); // false

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test