From ff796601093e88b5934a2b18441469d8ac4a5d22 Mon Sep 17 00:00:00 2001 From: cerberus Date: Sun, 18 Aug 2024 15:07:33 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=BD=EF=B8=8F=20run=20prettier;=20?= =?UTF-8?q?=E2=9E=95=20add=20tests;=20=F0=9F=93=96=20update=20readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- calc/compoundInterest.test.ts | 26 ++++++++++++++++++++++++++ calc/compoundInterest.ts | 8 ++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a2c7b4e..a2aa68b 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ const repayment = compoundInterestPerPeriod({ - `amountPerAnnum: number` The amount of contributions per annum (eg 6_000 for 500 per month) - `accrualOfPaymentsPerAnnum: boolean` If provided payments accrue interest per annum; Otherwise interest is only accrued on the principal payment. -- `contributionPerAnnumChange` Changes of annual contribution in percents (to adjust contribution according inflation rates, good for long investments) +- `contributionPerAnnumChange: number` Changes of annual contribution in percents (to adjust contribution according inflation rates, good for long investments) ###### Debt Repayment Options diff --git a/calc/compoundInterest.test.ts b/calc/compoundInterest.test.ts index 00f9f88..07b98c8 100644 --- a/calc/compoundInterest.test.ts +++ b/calc/compoundInterest.test.ts @@ -520,4 +520,30 @@ describe("compoundInterestPerPeriod", () => { ); }); }); + + describe("contributionPerAnnumChange", () => { + it("when contributionPerAnnumChange is supplied it increases annual additional contribution amountPerAnnum", () => { + const options: IOptions = { + type: "contribution", + principal: 250_000, + rate: 7.8, + years: 25, + paymentsPerAnnum: 1, + amountPerAnnum: 12_000, + contributionPerAnnumChange: 3, + currentPositionInYears: 1, + accrualOfPaymentsPerAnnum: true + }; + const result = compoundInterestPerPeriod(options); + expect(result).toMatchObject( + expect.objectContaining({ + currentBalance: 282436, + totalInterest: 2144895.2679852117, + endBalance: 2832406.46, + accrualOfPaymentsPerAnnum: true, + investmentType: "contribution" + }) + ); + }); + }); }); diff --git a/calc/compoundInterest.ts b/calc/compoundInterest.ts index ef54a96..99867ec 100644 --- a/calc/compoundInterest.ts +++ b/calc/compoundInterest.ts @@ -43,12 +43,12 @@ export const calcTotalInvestment = (options: IOptions, investmentType: Investmen if (investmentType === "contribution" && "amountPerAnnum" in options) { const { amountPerAnnum = 0, contributionPerAnnumChange = 0 } = options; // Adjust annual contributions if contributionPerAnnumChange rate provided - if ( contributionPerAnnumChange > 0 ) { + if (contributionPerAnnumChange > 0) { let repaymentWithAnnualChange = amountPerAnnum; for (let i = 1; i < years; i++) { - repaymentWithAnnualChange += repaymentWithAnnualChange * contributionPerAnnumChange / 100 + amountPerAnnum; + repaymentWithAnnualChange += (repaymentWithAnnualChange * contributionPerAnnumChange) / 100 + amountPerAnnum; } - return principal * years + repaymentWithAnnualChange + return principal * years + repaymentWithAnnualChange; } return principal + amountPerAnnum * years; } @@ -197,7 +197,7 @@ export const compoundInterestPerPeriod = (options: IOptions): CompoundInterestRe if (accrualOfPaymentsPerAnnum) { // Adjust contributions only from the 2nd year if (i >= 1 && contributionPerAnnumChange) { - amountPerAnnum = amountPerAnnum * contributionPerAnnumChange / 100 + amountPerAnnum; + amountPerAnnum = amountPerAnnum * (1 + contributionPerAnnumChange / 100); } const newBalanceWithAccrual = prevBalance + amountPerAnnum / paymentsPerAnnum; const interest = newBalanceWithAccrual * ratePerPeriod;