calculatetax v1.0.4
calculateTax Function Documentation
Introduction
The calculateTax function is a utility function designed to calculate tax and total amounts based on input parameters. This function returns a promise that resolves to an object containing the total amount and tax amount.
Function Signature
const calculateTax = (prop1, prop2, profile) => {
// Function body
}Parameters
prop1: An object that may contain asubTotalproperty to be used as the base amount.prop2: An object that may contain ataxproperty to determine the tax rate.profile: An object representing the user's profile information.
Function Logic
The
baseAmountis determined based on the providedprop1object:- If
prop1.subTotalexists and is notundefined, it is checked whether it is a string. If it is a string, it is converted to a numeric value by removing commas. If it is not a string, it is used as-is. Ifprop1.subTotalisundefined, the base amount is set to 0.
- If
The tax rate (
tax) is determined based on the providedprop2object and the user'sprofile:- If
prop2.taxis 0, the function uses the tax rate from the user's profile (profile.taxType.tax). Ifprop2.taxis not 0, it is used as the tax rate.
- If
The tax amount is calculated by multiplying the
baseAmountby the tax rate and dividing it by 100.The total amount is calculated by adding the
baseAmountand the tax amount.The function returns a promise that resolves to an object containing:
totalAmount: The total amount after applying tax, represented as a float.taxAmount: The tax amount, represented as a float.
Usage Example
import calculateTax from './calculateTax';
// Sample input parameters
const prop1 = { subTotal: '1000,00' }; // Base amount with commas
const prop2 = { tax: 5 }; // Tax rate
const profile = { taxType: { tax: 8 } }; // User's profile
// Calculate tax and total amounts using the calculateTax function
calculateTax(prop1, prop2, profile)
.then(result => {
console.log(`Total Amount: $${result.totalAmount.toFixed(2)}`);
console.log(`Tax Amount: $${result.taxAmount.toFixed(2)}`);
});GitHub Repository
- The source code for this function is available in the calculateTax GitHub repository.
Notes
This function allows for flexible tax and total amount calculations based on input parameters, providing versatility for different scenarios.
It can be used in applications requiring tax calculations for financial transactions.
This Markdown documentation provides a detailed explanation of the calculateTax function, its parameters, and how it performs tax and total amount calculations. It also includes a usage example and a link to the GitHub repository for reference.