universal-tax-calculator
Version:
A comprehensive tax calculator supporting multiple countries
108 lines (107 loc) • 4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// universal-tax-calculator/index.ts
const taxRules_1 = __importDefault(require("./taxRules"));
class TaxCalculator {
constructor(country) {
const rules = taxRules_1.default[country.toUpperCase()];
if (!rules) {
throw new Error(`Tax rules not found for country: ${country}`);
}
this.countryRules = rules;
}
calculateTax(grossIncome, options = {}) {
var _a, _b, _c;
const { additionalDeductions = 0, includeSocialSecurity = true } = options;
// Calculate taxable income
const standardDeduction = (_b = (_a = this.countryRules.deductions) === null || _a === void 0 ? void 0 : _a.standard) !== null && _b !== void 0 ? _b : 0;
const taxableIncome = Math.max(0, grossIncome - standardDeduction - additionalDeductions);
// Calculate tax by brackets
let totalTax = 0;
const breakdown = [];
let lastBracketRate = 0;
for (const bracket of this.countryRules.brackets) {
if (taxableIncome > bracket.min) {
const bracketMax = (_c = bracket.max) !== null && _c !== void 0 ? _c : Infinity;
const incomeInBracket = Math.min(taxableIncome - bracket.min, bracketMax - bracket.min);
const taxInBracket = incomeInBracket * bracket.rate;
totalTax += taxInBracket;
lastBracketRate = bracket.rate;
breakdown.push({
bracket,
taxAmount: taxInBracket,
});
}
}
// Calculate social security if applicable
let socialSecurityTax = 0;
if (includeSocialSecurity && this.countryRules.socialSecurity) {
const { employeeRate, maxIncome } = this.countryRules.socialSecurity;
const socialSecurityIncome = maxIncome
? Math.min(grossIncome, maxIncome)
: grossIncome;
socialSecurityTax = socialSecurityIncome * employeeRate;
}
// Calculate effective tax rate
const effectiveRate = totalTax / (taxableIncome || 1);
const netIncome = grossIncome - totalTax - socialSecurityTax;
return {
grossIncome,
taxableIncome,
totalTax,
effectiveRate,
marginalRate: lastBracketRate,
socialSecurityTax,
netIncome,
breakdown,
};
}
// Get available deductions for the country
getDeductions() {
return this.countryRules.deductions || {};
}
// Get tax brackets for the country
getTaxBrackets() {
return this.countryRules.brackets;
}
// Get currency for the country
getCurrency() {
return this.countryRules.currency;
}
// Format currency amount according to country
formatCurrency(amount) {
const formatted = new Intl.NumberFormat(this.getLocale(), {
style: 'currency',
currency: this.countryRules.currency,
}).format(amount);
return formatted.trim(); // Add this line to remove any trailing spaces
}
// Get locale for the country
getLocale() {
const localeMap = {
USD: 'en-US',
GBP: 'en-GB',
EUR: 'de-DE',
CAD: 'en-CA',
MXN: 'es-MX',
JPY: 'ja-JP',
CNY: 'zh-CN',
INR: 'en-IN',
SGD: 'en-SG',
AUD: 'en-AU',
NZD: 'en-NZ',
BRL: 'pt-BR',
ARS: 'es-AR',
ZAR: 'en-ZA',
NGN: 'en-NG',
AED: 'ar-AE',
SAR: 'ar-SA',
ILS: 'he-IL',
};
return localeMap[this.countryRules.currency] || 'en-US';
}
}
exports.default = TaxCalculator;