https://github.com/creativenull/deckjs
A simple card deck library written in TypeScript
https://github.com/creativenull/deckjs
card deck javascript typescript
Last synced: 9 months ago
JSON representation
A simple card deck library written in TypeScript
- Host: GitHub
- URL: https://github.com/creativenull/deckjs
- Owner: creativenull
- License: mit
- Created: 2020-06-19T16:32:02.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-04-03T17:14:05.000Z (over 1 year ago)
- Last Synced: 2024-10-05T22:41:05.938Z (about 1 year ago)
- Topics: card, deck, javascript, typescript
- Language: TypeScript
- Homepage:
- Size: 267 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deck.js
A simple card deck library written in TypeScript.
## Installation
```sh
npm install @creativenull/deckjs
```
### Deno
```ts
import { Deck } from "jsr:@creativenull/deckjs@2"
```
## Test
```sh
npm run test
```
# Documentation
## Create a new deck
Create a newly shuffled deck
```ts
import { Deck } from "@creativenull/deckjs"
const deck = new Deck();
```
By default, you get a shuffled deck, to get a sorted deck pass `false` when
creating new `Deck` or call `deck.sort()`.
```ts
import { Deck } from "@creativenull/deckjs"
// Sorted deck
const deck = new Deck(false);
// OR
const sortedDeck = deck.sort();
```
## Get cards from the deck
Get the cards from the deck by calling the `getCards` method and providing an amount.
If no amount was given then it will return an empty array.
```ts
// Get 5 cards from the deck
const cards = deck.getCards(5); // [{ id: 22, value: '8', rank: 8, suit: {...} }, ...]
```
## Serialize/Deserialize cards
If you want to pass the cards around different components and but want to pass data without
the heavy object array, you can serialize it and then parse it later on.
Use the static methods to stringify/parse the cards. Note: we are using the class name `Deck`
and not a class instance.
```ts
// Serialize/Stringify
const cardsSerialize = Deck.stringify(cards); // ['22#8H', '99#3D', ...]
// Deserialize/Parse
const cardsParsed = Deck.parse(cardsSerialize);
// output: [{ id: 22, value: '8', rank: 8, suit: {...} }, {...}, ...]
```
## Get Text instance of a card
Using the card object we can get the text equivalent of a card.
```ts
const oneCard = deck.getCards(1);
const cardText = Deck.getCardText(oneCard); // 'Four'
```
We can get the Suit text as well.
```ts
const suitText = Deck.getSuitText(oneCard); // 'Spades'
```
And finally we can get the card description.
```ts
const cardDesc = Deck.getCardDescription(oneCard); // 'Four of Spades'
```