1.0.0 ā€¢ Published 3 years ago

calculate-total-cost v1.0.0

Weekly downloads
8
License
ISC
Repository
github
Last release
3 years ago

useful to calculate the total cost of an order including taxes and respected discount based on your order total.

Dependencies

This package use only one dependency: mocha for unit testing. You can download using below command in your project.

npm install --save-dev mocha

Installation

This is a Node.js module available via npm registry. To install and use this module Node.js must be installed on your system. In your projects root directory run below command to install calculate-total-cost.

npm install calculate-total-cost

Usage

var calculate = require('calculate-total-cost');

This package contains 4 methods in total.

  • getTotalCost() which takes 3 arguments: number of items (number),price per item (number), 2 letter province code (string) Return total cost of the order after counting discount (if any) and adding taxes based on respected province. See below code for same.

    //getTotalCost(qty, price_per_item, province_code)
    calculate.getTotalCost(500,1,"ON") //Output => $565.00
    
    //input: qty: 3600, price_per_item: $2.25, province_code: MI or mi (province code are case insensitive)
    calculate.getTotalCost(3600,2.25,"MI") //Output => $7984.98
  • getTotal() which takes two arguments: number of items and price_per_item and return total cose without disc and tax.

    //getTotal(qty, price_per_item)
    calculate.getTotal(500,1) //Output => 500
  • getDiscountAmount() which takes one arguments: order total and return discount amount .
    //getDiscountAmount(qty, price_per_item)
    calculate.getDiscountAmount(500) //Output => 0
    calculate.getDiscountAmount(5000) //Output => 250
    calculate.getDiscountAmount(10000) //Output => 1000
  • getTaxAmount() which takes two arguments: order total after discount and 2 letter province code and return tax amount based on given province.
    //getTaxAmount(qty, price_per_item)
    calculate.getTaxAmount(500, "DE") //Output => 0
    calculate.getTaxAmount(500, "ON") //Output => 65
    calculate.getTaxAmount(500, "AB") //Output => 25

Click Here to see how discount and taxes are calculated.

Run tests

Run below command to run all 17 written test cases.

npm test

List of Provinces used

ProvinceTax Rate
AB5%
ON13%
QC14.975%
MI6%
DE0%

Discount Rate used for Calculation

Order ValueDiscount Rate
$1,0003%
$5,0005%
$7,0007%
$10,00010%

Author

šŸ‘¤ Devarsh Patel

šŸ“ License

Copyright Ā© 2021 Devarsh Patel. This project is ISC licensed.