Skip to content

Repository files navigation

Shopping Cart

A simple shopping cart for Node or Browser, written in TypeScript

Quality gateCoverageSecurity Rating

Install

npmi-S @atwright147/cart

Usage

// node < v15 does not support `crypto.getRandomValues()`, so you must install this polyfill (modern browsers should be fine though)global.crypto={getRandomValues: require('polyfill-crypto.getrandomvalues')};import{Cart}from'@atwright147/cart';constcart=newCart();// add an item to the cart// `id` could be anything but I suggest using the `id` from your databasecart.add({id: 1,name: 'Item 1',quantity: 1,price: 10});cart.add({id: 2,name: 'Item 2',quantity: 1,price: 5});// check item is in the cart (returns a `boolean`)cart.has({id: 1})cart.has({uuid: '<uuid>'})// check how many unique items are in the cartcart.length// 2// adding an item to the cart which is already added increments the quantity of item in the cart with ID 2 by `quantity`cart.add({id: 2,name: 'Item 2',quantity: 1,price: 5});// check the total value of the cartcart.total// 20// list all items in the cart// also tells you the total value of the whole cart and the `subTotal` for each rowcart.all/* { items: [ { id: 1, uuid: "<uuid1>", name: "Item 1", quantity: 1, price: 10, subTotal: 10 }, { id: 2, uuid: "<uuid2>", name: "Item 2", quantity: 2, price: 5, subTotal: 10 } ], total: 20 } */// get items by `id` or `uuid`cart.get({id: 1})// { id: 1, uuid: "<uuid1>", name: "Item 1", quantity: 2, price: 5, subTotal: 10 }cart.get({uuid: '<uuid2>'})// { id: 2, uuid: "<uuid2>", name: "Item 2", quantity: 2, price: 5, subTotal: 10 }// remove an item by `id` or `uuid`cart.remove({id: 1});// remove row where id equals 1cart.remove({uuid: '<uuid2>'});// remove row where uuid equals <uuid2>

Please refer to the included demos for working examples of CommonJS and UMD usage.

About

A simple shopping cart for Node or Browser, written in TypeScript

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages