Skip to content

Commit

Permalink
🔖 release: first release
Browse files Browse the repository at this point in the history
  • Loading branch information
Nembie committed Apr 13, 2024
0 parents commit 2d26c1b
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
yarn.lock
package-lock.json
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2024 Luca Pacitto <[email protected]> (https://nembie.dev)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## 🏠 calculator-home-loan

[![GitHub](https://img.shields.io/github/license/Nembie/calculator-home-loan?style=flat-square)](LICENSE.md)
[![GitHub issues](https://img.shields.io/github/issues/Nembie/calculator-home-loan?style=flat-square)](https://github.com/Nembie/calculator-home-loan/issues)
[![GitHub Release](https://img.shields.io/github/v/release/Nembie/calculator-home-loan?style=flat-square)](https://github.com/Nembie/calculator-home-loan/releases)

A JavaScript open-source package to easily calculate monthly mortgage payments, total repayment, and total interest.

### ⚙️ Install
```bash
yarn add calculator-home-loan
```

### 🚨 Example
```javascript
import calculateHomeLoan from 'calculator-home-loan';

const loan = 80000;
const years = 30;
const rate = 4.5;
const frequency = 12;

calculateHomeLoan(loan, years, rate, frequency);

// Output
{
monthlyPayment: 405.35,
totalPayment: 145925.37,
totalInterest: 65925.37
}
```


### 🤝 Contribution
If you find any issues or have suggestions for improvements, feel free to open a pull request or issue. Your contribution is highly appreciated.

### 🔨 Test

```npx ava```

### 📝 License

This package is open-sourced software licensed under the [MIT license](https://github.com/Nembie/calculator-home-loan/blob/main/LICENSE.md).
19 changes: 19 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Calculate the monthly payment, total payment, and total interest of a loan
*
* @param {number} loanValue The value of the loan
* @param {number} years The number of years for the loan
* @param {number} rate The interest rate for the loan
* @param {number} frequency The number of payments per year
* @returns {object} An object containing the monthly payment, total payment, and total interest
*
* @example
*
* ```
* import calculateHomeLoan from 'calculator-home-loan';
*
* calculateHomeLoan(80000, 30, 4.5, 12);
* // => { monthlyPayment: 405.35, totalPayment: 145925.37, totalInterest: 65925.37 }
* ```
*/
export default function calculateHomeLoan(houseValue: number, loanValue: number, years: number, rate: number, frequency: number): number;
69 changes: 69 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export default function calculateHomeLoan(loanValue, years, rate, frequency) {
// Check for negative values
if(loanValue < 0 || years < 0 || rate < 0 || frequency < 0) {
return {
monthlyPayment: NaN,
totalPayment: NaN,
totalInterest: NaN
};
}

// Check for null values
if(loanValue === 0 && years === 0 && rate === 0 && frequency === 0) {
return {
monthlyPayment: 0,
totalPayment: 0,
totalInterest: 0
};
}

// Check for undefined values
if(rate === 0) {
return {
monthlyPayment: Math.round(loanValue / years / frequency * 100) / 100,
totalPayment: loanValue,
totalInterest: 0
};
}

// Check for 0 values
if(years === 0) {
return {
monthlyPayment: loanValue,
totalPayment: loanValue,
totalInterest: 0
};
}

// Check for 0 values
if(frequency === 0) {
return {
monthlyPayment: loanValue,
totalPayment: loanValue,
totalInterest: 0
};
}

// Calculate the monthly rate
let monthlyRate = rate / frequency / 100;
// Calculate the number of payments
let numberOfPayments = years * frequency;
// Calculate the monthly payment
let monthlyPayment = loanValue * monthlyRate / (1 - Math.pow(1 + monthlyRate, -numberOfPayments));
// Calculate the total payment and total interest
let totalPayment = monthlyPayment * numberOfPayments;
// Calculate the total interest
let totalInterest = totalPayment - loanValue;

// Round to the nearest cent
monthlyPayment = Math.round(monthlyPayment * 100) / 100;
totalPayment = Math.round(totalPayment * 100) / 100;
totalInterest = Math.round(totalInterest * 100) / 100;

// Return the results
return {
monthlyPayment: monthlyPayment,
totalPayment: totalPayment,
totalInterest: totalInterest
};
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "calculator-home-loan",
"version": "1.0.0",
"description": "Calculate home loan, monthly payment, total interest.",
"license": "MIT",
"repository": "Nembie/calculator-home-loan",
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=18"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"home",
"loan",
"calculator",
"home loan",
"home loan calculator"
],
"devDependencies": {
"ava": "^5.3.1"
}
}
98 changes: 98 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import test from 'ava';
import calculateHomeLoan from './index.js';

test('Calculate home loan and expect object', t => {
let result = calculateHomeLoan(80000, 30, 4.5, 12);
t.is(typeof result, 'object');
});

test('Calculate home loan at 4.5% for 30 years', t => {
let result = calculateHomeLoan(80000, 30, 4.5, 12);
t.is(result.monthlyPayment, 405.35);
t.is(result.totalPayment, 145925.37);
t.is(result.totalInterest, 65925.37);
});

test('Calculate home loan at 3.5% for 15 years', t => {
let result = calculateHomeLoan(80000, 15, 3.5, 12);
t.is(result.monthlyPayment, 571.91);
t.is(result.totalPayment, 102943.09);
t.is(result.totalInterest, 22943.09);
});

test('Calculate home loan at 5.0% for 20 years', t => {
let result = calculateHomeLoan(80000, 20, 5.0, 12);
t.is(result.monthlyPayment, 527.96);
t.is(result.totalPayment, 126711.5);
t.is(result.totalInterest, 46711.5);
});

test('Calculate home loan with 0% interest', t => {
let result = calculateHomeLoan(80000, 30, 0.0, 12);
t.is(result.monthlyPayment, 222.22);
t.is(result.totalPayment, 80000);
t.is(result.totalInterest, 0);
});

test('Calculate home loan with 0 years', t => {
let result = calculateHomeLoan(80000, 0, 4.5, 12);
t.is(result.monthlyPayment, 80000);
t.is(result.totalPayment, 80000);
t.is(result.totalInterest, 0);
});

test('Calculate home loan with 0 loan value', t => {
let result = calculateHomeLoan(0, 30, 4.5, 12);
t.is(result.monthlyPayment, 0);
t.is(result.totalPayment, 0);
t.is(result.totalInterest, 0);
});

test('Calculate home loan with 0 frequency', t => {
let result = calculateHomeLoan(80000, 30, 4.5, 0);
t.is(result.monthlyPayment, 80000);
t.is(result.totalPayment, 80000);
t.is(result.totalInterest, 0);
});

test('Calculate home loan with 0 rate', t => {
let result = calculateHomeLoan(80000, 30, 0, 12);
t.is(result.monthlyPayment, 222.22);
t.is(result.totalPayment, 80000);
t.is(result.totalInterest, 0);
});

test('Calculate home loan with 0 0 loan value, 0 years, 0 rate, 0 frequency', t => {
let result = calculateHomeLoan(0, 0, 0, 0);
t.is(result.monthlyPayment, 0);
t.is(result.totalPayment, 0);
t.is(result.totalInterest, 0);
});

test('Calculate home loan with negative loan value, years, rate, frequency', t => {
let result = calculateHomeLoan(-80000, -30, -4.5, -12);
t.is(result.monthlyPayment, NaN);
t.is(result.totalPayment, NaN);
t.is(result.totalInterest, NaN);
});

test('Calculate home loan with null loan value, years, rate, frequency', t => {
let result = calculateHomeLoan(null, null, null, null);
t.is(result.monthlyPayment, NaN);
t.is(result.totalPayment, NaN);
t.is(result.totalInterest, NaN);
});

test('Calculate home loan with undefined loan value, years, rate, frequency', t => {
let result = calculateHomeLoan(undefined, undefined, undefined, undefined);
t.is(result.monthlyPayment, NaN);
t.is(result.totalPayment, NaN);
t.is(result.totalInterest, NaN);
});

test('Calculate home loan with missing loan value, years, rate, frequency', t => {
let result = calculateHomeLoan();
t.is(result.monthlyPayment, NaN);
t.is(result.totalPayment, NaN);
t.is(result.totalInterest, NaN);
});

0 comments on commit 2d26c1b

Please sign in to comment.