cart-localstorage
Advanced tools
Weekly downloads
Readme
This micro library utilizes the browser's localStorage to create a persistent shopping cart instance
Use npm or yarn to add it to your ES6 project:
npm install cart-localstorage
yarn add cart-localstorage
OR use the bundled version (via CDN):
<script src="https://unpkg.com/cart-localstorage@1.1.2/dist/cart-localstorage.min.js" type="text/javascript"></script>
import { add, total } from 'cart-localstorage'
add({id: 1, name: "Product 1", price: 100})
add({id: 2, name: "Product 2", price: 100}, 2)
console.log(total())
// output: 300
Adds product to the cart. If the product already exists it increases the quantity with 1. The product object structure is flexible, only "id" and "price" are mandatory properties.
const myproduct = {id: 3, name: "Vans", price: 75}
add(myproduct, 2)
Get product from the cart by id
get(1)
// {id: 1, name: "Nike Air", price: 100, quantity: 1}
Checks if the product already exists in the cart
exists(21)
// true or false
Get the content of the cart as an array of products.
list()
// [{id: 1, name: "Nike Air", price: 100, quantity: 1}, {id: 2, name: "Adidas Superstar", price: 120, quantity: 2}]
Removes the product from the cart
remove(1)
Updates the product's property with a certain value.
update(1,'price',200)
Increase / decrease product's quantity with a positive or negative value.
quantity(22,-1) // will decrease the quantity of product [id:22] with 1.
By default returns with the total price:
total()
// 220
or you can pass a custom reducer function to have full control over the calculation.
Deletes the cart array from the browser's localStorage.
destroy()
This plugin is available under Apache 2.0 license.
FAQs
Tiny shopping cart on top of LolcalStorage - ES6, < 1Kb, full test coverage
The npm package cart-localstorage receives a total of 452 weekly downloads. As such, cart-localstorage popularity was classified as not popular.
We found that cart-localstorage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.