- Items are presented sequentially in an array or as a string
- One offer per item (this could be extended to support an array of offers)
- Products and offers are known in advance (these could be stored elsewhere)
- Clone the repo
yarnornpm installyarn testornpm test
Requires node 6.0.0+ for ES6 compatibility
Accepts string or array of strings and returns true if successful
basket.add(['Apple', 'Orange']);
basket.add('Apple');Removes all items from the basket
basket.empty();Returns the contents of the basket in JSON which is suitable for rendering by a view layer like React
basket.getTotal();{
"items": [
{
"name": "Papaya",
"quantity": 3,
"price": "£1.00",
"discount": "£0.50"
},
{
"name": "Garlic",
"quantity": 2,
"price": "£0.30"
},
{
"name": "Apple",
"quantity": 1,
"price": "£0.25"
},
{
"name": "Orange",
"quantity": 2,
"price": "£0.60"
}
],
"totalPrice": "£2.15",
"totalItems": 8
}Offers are just functions that receive the quantity, price, and total. The
value returned is used as the discounted price.
Add new offers to offers.js:
module.exports = {
someNewOffer(quantity, price, total) {
// return a value
},
};The function name is used by the product in products.js
{
'pears': {price: 25, offer: 'someNewOffer'},
}Offers can be added to every product
Adding remove