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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ravishranjan/cart

v2.1.5

Published

A native cart system for web apps with optional React hooks

Readme

@ravishranjan/cart

A lightweight, native frontend cart system with optional React hooks.

  • 🧠 Works in any JavaScript framework — Vanilla JS, React, Vue, Svelte, etc.
  • ⚛️ Includes React hooks and <CartProvider> for easy React integration.
  • 💾 Uses localStorage or sessionStorage for persistence.
  • 🔌 Works both via npm install and CDN — no backend required.

📦 Installation

Option 1 — NPM

npm install @ravishranjan/cart

Option 2 — CDN (no install)

Use directly in your webpage:

<script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>

Cart item type

interface CartItem {
	id: string | number;
	name: string;
	price?: number;
	quantity?: number;
	category?: string;
	[key: string]: any;
}

🧩 Usage Examples

1️⃣ Basic JavaScript (no framework)

<script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>
<script>
	const cart = new Cart({ storage: "localStorage" });

	cart.addItem({ id: 1, name: "Apple", price: 30, quantity: 2 });
	cart.addItem({ id: 2, name: "Orange", price: 25 });

	cart.removeItem(2);

	console.log("Total price:", cart.getTotal());
	console.log(cart.getItems());

	cart.clear();
</script>

2️⃣ Node or Framework Projects (NPM install)

import Cart from "@ravishranjan/cart";

const cart = new Cart({ storage: "localStorage" });

cart.addItem({ id: 101, name: "Book", price: 199 });
cart.addItem({ id: 102, name: "Pen", price: 49, quantity: 2 });

console.log(cart.getItems());
console.log("Total Price:", cart.getTotal());

Available methods: | Method | Description | Example | |---------|--------------|----------| | addItem(item) | Adds an item object | cart.addItem({ id: 1, name: 'Apple', quantity: 2 }) | | removeItem(id) | Removes an item by id | cart.removeItem(1) | | getItems() | Returns all items | cart.getItems() | | getItems(category) | Returns items by given catefory | cart.getItems("<category>") | | getTotal() | Sum of all item prices × qty | cart.getTotal() | | clear() | Clears the entire cart | cart.clear() |


3️⃣ React Usage (with hooks + context)

Setup the Provider

import React from "react";
import { CartProvider } from "@ravishranjan/cart/react";

import App from "./App";

export default function Root() {
	return (
		<CartProvider storage="localStorage">
			<App />
		</CartProvider>
	);
}

Use the Hook

import { useCart } from "@ravishranjan/cart/react";

export default function Shop() {
	const { items, addItem, removeItem, getTotal, clear } = useCart();

	return (
		<div>
			<button
				onClick={() => addItem({ id: 1, name: "Apple", price: 30 })}
			>
				Add Apple
			</button>
			<button onClick={() => clear()}>Clear Cart</button>

			<ul>
				{items.map((item) => (
					<li key={item.id}>
						{item.name} - ₹{item.price}
						<button onClick={() => removeItem(item.id)}>❌</button>
					</li>
				))}
			</ul>

			<h3>Total: ₹{getTotal()}</h3>
		</div>
	);
}

⚙️ Storage Options

| Option | Description | | ----------- | -------------------------------------------------------- | | "localStorage" | Persists cart across browser sessions (via localStorage) | | "sessionStorage" | Clears cart when the tab is closed (via sessionStorage) |

Usage:

const cart = new Cart({ storage: "sessionStorage" });

or in React:

<CartProvider storage="sessionStorage">
	<App />
</CartProvider>

🌍 CDN Quick Demo

<!DOCTYPE html>
<html>
	<head>
		<title>Cart Demo</title>
	</head>
	<body>
		<script src="https://cdn.jsdelivr.net/npm/@ravishranjan/cart/dist/cart.min.js"></script>
		<script>
			const cart = new Cart();
			cart.addItem({ id: 1, name: "Coffee", price: 120 });
			console.log(cart.getTotal()); // 120
		</script>
	</body>
</html>

🧱 API Summary

| API | Type | Description | | -------------- | --------------- | ------------------------------- | | Cart | Class | Core cart logic | | CartProvider | React Component | Context provider for cart state | | useCart() | React Hook | Access cart in React components |


🧾 License

ISC © Ravish Ranjan