card-games-utils
Version:
Utility package for card games
178 lines (177 loc) • 6.91 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 });
exports.StandardCardHelper = void 0;
var StandardDeck_1 = require("../data/StandardDeck");
/**
* Class for helper methods related to Standard card.
* @class
*/
var StandardCardHelper = /** @class */ (function () {
function StandardCardHelper() {
}
/**
* @method
* @static
* generate the StandardCard interface instance based on the card name given
*
* @param {keyof typeof StandardCardName} cardName name of card
* @returns {StandardCard} - instance of StandardCard interface based on given cardName
*/
StandardCardHelper.makeStandardCard = function (cardName) {
var card = {
name: cardName,
color: StandardDeck_1.StandardDeck.getColor(cardName),
number: StandardDeck_1.StandardDeck.getNumber(cardName),
rank: StandardDeck_1.StandardDeck.getRank(cardName),
suite: StandardDeck_1.StandardDeck.getSuite(cardName),
};
return card;
};
/**
* @method
* @static
* sort the given Array of Card in ascending order of it's number
*
* @param {StandardCard[]} cards - the array of cards to be sorted
* @returns {StandardCard[]} - the sorted array of cards
*/
StandardCardHelper.sortCards = function (cards) {
var tempCards = __spreadArray([], cards, true);
return tempCards.sort(function (currentCard, nextCard) { return currentCard.number - nextCard.number; });
};
/**
* @method
* @static
* decide if all the cards has same suite or not
*
* @param {StandardCard[]} cards - the array of cards to be checked
* @returns {boolean} - the boolean indicating that cards has same suite or not
*/
StandardCardHelper.hasSameSuite = function (cards) {
return cards.every(function (card) {
return cards.every(function (otherCard) {
return card.suite === otherCard.suite;
});
});
};
/**
* @method
* @static
* decide if there is any pair in the given cards that has same suite or not
*
* @param {StandardCard[]} cards - the array of cards to be checked
* @returns {boolean} - the boolean indicating that cards has pair of same suite or not
*/
StandardCardHelper.hasPairSuite = function (cards) {
return cards.some(function (card, index) {
return cards.some(function (otherCard, otherIndex) {
return index !== otherIndex && card.suite === otherCard.suite;
});
});
};
/**
* @method
* @static
* decide if all the cards has same number or not
*
* @param {StandardCard[]} cards the array of cards to be checked
* @returns {boolean} the boolean indicating that cards has same number or not
*/
StandardCardHelper.hasSameNumber = function (cards) {
return cards.every(function (card) {
return cards.every(function (otherCard) {
return card.number === otherCard.number;
});
});
};
/**
* @method
* @static
* decide if there is any pair in the given cards that has same number or not
*
* @param {StandardCard[]} cards the array of cards to be checked
* @returns {boolean} the boolean indicating that cards has pair of same number or not
*/
StandardCardHelper.hasPairNumber = function (cards) {
return cards.some(function (card, index) {
return cards.some(function (otherCard, otherIndex) {
return index !== otherIndex && card.number === otherCard.number;
});
});
};
/**
* @method
* @static
* decide if the given array has the card given in other parameter
*
* @param {StandardCard[]} cards the array of cards to be checked
* @param {StandardCardName} cardToFind the StandardCardName that needs to be found
* @returns {number} the index of the card in array, -1 if it doesn't exist
*/
StandardCardHelper.isInDeck = function (cards, cardToFind) {
return cards.findIndex(function (card) { return card.name === cardToFind; });
};
/**
* @method
* @static
* decide if the given array has the card of given NUMBER in other parameter
*
* @param {StandardCard[]} cards the array of cards to be checked
* @param {number} cardNumberToFind the number that's card needs to be found
* @returns {number} the index of the card in array, -1 if it doesn't exist
*/
StandardCardHelper.isNumberInDeck = function (cards, cardNumberToFind) {
return cards.findIndex(function (card) { return card.number === cardNumberToFind; });
};
/**
* @method
* @static
* returns the count of particular card from the given cards
*
* @param {StandardCard[]} cards the array of cards to be checked
* @param {StandardCardName} cardToFind the StandardCardName that needs to be found
* @returns {number} the count indicating how many times does that card appear in the given cards(0 if it doesn't exist)
*/
StandardCardHelper.getCountFromDeck = function (cards, cardToFind) {
return cards.reduce(function (count, card) {
if (card.name === cardToFind) {
return count + 1;
}
return count;
}, 0);
};
/**
* @method
* @static
* checks if the given cards has any duplicate cards or not
*
* @param {StandardCard[]} cards the array of cards to be checked
* @param {StandardCardName} cardToFind the StandardCardName that needs to be found
* @param {StandardCard[]} cardsToIgnore array of cards that should be ignored
* @returns {boolean} indicate that is card has duplicate card or not.
*/
StandardCardHelper.hasDuplicates = function (cards, cardsToIgnore) {
if (cardsToIgnore === void 0) { cardsToIgnore = []; }
return cards.some(function (card, index) {
return cards.some(function (otherCard, otherIndex) {
if (cardsToIgnore.some(function (obj) { return obj.name === otherCard.name; })) {
return false;
}
else {
return index !== otherIndex && card.name === otherCard.name;
}
});
});
};
return StandardCardHelper;
}());
exports.StandardCardHelper = StandardCardHelper;