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

ecommerce-basics

v1.1.0

Published

This JS library is designed to simplify the development of e-commerce websites. It provides essential functionalities for building shopping carts, fetching products, and managing cart operations.

Downloads

17

Readme

workflow

npm version npm downloads

ecommerce-basics NPM Library

Ecommerce Basics npm Library

Ecommerce Basics is a lightweight and easy-to-use npm library designed to simplify common e-commerce operations such as cart management, product calculations, and currency formatting. With a focus on simplicity and efficiency, this library provides essential functions to enhance the e-commerce experience in your applications.

Installation

To start using ecommerce-basics in your JavaScript projects, you can install it via npm:

npm install ecommerce-basics

Usage

Once you have installed ecommerce-basics, you can import the desired functions into your JavaScript code:

const ecommerce = require('ecommerce-basics');

Features

ecommerce-basics offers a wide range of features:

formatCurrency(amount)

  • Format a numerical value into a currency format.
const amount = 252.333334506
const formattedAmount = ecommerce.formatCurrency(amount);
console.log('Formatted Currency:', formattedAmount); // Output: $252.33

calculateTotal(cart)

  • Calculate the total price of products in the cart.
const cart = [{ id: 1, title: 'Product 1', price: 10, quantity: 2 }];
const total = ecommerce.calculateTotal(cart);
console.log('Total Price:', ecommerce.formatCurrency(total)); // Output: $20.00

addToCart(cart, product)

  • Add a product to the cart. If the product already exists, it updates the quantity.
const cart = [];
const productOne = { id: 1, title: 'Product 1', price: 10, quantity: 2 }
ecommerce.addToCart(cart, productOne);
console.log(cart); // Output: [{ id: 1, title: 'Product 1', price: 10, quantity: 2 }];
const productTwo = { id: 2, title: 'Product 2', price: 100, quantity: 1 }
ecommerce.addToCart(cart, productTwo);
console.log(cart); 
/* Output:
    [
        { id: 1, title: 'Product 1', price: 10, quantity: 2 },
        { id: 2, title: 'Product 2', price: 100, quantity: 1 }
    ];
/*

removeFromCart(cart, product)

  • Remove a single quantity of a product from the cart. If quantity becomes zero, the product is removed from the cart.
const productOne = { id: 1, title: 'Product 1', price: 10, quantity: 2 }
const productTwo = { id: 2, title: 'Product 2', price: 100, quantity: 1 }
const cart = [productOne, productTwo];
ecommerce.removeFromCart(cart, productOne);
console.log(cart[0]); // Output: [{ id: 1, title: 'Product 1', price: 10, quantity: 1 }];
console.log(cart[1]); // Output: [{ id: 2, title: 'Product 2', price: 100, quantity: 1 }];
ecommerce.removeFromCart(cart, productOne);
console.log(cart[0]); // Output: [{ id: 2, title: 'Product 2', price: 100, quantity: 1 }];
ecommerce.removeFromCart(cart, productTwo);
console.log(cart); // Output: []

removeProductFromCart(cart, product)

  • Remove a product completely from the cart.
const productOne = { id: 1, title: 'Product 1', price: 10, quantity: 4 }
const productTwo = { id: 2, title: 'Product 2', price: 100, quantity: 1 }
const cart = [productOne, productTwo];
ecommerce.removeProductFromCart(cart, productOne);
console.log(cart); // Output: [{ id: 2, title: 'Product 2', price: 100, quantity: 1 }];
console.log(cart.length); // Output: 1
ecommerce.removeProductFromCart(cart, productTwo);
console.log(cart.length); // Output: 0

calculateTotalItemCount(cart)

  • Calculate the total number of items in the cart.
const productOne = { id: 1, title: 'Product 1', price: 10, quantity: 4 }
const productTwo = { id: 2, title: 'Product 2', price: 100, quantity: 1 }
const cart = [productOne, productTwo];
const itemCount = ecommerce.calculateTotalItemCount(cart)
console.log(itemCount); // Output: 5

clearCart(cart)

  • Clear all items from the cart.
const productOne = { id: 1, title: 'Product 1', price: 10, quantity: 4 }
const productTwo = { id: 2, title: 'Product 2', price: 100, quantity: 1 }
const cart = [productOne, productTwo];
const updatedCart = ecommerce.clearCart(cart)
console.log(updatedCart); // Output: []

fetchProducts(apiUrl)

  • Fetch products from an API endpoint. Returns an array of products or null if there's an error.
const apiUrl = 'https://example-api.com/products';
ecommerce.fetchProducts(apiUrl)
    .then(products => {
        console.log('Fetched Products:', products);
    })
    .catch(error => {
        console.error('Error:', error);
    });

Contributing

I welcome contributions from the community to improve and expand the functionality of ecommerce-basics. If you have any suggestions, bug reports, or feature requests, please don't hesitate to open an issue or submit a pull request on the GitHub repository.

  • The package is intended to be used for educational purposes only.