1.0.4 • Published 8 years ago
basket-helper v1.0.4
basket-helper
A simple utility class for managing baskets in node js
install
npm install basket-helper
usage
first require the module
var BH = require("basket-helper")
add a config file and pass it to the basket helper
var config = {
"validateItems" : true, //check if products exist in the list of products before adding them
"currency": "£", // currency for display purposes
"products" : [
{
"name" : "apple", // product name
"price" : 0.20 // product price
},
{
"name" : "orange",
"price" : 0.30
},
{
"name" : "garlic",
"price" : 0.15
},
{
"name" : "papaya",
"price" : 0.5
}
],
"promotions":[ // an array of promotions
{
"type": "buyXofferY", //the type of the promotion
"product": "papaya", // the products on which the promo applicable
"buy": 6, // how much products to buy to trigger the promo
"offer": 2 // how much products are offers
}
]
}
var bHelp = new BH(config) // pass config object
addAll with a list of product names
bHelp.addAll([
"papaya",
"papaya",
"papaya",
"papaya",
"papaya",
"papaya"
])
bHelp.addAll([
{"name" : "garlic", "qt" : 3, "price":5 }, // you can also optionaly pass quantity and price.
//it you do not have validation function on you have to pass a price at least the 1st time you add a product name differently
{"name" : "apple"} // or simply a name
])
pass products individualy with add method
bHelp.add("papaya")
bHelp.add("Orange", 2)
total
use total function to calculate the cart total it returns an object with a msg properties displaying a kinda bill and the sum of all products prices in the total property
process.stdout.write(bHelp.total().msg)
you can open exemple.js file to see basic exemple of usage
Contribute
download the source from git hub then in the directory of the project run :
npm run dev
make your changes in src directory, and nodemon will trigger babel compiling notice that app.js is a dummy app for testing purposes in production it's basketHelper.js that is exported