deck-of-cards-ts
Version:
Deck of cards package with TypeScript types.
71 lines • 2.73 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var constants_1 = require("./constants");
var Deck = /** @class */ (function () {
function Deck(numberOfFullDecks) {
if (numberOfFullDecks === void 0) { numberOfFullDecks = 1; }
this.cards = [];
this.numberOfFullDecks = numberOfFullDecks;
this.shuffle();
}
Deck.prototype.resetDeck = function () {
var _a;
for (var i = 0; i < this.numberOfFullDecks; i++) {
(_a = this.cards).push.apply(_a, this.getFullDeck());
}
};
Deck.prototype.getFullDeck = function () {
var cards = [];
var _loop_1 = function (suit) {
var _loop_2 = function (rank) {
var value = constants_1.RANK_TO_VALUE[rank];
cards.push({
suit: suit,
rank: rank,
value: Array.isArray(value) ? value[0] : value,
secondaryValue: Array.isArray(value) ? value[1] : null,
toString: function () { return "".concat(rank, " of ").concat(suit); },
});
};
for (var _a = 0, ALL_RANKS_1 = constants_1.ALL_RANKS; _a < ALL_RANKS_1.length; _a++) {
var rank = ALL_RANKS_1[_a];
_loop_2(rank);
}
};
for (var _i = 0, ALL_SUITS_1 = constants_1.ALL_SUITS; _i < ALL_SUITS_1.length; _i++) {
var suit = ALL_SUITS_1[_i];
_loop_1(suit);
}
return cards;
};
Deck.prototype.getCards = function () {
return __spreadArray([], this.cards, true);
};
Deck.prototype.shuffle = function () {
var _a;
this.resetDeck();
for (var i = this.cards.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
_a = [this.cards[j], this.cards[i]], this.cards[i] = _a[0], this.cards[j] = _a[1];
}
};
Deck.prototype.draw = function () {
var _a;
return (_a = this.cards.pop()) !== null && _a !== void 0 ? _a : null;
};
Deck.prototype.isThisYourCard = function (cardOne, cardTwo) {
return cardOne.rank === cardTwo.rank && cardOne.suit === cardTwo.suit;
};
return Deck;
}());
exports.default = Deck;
//# sourceMappingURL=deck.js.map