UNPKG

@alexjamesmalcolm/poker-odds-machine

Version:
132 lines 6.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shuffle = exports.uniqWith = exports.cleanInput = exports.validateInput = void 0; const constants_1 = require("./constants"); const types_1 = require("./types"); function validateInput(input) { var _a, _b, _c; // numPlayers or hands if (!('numPlayers' in input) && !('hands' in input)) throw new Error(`Either "numPlayers" or "hands" must be provided.`); // returnHandStats if ('returnHandStats' in input) { if (typeof input.returnHandStats !== 'boolean') throw new Error(`"returnHandStats" must be a boolean. Invalid: ${input.returnHandStats}`); } // returnTieHandStats if ('returnTieHandStats' in input) { if (typeof input.returnTieHandStats !== 'boolean') throw new Error(`"returnTieHandStats" must be a boolean. Invalid: ${input.returnTieHandStats}`); } // numPlayers if ('numPlayers' in input) { if (typeof input.numPlayers !== 'number' || !Number.isSafeInteger(input.numPlayers) || input.numPlayers < 1) throw new Error(`"numPlayers" must be an integer greater than 0. Invalid: ${input.numPlayers}`); if (types_1.isInputAnExternalInputWithHands(input) && input.numPlayers < input.hands.length) throw new Error(`"numPlayers" must be equal to or greater than number of hands. Invalid: ${input.numPlayers} | ${input.hands}`); } // boardSize if ('boardSize' in input) { if (typeof input.boardSize !== 'number' || !Number.isSafeInteger(input.boardSize) || input.boardSize < 0) throw new Error(`"boardSize" must be a positive integer. Invalid: ${input.boardSize}`); } // numDecks if ('numDecks' in input) { if (typeof input.numDecks !== 'number' || !Number.isSafeInteger(input.numDecks) || input.numDecks < 1) throw new Error(`"numDecks" must be an integer greater than 0. Invalid: ${input.numDecks}`); } // board if ('board' in input) { if (typeof input.board !== 'string') throw new Error(`"board" must be a string. Invalid: ${input.board}`); const cardGroup = new types_1.CardGroup(input.board); if ('boardSize' in input) { if (cardGroup.cards.length > ((_a = input.boardSize) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_BOARD_SIZE)) throw new Error(`"board" cannot contain more than "boardSize" number of cards. Invalid: ${input.board} on boardSize ${input.boardSize}`); } else { if (cardGroup.cards.length > 5) throw new Error(`"board" cannot contain more than 5 cards. Invalid: ${input.board}`); } } // iterations if ('iterations' in input) { if (typeof input.iterations !== 'number' || !Number.isSafeInteger(input.iterations) || input.iterations < 1) throw new Error(`"iterations" must be a string. Invalid: ${input.iterations}`); } // handSize if ('handSize' in input) { if (typeof input.handSize !== 'number' || !Number.isSafeInteger(input.handSize) || input.handSize < 0) throw new Error(`"handSize" must be a positive integer. Invalid: ${input.handSize}`); } // hands if ('hands' in input) { if (!Array.isArray(input.hands)) throw new Error(`"hands" must be an array of strings like ["5c,Th"]. Invalid: ${input.hands}`); for (const hand of input.hands) { const cardGroup = new types_1.CardGroup(hand); if (!('handSize' in input)) { if (cardGroup.cards.length > 2) throw new Error(`Each hand must specify at most 2 cards. Invalid ${hand}`); } else { if (cardGroup.cards.length > ((_b = input.handSize) !== null && _b !== void 0 ? _b : constants_1.DEFAULT_HAND_SIZE)) throw new Error(`Each hand must specify at most ${(_c = input.handSize) !== null && _c !== void 0 ? _c : constants_1.DEFAULT_HAND_SIZE} cards. Invalid ${hand}`); } } } // hands + board must be unique const allCards = []; if (types_1.isInputAnExternalInputWithHands(input)) input.hands.forEach((e) => allCards.push(...e.split(','))); if (input.board) allCards.push(...input.board.split(',')); if (new Set(allCards).size !== allCards.length) throw new Error(`Input cards must be unique. Invalid: ${allCards}`); } exports.validateInput = validateInput; function cleanInput(input) { var _a, _b, _c, _d, _e, _f, _g; return { board: (_a = input.board) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_BOARD, boardSize: (_b = input.boardSize) !== null && _b !== void 0 ? _b : constants_1.DEFAULT_BOARD_SIZE, handSize: (_c = input.handSize) !== null && _c !== void 0 ? _c : constants_1.DEFAULT_HAND_SIZE, hands: types_1.isInputAnExternalInputWithHands(input) ? input.hands : [], iterations: (_d = input.iterations) !== null && _d !== void 0 ? _d : constants_1.DEFAULT_ITERATIONS, numDecks: (_e = input.numDecks) !== null && _e !== void 0 ? _e : constants_1.DEFAULT_NUMBER_OF_DECKS, numPlayers: types_1.isInputAnExternalInputWithNumberOfPlayers(input) ? input.numPlayers : input.hands.length, returnHandStats: (_f = input.returnHandStats) !== null && _f !== void 0 ? _f : false, returnTieHandStats: (_g = input.returnTieHandStats) !== null && _g !== void 0 ? _g : false, }; } exports.cleanInput = cleanInput; exports.uniqWith = (arr, comparator) => { const uniques = []; for (const a of arr) { if (uniques.findIndex((u) => comparator(a, u)) === -1) uniques.push(a); } return uniques; }; function shuffle(arr) { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arr[i], arr[j]] = [arr[j], arr[i]]; } } exports.shuffle = shuffle; //# sourceMappingURL=util.js.map