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

@svz1234/shopping-cart

v1.3.1

Published

A simple shopping cart module using local- or session storage for persistence.

Downloads

834

Readme

Shopping Cart Module

This module is responsible for creating and managing a shopping cart object. As of now it supports storing the cart in the local- or session storage of the browser. It provides the following functionalities:

  • Add a product to the cart
  • Remove a product from the cart
  • Increment/decrement the quantity of a product in the cart
  • Clear the cart
  • Load the cart from storage of choice
  • Get the cart object, containing all products in the cart with their respective quantities
  • Get the total quantity of products in the cart
  • Get the quantity of a specific product in the cart
  • Data from local- or session storage gets automatically sanitized to make it more secure

Important

If other storage options are wanted, they can be added by implementing the StorageHandler interface. The LocalStorageHandler and SessionStorageHandler classes are already implemented and can be used as examples.

No visual representation of the cart is provided by this module. It is up to the user to implement the visual representation of the cart in their application.

A product template is not provided by this module. The user is responsible for creating a product template that fits their needs. The only thing the module requires is a product id to store in the cart. The product id can then be used to fetch the product from a database or similar.

Installation

  npm install @svz1234/shopping-cart

Usage

  1. Install the module as described above
  2. Import the ShoppingCart class into your project
  3. Create a new instance of the ShoppingCart class
  4. Pass the string "localStorage" or "sessionStorage" to the constructor of the ShoppingCart to use the respective storage handler. Local storage is used by default if no argument is provided
  5. Pass the string "uuid" or "alphanumeric" to the constructor of the ShoppingCart to use the respective string sanitization method. "alphanumeric" is used by default if no argument is provided
  6. Use the methods provided by the ShoppingCart class to manage the cart
import { ShoppingCart } from "@svz1234/shopping-cart";

const cart = new ShoppingCart("localStorage", "uuid");

/**
 * The following methods take a product id as argument.
 * The product id can be either a number or a string.
 * If the product id is a string, it will be sanitized according to the string sanitization method provided in the constructor.
 */
cart.addProduct(123); // Returns void
cart.removeProduct(123); // Returns void
cart.incrementProductQuantity(123); // Returns void
cart.decrementProductQuantity(123); // Returns void

cart.getProductQuantity(123); // Returns number
console.log(cart.getProductQuantity(123)); // Example output: 4

/**
 * The following methods do not take any arguments.
 */
cart.load(); // Returns void
cart.getCart(); // Returns CartItem[]
console.log(cart.getCart());
/*
  Example output: [
    CartItem { productId: 1, quantity: 1 },
    CartItem { productId: 2, quantity: 3 },
    CartItem { productId: 3, quantity: 5 }
  ]
 */

cart.getTotalQuantity(); // Returns number
console.log(cart.getTotalQuantity()); // Example output: 3

cart.clearCart(); // Returns void

Requirements

The module must be used client side in a browser environment. This is because the module needs access to the local- and session storage of the browser.

License

MIT ©

This project is licensed under the MIT License - see the LICENSE file for details.

License: MIT

GitHub

GitHub Repository

Testing is done using ts-jest (jest with typescript support). The tests can be found in the GitHub repository under the test directory. The tests are run using the npm run test command.