npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cardeck

v1.1.5

Published

A simple JavaScript library to manage "generic" decks of cards.

Downloads

15

Readme

Cardeck.js

npm Build Status npm npm

A simple JavaScript library to manage "generic" decks of cards.


Contents

Installation

To download Cardeck.js, you can do it via npm, using the following command:

npm install cardeck

If you want to save the dependency into the package.json file of your project, use:

npm install --save cardeck

when you download the package via npm, by default, it will be saved under the node_modules folder.

Usage

Cardeck.js works in both Node.js and browser JavaScript.

If you want to use Cardeck.js on Node.js:

var cardeck = require('cardeck');

var Card = cardeck.Card;
var Deck = cardeck.Deck;

// Now you can use both Card and Deck classes

If you want to use Cardeck.js on the browser:

<html>
    <head>...</head>
    <body>
        ...
        <script src="path_to_node_modules_folder/cardeck.js"></script>
        <script>
            var Card = cardeck.Card;
            var Deck = cardeck.Deck;

            // Now you can use both Card and Deck classes
        </script>
</html>

Methods

Card class methods

Card(value=0, type='default', imgPath='./card.png')

Constructor. Creates an instance of Card.

|Parameter|Description| |-------|-------------------| |value| Value of the card. (Optional, default value: 0)| |type | Type of the card. (Optional, default value: 'default')| |imgPath| Path to the image which represents the card. (Optional, default value: './card.png')|

Example:

var myFirstCard = new Card('A', 'Aces', 'img/aces/A.png');

// myFirstCard = Card { value: 'A', type: 'Aces', imgPath: 'img/aces/A.png' }

Card.setValue(v)

Sets the value of a card.

|Parameter|Description| |-------|-------------------| |v| Value of the card. |

Example:

var myFirstCard = new Card();
// myFirstCard = Card { value: 0, type: 'default', imgPath: './card.png' }

myFirstCard.setValue(4);
// myFirstCard = Card { value: 4, type: 'default', imgPath: './card.png' }

Card.getValue()

Gets the value of the card.

Example:

var myFirstCard = new Card(6);
// myFirstCard = Card { value: 6, type: 'default', imgPath: './card.png' }

myFirstCard.getValue();
// 6

Card.setType(t)

Sets the type of a card.

|Parameter|Description| |-------|-------------------| |t| Type of the card. |

Example:

var myFirstCard = new Card();
// myFirstCard = Card { value: 0, type: 'default', imgPath: './card.png' }

myFirstCard.setType('Diamonds');
// myFirstCard = Card { value: 0, type: 'Diamonds', imgPath: './card.png' }

Card.getType()

Gets the type of the card.

Example:

var myFirstCard = new Card(6, 'Clubs');
// myFirstCard = Card { value: 6, type: 'Clubs', imgPath: './card.png' }

myFirstCard.getType();
// 'Clubs'

Card.setImgPath(i)

Sets the path to the image which represents a card.

|Parameter|Description| |-------|-------------------| |i| The path to the image which represents the card. |

Example:

var myFirstCard = new Card();
// myFirstCard = Card { value: 0, type: 'default', imgPath: './card.png' }

myFirstCard.setImgPath('./img/my-card.png');
// myFirstCard = Card { value: 0, type: 'Diamonds', imgPath: './img/my-card.png' }

Card.getType()

Gets the path to the image which represents a card.

Example:

var myFirstCard = new Card(6, 'Clubs', './img/my-card.png');
// myFirstCard = Card { value: 6, type: 'Clubs', imgPath: './img/my-card.png' }

myFirstCard.getImgPath();
// './img/my-card.png'

Deck class methods

Deck(name='default')

Constructor. Creates an instance of Deck.

|Parameter|Description| |-------|-------------------| |name| Name of the deck. (Optional, default value: 'default')|

Example:

var myFirstDeck = new Deck('The best deck');

// myFirstDeck = Deck { name: 'The best deck', cards: [], size: 0 }

Deck.setName(n)

Sets the name of a deck.

|Parameter|Description| |-------|-------------------| |n| Name of the deck. |

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.setName('The very best deck');
// myFirstDeck = Deck { name: 'The very best deck', cards: [], size: 0 }

Deck.getName()

Gets the name of the deck.

Example:

var myFirstDeck = new Card('Just a normal deck');
// myFirstDeck = Deck { name: 'Just a normal deck', cards: [], size: 0 }

myFirstDeck.getName();
// 'Just a normal deck'

Deck.getSize()

Gets the size (number of cards) of the deck.

Example:

var myFirstDeck = new Card('Just another deck');
// myFirstDeck = Deck { name: 'Just another deck', cards: [], size: 0 }

myFirstDeck.getSize();
// 0

Deck.addCardToTop(c)

Adds a new card at the top of the deck.

|Parameter|Description| |-------|-------------------| |c| The object or Card to insert on the deck. |

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToTop(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToTop(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 2 }

Deck.addCardToBottom(c)

Adds a new card at the end of the deck.

|Parameter|Description| |-------|-------------------| |c| The object or Card to insert on the deck. |

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToBottom(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToBottom(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 }

Deck.drawCardFromTop()

Draws a card from the top of the deck.

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToTop(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToTop(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 2 }

myFirstDeck.drawCardFromTop();
// Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }

Deck.drawCardFromBottom()

Draws a card from the bottom of the deck.

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToTop(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToTop(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 2 }

myFirstDeck.drawCardFromBottom();
// Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }

Deck.addCardToRandom(c)

Adds a new card at a random position of the deck.

|Parameter|Description| |-------|-------------------| |c| The object or Card to insert on the deck. |

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 }

myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 }

Deck.drawCardFromRandom()

Draws a card from a random position of the deck.

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 }

myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 }

myFirstDeck.drawCardFromRandom();
// Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }

Deck.shuffle()

Shuffles the order of the cards in a deck.

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 }

myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 }

myFirstDeck.shuffle();
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' } ], size: 3 }

Deck.cut()

xchange the order of the first half of cards of the deck with the second one.

Example:

var myFirstDeck = new Deck();
// myFirstDeck = Deck { name: 'default', cards: [], size: 0 }

myFirstDeck.addCardToRandom(new Card('K', 'Clubs', 'img/theking.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 1 }

myFirstDeck.addCardToRandom(new Card('Q', 'Hearts', 'img/thequeen.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 2 }

myFirstDeck.addCardToRandom(new Card('J', 'Diamonds', 'img/thetroll.jpg'));
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' }, Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' } ], size: 3 }

myFirstDeck.cut();
// myFirstDeck = Deck { name: 'default', cards: [ Card { value: 'J', type: 'Diamonds', imgPath: 'img/thetroll.jpg' }, Card { value: 'Q', type: 'Hearts', imgPath: 'img/thequeen.jpg' }, Card { value: 'K', type: 'Clubs', imgPath: 'img/theking.jpg' } ], size: 3 }

Eliasib García, 2017