Skip to content

Commit 8c9f28c

Browse files
committed
feat(cardCollection): add helpers to reduce repeated code
1 parent f9e2bb3 commit 8c9f28c

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/models/cardCollection/cardCollection.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { ICard } from '../card/card.interface'
22

33
export interface ICardCollection {
44
getCards (): ICard[]
5+
addCard (card: ICard): ICardCollection
56
addCards (cards: ICard[]): ICardCollection
67
removeCards (cards: ICard[]): ICardCollection
78
takeCard (): ICard
89
takeCards (amount: number): ICard[]
910
getCount (): number
11+
hasCards (): boolean
1012
shuffle (): void
1113
}

src/models/cardCollection/cardCollection.model.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export class CardCollection implements ICardCollection {
2424

2525
}
2626

27+
public addCard (card: ICard): this {
28+
return this.addCards([card])
29+
}
30+
2731
public addCards (cards: ICard[]): this {
2832
this.getCards().unshift(...cards)
2933
return this
@@ -42,7 +46,7 @@ export class CardCollection implements ICardCollection {
4246
}
4347

4448
public takeCard (): ICard {
45-
if (this.getCount() > 0) {
49+
if (this.hasCards()) {
4650
return this.getCards().shift() as ICard
4751
}
4852
throw new Error('No cards remaining in pile.')
@@ -58,7 +62,7 @@ export class CardCollection implements ICardCollection {
5862
}
5963
// tslint:disable-next-line:prefer-const
6064
let pulledCards: ICard[] = []
61-
while (this.getCount() > 0 && pulledCards.length < amount) {
65+
while (this.hasCards() && pulledCards.length < amount) {
6266
pulledCards.push(this.getCards().shift() as ICard)
6367
}
6468
return pulledCards
@@ -77,6 +81,10 @@ export class CardCollection implements ICardCollection {
7781
return this.getCards().length
7882
}
7983

84+
public hasCards (): boolean {
85+
return this.getCount() > 0
86+
}
87+
8088
public shuffle (): void {
8189
this.setCards(this.shuffleService.shuffle(this.getCards()))
8290
}

src/models/cardCollection/cardPile.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ export class CardPile extends CardCollection implements ICardPile {
2020
}
2121

2222
public takeCardFromBottom (): ICard {
23-
if (this.getCount() > 0) {
23+
if (this.hasCards()) {
2424
return this.getCards().pop() as ICard
2525
}
2626
throw new Error('No cards remaining in pile.')
2727
}
2828

2929
public takeCardsFromBottom (amount: number): ICard[] {
3030
let pulledCards: ICard[] = []
31-
while (this.getCount() > 0 && pulledCards.length < amount) {
31+
while (this.hasCards() && pulledCards.length < amount) {
3232
pulledCards.push(this.getCards().pop() as ICard)
3333
}
3434
return pulledCards

src/models/cardCollection/hand.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class Hand extends CardCollection implements IHand {
3434
*/
3535
public sortCards (cardRank: IRankSet): this {
3636
const cards = this.getCards()
37-
if (cards.length === 0) {
37+
if (!this.hasCards()) {
3838
throw new Error('No cards to sort.')
3939
}
4040
if (this.suitOrder.length === 0) {

0 commit comments

Comments
 (0)