-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create and use @shoki/card-deck
- Loading branch information
1 parent
1293aaa
commit 1bcf9de
Showing
10 changed files
with
356 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# card-deck | ||
|
||
A TypeScript card deck implementation, with shuffling using the [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). | ||
|
||
## Installation | ||
|
||
``` | ||
npm i @shoki/card-deck | ||
``` | ||
|
||
or | ||
|
||
``` | ||
yarn add @shoki/card-deck | ||
``` | ||
|
||
## Usage | ||
|
||
### Creating a deck | ||
|
||
When creating a deck, you must provide a generic parameter `TCard`. This indicates the type of card within the deck. | ||
|
||
```ts | ||
import { CardDeck } from "@shoki/card-deck"; | ||
|
||
type Card = { | ||
value: number; | ||
suit: string; | ||
}; | ||
|
||
const deck = new CardDeck<Card>(); | ||
``` | ||
|
||
### Adding cards | ||
|
||
You can add a card, or cards, to the deck with the `addCards` function. | ||
|
||
There is an optional boolean parameter `shouldShuffle` (which defaults to `true`) to indicate whether the deck should be shuffled (with the `shuffle` function described below) immediately after the addition. | ||
|
||
```ts | ||
// Add a single card | ||
deck.addCards({ value: 1, suit: "hearts" }); | ||
|
||
// Add a card without shuffling | ||
deck.addCards({ value: 2, suit: "hearts" }, false); | ||
|
||
// Add multiple cards | ||
deck.addCards([ | ||
{ value: 3, suit: "hearts" }, | ||
{ value: 4, suit: "hearts" }, | ||
]); | ||
|
||
// Add multiple cards without shuffling | ||
deck.addCards( | ||
[ | ||
{ value: 3, suit: "hearts" }, | ||
{ value: 4, suit: "hearts" }, | ||
], | ||
false | ||
); | ||
``` | ||
|
||
### Taking cards | ||
|
||
You can take a card, or multiple cards, from the deck with the `take` function. | ||
|
||
```ts | ||
// Take a single card | ||
const card = deck.take(); | ||
|
||
// Take multiple cards | ||
const cards = deck.take(2); | ||
``` | ||
|
||
### Shuffling the deck | ||
|
||
This uses the [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) through [`lodash.shuffle`](https://www.npmjs.com/package/lodash.shuffle). | ||
|
||
```ts | ||
// Shuffle the deck | ||
deck.shuffle(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { CardDeck } from "./index"; | ||
|
||
type Card = { | ||
value: number; | ||
suit: string; | ||
}; | ||
|
||
const deck = new CardDeck<Card>(); | ||
|
||
// Add a single card | ||
deck.addCards({ value: 1, suit: "hearts" }); | ||
|
||
// Add a card without shuffling | ||
deck.addCards({ value: 2, suit: "hearts" }, false); | ||
|
||
// Add multiple cards | ||
deck.addCards([ | ||
{ value: 3, suit: "hearts" }, | ||
{ value: 4, suit: "hearts" }, | ||
]); | ||
|
||
// Take a single card | ||
const card = deck.take(); | ||
|
||
// Take multiple cards | ||
const cards = deck.take(2); | ||
|
||
// Shuffle the deck | ||
deck.shuffle(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
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)); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
exports.__esModule = true; | ||
exports.CardDeck = void 0; | ||
var lodash_shuffle_1 = __importDefault(require("lodash.shuffle")); | ||
/** | ||
* A deck of cards | ||
* | ||
* @template TCard The type of card | ||
* | ||
* @author jameskmonger | ||
*/ | ||
var CardDeck = /** @class */ (function () { | ||
function CardDeck(deck) { | ||
this.deck = deck || []; | ||
} | ||
CardDeck.prototype.take = function (count) { | ||
if (count === void 0) { count = 1; } | ||
return this.deck.splice(this.deck.length - count, count); | ||
}; | ||
/** | ||
* Add a number of cards to the top of the deck | ||
* | ||
* Shuffles the deck after adding by default (see `shouldShuffle` parameter) | ||
* | ||
* @param cards The cards to add | ||
* @param shouldShuffle Whether to shuffle the deck after adding | ||
*/ | ||
CardDeck.prototype.addCards = function (cards, shouldShuffle) { | ||
var _a; | ||
if (shouldShuffle === void 0) { shouldShuffle = true; } | ||
// TODO null check? | ||
if (Array.isArray(cards)) { | ||
(_a = this.deck).push.apply(_a, __spreadArray([], __read(cards), false)); | ||
} | ||
else { | ||
this.deck.push(cards); | ||
} | ||
if (shouldShuffle) { | ||
this.shuffle(); | ||
} | ||
}; | ||
/** | ||
* Shuffle the deck using lodash.shuffle (Fisher-Yates) | ||
*/ | ||
CardDeck.prototype.shuffle = function () { | ||
this.deck = (0, lodash_shuffle_1["default"])(this.deck); | ||
}; | ||
return CardDeck; | ||
}()); | ||
exports.CardDeck = CardDeck; |
Oops, something went wrong.