a elegant way to calculation product price according to some discount rules
npm install cow-price
import Cow from 'cow-price'
const originalPrice = 200
const discountRule = {
category: 'percentage',
condition: {
perCount: 100,
perDiscount: 90,
maxDiscount: 10,
},
}
const c = new Cow(originalPrice, discountRule)
const actualPrice = c.cost()
console.log(actualPrice) // 190
for now, two kinds of discount rule are supported
- 'flat': every
perCount
of the original price has aperDiscount
, the max discount amount ismaxDiscount
{
category: 'flat',
condition: {
perCount: 100,
perDiscount: 10,
maxDiscount: 15,
},
}
- 'percentage': original price has a percentage
perDiscount
discount when price is over theperCount
, the max discount amount ismaxDiscount
{
category: 'percentage',
condition: {
perCount: 100,
perDiscount: 90,
maxDiscount: 100,
},
}
git clone git@github.com:kimochg/cow-price.git
cd cow-price
npm install
npm run watch
npm run test
MIT © Cheng Liu