Skip to content

Commit

Permalink
Add method for writing/reading Deck to/from string (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta committed Aug 2, 2023
1 parent decff57 commit 118602b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/deck.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Card, Deck } from '../two-to-seven-triple-draw';
import { isSorted } from '../arr';
import { Cards } from '../card';

describe('Deck', (): void => {
const asCardsArray = (deck: Deck): Card[] => {
Expand Down Expand Up @@ -44,4 +45,16 @@ describe('Deck', (): void => {
expect(deck.popN(52)).toHaveLength(52);
expect(deck.cardsRemaining).toEqual(52 * 2);
});
it('provides method to read deck from string', (): void => {
const deck = Deck.fromString('_XYZabc');

expect(deck.cardsRemaining).toEqual(6);
const cards = deck.popN(6).reverse();
expect(cards).toEqual(new Cards([23, 24, 25, 26, 27, 28]));
});
it('provides method to write the deck into a string', (): void => {
const deck = Deck.fromString('Ah As Ad Ac');

expect(deck.toString()).toEqual('_ANan');
});
});
24 changes: 22 additions & 2 deletions src/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum CardSuits {
}

export enum CardStringType {
Char = 'CHAR',
Short = 'SHORT',
Long = 'LONG',
ShortEmoji = 'SHORT_EMOJI',
Expand Down Expand Up @@ -54,18 +55,34 @@ const stringToNum = (cardStr: string): number => {
return suitMap[suitKey] * 13 + rank;
};

const parseString = (input: string) => {
const charStrRe = /^_[a-z]+$/i;
if (input.match(charStrRe)) {
const chars = Array.from(input.slice(1));
return chars.map((i) => {
const c = i.charCodeAt(0);
// A = 65, a = 97
return c < 91 ? c - 65 : c - 97 + 26;
});
}
return input.split(/[\s,]+/);
};

export class Cards extends Array<Card> {
constructor(input: string | number | string[] | number[] | Card[]) {
if (typeof input === 'number') {
super(input);
} else {
super();
const arr = Array.isArray(input) ? input : input.split(/[\s,]+/);
const arr = Array.isArray(input) ? input : parseString(input);
arr.forEach((i) => this.push(new Card(i)));
}
}

public toString(stringType = Card.StringType.Long): string {
public toString(stringType = Card.StringType.Short): string {
if (stringType === CardStringType.Char) {
return '_' + this.map((card) => card.toString(stringType)).join('');
}
return this.map((card) => card.toString(stringType)).join(', ');
}
}
Expand Down Expand Up @@ -97,6 +114,7 @@ class Card {
public static readonly Suits = CardSuits;
public static readonly StringType = CardStringType;

private static chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
private static emojiSuits = ['♥', '♠', '♦', '♣'];
private static shortSuits = ['h', 's', 'd', 'c'];
private static longSuits = ['hearts', 'spades', 'diamonds', 'clubs'];
Expand Down Expand Up @@ -149,6 +167,8 @@ class Card {
' of ' +
this.toString(Card.StringType.LongSuit)
);
case Card.StringType.Char:
return Card.chars[this.num];
case Card.StringType.ShortSuit:
return Card.shortSuits[this.suit];
case Card.StringType.LongSuit:
Expand Down
12 changes: 11 additions & 1 deletion src/deck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Card, { CardJSON } from './card';
import Card, { CardJSON, Cards } from './card';
import { shuffle, isSorted } from './arr';

interface DeckJSON {
Expand All @@ -23,6 +23,12 @@ class Deck {
return deck;
}

public static fromString(input: string): Deck {
const deck = new Deck(false, 0);
deck.cards = new Cards(input);
return deck;
}

public shuffle(): void {
shuffle(this.cards);
}
Expand All @@ -49,6 +55,10 @@ class Deck {

return cards;
}

public toString(stringType = Card.StringType.Char): string {
return new Cards(this.cards).toString(stringType);
}
}

export default Deck;

0 comments on commit 118602b

Please sign in to comment.