UNPKG

@votemike/property

Version:

Property class and related classes

61 lines (60 loc) 2.3 kB
import { round } from './helpers'; import Fee from './fee'; export default class Finance { constructor(amount, repayment, length, rate, fees, teaserRate) { this.amount = amount; this.repayment = repayment; this.length = length; this.rate = rate; this.fees = fees; this.teaserRate = teaserRate; } get totalCostOfFinance() { return round(this.calculateTotalCostOfFinance()); } get totalOneOffCosts() { return this.fees.reduce((total, fee) => fee.amount + total, 0); } get monthlyCostOfFinance() { return round(this.calculateMonthlyCostOfFinance(this.rate)); } get monthlyTeaserCostOfFinance() { var _a; return round(this.calculateMonthlyCostOfFinance((_a = this.teaserRate) !== null && _a !== void 0 ? _a : this.rate)); } get yearlyCostOfFinance() { return round(this.calculateYearlyCostOfFinance(this.rate)); } get yearlyTeaserOfFinance() { var _a; return round(this.calculateYearlyCostOfFinance((_a = this.teaserRate) !== null && _a !== void 0 ? _a : this.rate)); } calculateTotalCostOfFinance() { const feeCosts = this.fees.reduce((total, fee) => fee.amount + total, 0); if (this.repayment) { return this.calculateYearlyCostOfFinance(this.rate) * this.length + feeCosts; } return this.amount + this.calculateYearlyCostOfFinance(this.rate) * this.length + feeCosts; } calculateYearlyCostOfFinance(rate) { return this.calculateMonthlyCostOfFinance(rate) * 12; } calculateMonthlyCostOfFinance(rate) { const monthlyInterestRate = rate / 12 / 100; if (!this.repayment) { return this.amount * monthlyInterestRate; } const monthsLeft = this.length * 12; // M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1] const pow = Math.pow((monthlyInterestRate + 1), monthsLeft); return this.amount * ((monthlyInterestRate * pow) / (pow - 1)); } static fromJson(json) { const finance = Object.create(Finance.prototype); return Object.assign(finance, json, { fees: json.fees.map((feeObject) => { return Fee.fromJson(feeObject); }) }); } }