Skip to content

Commit

Permalink
πŸ‘½οΈ run prettier; βž• add tests; πŸ“– update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
cerberus committed Aug 18, 2024
1 parent 9cf999e commit ff79660
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions calc/compoundInterest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
})
);
});
});
});
8 changes: 4 additions & 4 deletions calc/compoundInterest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ff79660

Please sign in to comment.