Skip to content

Commit

Permalink
style: linter
Browse files Browse the repository at this point in the history
  • Loading branch information
alepaz committed Sep 28, 2020
1 parent 3433c26 commit 427ea87
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 16 deletions.
122 changes: 121 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,132 @@
# Loan utils

Project with different functions to calculate:

-The minimun payment of a mortgage

**Example parameters**:

Loan = 150000.00

Interest Rate: 7.5%, //format like 0.075

Loan Length: 30 years, //format in months

```
import { getMonthlyPayment } from 'mortgate';
const monthlyPayment = getMonthlyPayment(150e3, 7.5e-2, 30*12);
```

-The loan lenght of a mortgage

-Calculate the new loan lenght adding an extra payment
**Example parameters**:

Loan = 150000.00

Interest Rate: 7.5%, //format like 0.075

Mountly Payment: 2000.00

```
import { getLoanLength } from 'mortgate';
const newLoanLenght = getLoanLength(150e3, 7.5e-2, 2000);
```

-The amortization table of the payoffs

**Example parameters**:

Loan = 200000.00

Interest Rate: 7.5% //format like 0.075

Loan Length: 30 years //format in months

Extra Payment = 1000

Initial Date: number = Date.now() //format milliseconds

Times Interest Compounds = 12 //default 12

```
import { getMortgagePayoff } from 'mortgate';
const payoffData = getMortgagePayoff(2e5, 7.5e-2, 30 * 12, 1000);
//payoffData
{
mountlyPayment: 1398.429017105553,
extraPrincipal: 0,
interestRate: 0.075,
defaultTotalInterest: 303434.44615799905,
totalInterestWithSavings: 303434.4461580063,
loan: 200000,
defaultLoanLength: 360,
loanLength: 360,
startDate: 2020-09-28T08:44:15.873Z,
endDate: 2050-08-28T08:44:15.873Z,
defaultEndDate: 2050-08-28T08:44:15.873Z,
data: [{
date: 2050-09-28T08:44:15.873Z,
interest: 1250,
principal: 148.42901710555293,
principalExtra: 148.42901710555293,
balance: 199851.57098289445,
},
{
date: 2050-09-28T08:44:15.873Z,
interest: 1249.0723186430903,
principal: 149.35669846246265,
principalExtra: 149.35669846246265,
balance: 199702.214284432,
},
...
]
}
```

-Calculate simple interest and amortizing interest

**Total interest for a loan paying only the minimum monthly payment**
Use this function when you are going to calculate the lifetime interest using the minimun mountly payment.

Loan = 200000.00

Interest Rate: 7.5% //format like 0.075

Loan Length: 30 years //format in months
```
import { getTotalInterest } from 'mortgate';
const lifetimeInterest = getTotalInterest(2e5, 7.5e-2, 30 * 12);
```

**Total interest for a loan paying a custom monthly payment**
Note: It could vary in a few decimal places far from the function getTotalInterest, by the method of recalculation of interest.

**Example of variation**

Using getTotalInterest: 303434.44615799905

Using getTotalAmortizingInterest: 303434.4461580063

Use getTotalAmortizingInterest if there is an Extra Payment Amount for the monthly payment

Example

Loan = 200000.00

Interest Rate: 7.5% //format like 0.075

Mountly Payment: 2000
```
import { getTotalAmortizingInterest } from 'mortgate';
const lifetimeInterest = getTotalAmortizingInterest(2e5, 7.5e-2, 2000);
```

25 changes: 10 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const getLoanLength = (
* @param {number} Interest rate annually
* @param {number} Loan length express in months
* @param {number} Extra payment add to the principal
* @param {number} Date to start the payoff in miliseconds
* @param {number} Date to start the payoff in milliseconds
* @param {number} Number of times interest compounds, default 12 (t)
* @returns {object} Payoff Results
*/
Expand All @@ -196,9 +196,7 @@ export const getMortgagePayoff = (
if (extraPrincipal < 0) throw new Error('Please provide a valid extra payment');
if (timesInterestCompounds <= 0) throw new Error('Please provide a valid number of times interest compounds');

//Payoff results (Payments)
const payoffGrid: any = [];

let remainingLoan = loan;
const defaultLoanLength = loanLength;
const defaultInitialDate = initialDate;
Expand All @@ -215,7 +213,7 @@ export const getMortgagePayoff = (
interestRate,
defaultTotalInterest,
totalInterestWithSavings: getTotalAmortizingInterest(loan, interestRate, mountlyPayment, timesInterestCompounds),
loan: loan,
loan,
defaultLoanLength: loanLength,
loanLength: newLoanLength,
startDate: new Date(initialDate),
Expand All @@ -231,38 +229,34 @@ export const getMortgagePayoff = (
const dateRow = date;
const interestRow = getSimpleInterest(remainingLoan, interestRate);
const principal = mountlyPayment - interestRow;
let principalExtra = principal + extraPrincipal;
const principalExtra = principal + extraPrincipal;
let balance = remainingLoan - principalExtra;

/*
* Stop Condition
*/
if (remainingLoan + interestRow < mountlyPayment + extraPrincipal) {
const row = {
payoffGrid.push({
date: dateRow,
interest: interestRow,
principal,
principalExtra: principalExtra + balance,
balance: 0,
type: 'row',
};
payoffGrid.push(row);
remainingLoan = balance = 0; //remaining loan
});
remainingLoan = balance = 0;
break;
}

/*
* Add row to the payoff Grid
*/
const row = {
payoffGrid.push({
date: dateRow,
interest: interestRow,
principal,
principalExtra,
balance,
type: 'row',
};
payoffGrid.push(row);
});

/*
* Update conditions for next iteration
Expand All @@ -272,13 +266,14 @@ export const getMortgagePayoff = (
}

results.data = payoffGrid;
console.log(results);
return results;
};

/*
* Calculate end date of the loan
*
* @param {number} date in miliseconds
* @param {number} date in milliseconds
* @param {number} loan Length
* @returns {string} end date of the loan
*/
Expand Down

0 comments on commit 427ea87

Please sign in to comment.