UNPKG

poker-odds-calculator

Version:

A pre-flop and post-flop odds calculator for Texas Holdem

62 lines 1.7 kB
/** * CardGroup * * a group of card objects * (typically either a player's hand, or the shared board) */ import countBy from 'lodash/countBy.js'; import orderBy from 'lodash/orderBy.js'; import { Card } from './Card.js'; export class CardGroup extends Array { constructor() { super(); } static fromString(s) { const tmp = s.replace(/[^a-z0-9]/gi, ''); if (tmp.length % 2 !== 0) { throw new Error(`Invalid cards: ${s}`); } const cardgroup = new CardGroup(); for (let i = 0; i < tmp.length; i = i + 2) { cardgroup.push(Card.fromString(tmp.substring(i, i + 2))); } return cardgroup; } static fromCards(cards) { const cardgroup = new CardGroup(); for (const card of cards) { cardgroup.push(card); } return cardgroup; } contains(c) { for (const card of this) { if (card.equals(c)) { return true; } } return false; } toString() { return '' + this.join(' '); } sortCards(orderType) { const sorted = orderBy(this, ['rank', 'suit'], [orderType, orderType]); // eslint-disable-next-line @typescript-eslint/no-explicit-any this.splice.apply(this, [0, this.length].concat(sorted)); } concat(cardgroup) { const ret = new CardGroup(); for (const card of this) { ret.push(card); } for (const card of cardgroup) { ret.push(card); } return ret; } countBy(cardType) { return countBy(this, cardType); } } //# sourceMappingURL=CardGroup.js.map