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

@nichoth/shopping-cart

v1.0.1

Published

shopping cart thing

Downloads

45

Readme

shopping cart

A shopping cart UI widget

Uses preact

install

npm i -S @nichoth/shopping-cart

A browserified bundle is in /dist. Or you would import src/cart if you are bundling these prior to use.

example

With import

import Cart from '@nichoth/shopping-cart'
import EVENTS from '@nichoth/shopping-cart/src/EVENTS'

// args are optional
var cart = new Cart({
    storage: true // store the state in localStorage?
    key: 'cart'  // default is 'cart'
})

// `cart` is an event emitter
cart.on(EVENTS.quantity.change, ev => {
    console.log('quantity', ev.quantity)
    console.log('index', ev.index)
})

cart.on(EVENTS.cart.remove, index => {
    console.log('index of the removed item', index)
})

cart.on(EVENTS.product.change, (index, updatedProduct) => {
    console.log('product change', updatedProduct)
})

// pass in an element to mount it at
// or it will automatically mount to the given ID
// for any product, if the `quantity` is greater than the
// `quantityAvaileble`, the icon will render as an exclamation point
cart.createIcon(document.getElementById('shopping-cart-icon'), {
    link: '/my-cart'
})
cart.createPage(document.getElementById('shopping-cart-page'), mapper)

function mapper (html, product) {  // `html` here is from 'htm' on npm
    console.log('in map', product)
    return html`
        <span>name: ${product.name || 'none'}, </span>
        <span>price: ${product.price || 'none'}<//>
    `
}

var products = cart.products()

cart.remove(1) // remove the product at index 1
cart.empty()  // remove everything in the cart

// add something
// icon and page in the UI will auto update
// index is for the item that was added
// default quantity of 1 will be added if no quantity key is present
var index = cart.add({ name: 'my product', quantity: 1, price: 10 }) {

// index of item & desired quantity
// returns itself for chaining
// will emit EvENTS.quantity.change
cart = cart.changeQuantity(0, 2) {

// will emit EVENTS.product.change (index, updatedProduct)
cart = cart.update(0, { quantityAvailable: 4 })

// -----------------------------------------------------------


// get the cart state
var state = cart.state()
var { products } = state

Without importing

<script src="/path/to/cart/dist/bundle.js"></script>
<script>
    var cart = new window.Cart({})
    cart.createIcon(document.getElementById('shopping-cart-icon'), {
        link: '/my-shopping-cart'
    })
    cart.createPage(document.getElementById('shopping-cart-page'))
</script>

develop

Start a local dev server

npm start

Creating A Shopping Cart With HTML5 Web Storage

Ecommerce cart design best practices