1.0.3 • Published 6 years ago

amortizejs v1.0.3

Weekly downloads
13
License
MIT
Repository
-
Last release
6 years ago

AmortizeJS

Website Version npm license

Loan calculation and amortization schedule utility with support for custom amortization methods, available for node and browser.

AmortizeJS.calculate({
    method:   'mortgage', 
    apr:      3.5, 
    balance:  280350, 
    loanTerm: 60,
    startDate: new Date('December 24 2017')
});

Table of Contents

  1. Features
  2. Installation
  3. Quickstart
  4. API Docs
  5. Formatting of results in examples
  6. Contributing

Features

  • Periodic payment calculation
  • Total interest calculation
  • Total payment calculation
  • Calculate a payment's interest amount, principal amount and remaining balance
  • Amortization schedule creation with optional payment date support
  • Easily customize and create new amortization methods

Supported amortization methods

  • Mortgage-Style/Constant-Payment method

Installation

Using Bower:

$ bower install amortizejs

<script src="./bower_components/AmortizeJS/dist/web/bundle.js"></script>

Using npm:

$ npm install amortizejs

Usage

In Browser: AmortizeJS class is available on the window object.

In Node.js

var AmortizeJS = require('amortizejs').Calculator;

Quickstart

To calculate a mortgage amortization schedule including payment dates:

var mortgage = AmortizeJS.calculate({
    method:   'mortgage',
    apr:      3.5, 
    balance:  280350,    
    loanTerm: 60,         
    startDate: new Date('December 24 2017')
});

console.log( mortgage.periodicPayment );    // 5100.06
console.log( mortgage.periodicInterest );   // 0.00292
console.log( mortgage.totalInterest );      // 25653.34
console.log( mortgage.totalPayment );       // 306003.34
console.log( mortgage.endDate );            // Sat Dec 24 2022 00:00:00 GMT-0500 (EST)
console.log( mortgage.schedule );           // [{"interest": 817.69, "principal": 4282.37, "remainingBalance": 276067.63, "date":"2017-12-24T05:00:00.000Z"} ...]

To retrieve list of available amortization methods:

AmortizeJS.availableMethods(); // ['mortgage']

API Docs

AmortizeJS

CommandParamsReturnDescription
calculate(config)calculatorConfigAmortizationMethodCalculates amortization details and schedule.
availableMethods()nonestring[]Returns the amortization methods that are available.

CalculatorConfig

An object conforming to the CalculatorConfig Interface is required when calling AmortizeJS.calculate(config), the following options are available:

AttributeTypeDescription
methodstring | functionA string identifying the amortization method to use, or a custom amortization function.
balancenumberThe loan amount.
loanTermnumberLoan term in month.
aprnumberThe Anual Percentage Rate (ex: 3.5%)
startDateDate (Optional)Optional start date, will cause monthly payments to have dates attached to them.

AmortizationMethod

An object conforming to the AmortizationMethod Interface is returned when calling AmortizeJS.calculate(config), the following attributes are available on it:

AttributeTypeDescription
balancenumberThe loan amount.
periodsnumberThe total number of periods.
periodicInterestnumberThe interest payed per period, if the period is month then the APR will be divided by 12 (ex: APR = 3.5%, i = 0.035/12).
periodicPaymentnumberThe total payment that needs to be made per period.
schedulePayment[]Array of payments required to pay off the balance.
totalPaymentnumberThe total amount of all payments over the term.
totalInterestnumberThe total amount of interest spend over the term.
startDateDate (Optional)The start date of the loan.
endDateDate (Optional)The pay off date (Will only be calculated if startDate was given).

Payment

A payment for the loan, will include the following:

AttributeTypeDescription
interestnumberPortion of the payment that goes to interest.
principalnumberPortion of the payment that goes to principal.
remainingBalancenumberRemaining balance after this payment.
dateDateThe date of the payment.

Formatting of results in examples

Results are not truncated or formatted in any way, the results in the examples are truncated for clarity.

Contributing

It is easy to extend AmortizeJS with custom amortization methods, all you need to do is create a javascript class or function that can be initiated via the new operator. This constructor will be supplied the following arguments in order:

ArgumentTypeDescription
balancenumberThe loan amount.
periodicInterestnumberThe interest payed per period.
periodsnumberThe total number of periods.
startDateDate (Optional)The start date of the loan.

The constructor should return an object that conforms to the AmortizationMethod interface.

Using a custom method

function customFunction(balance, periodicInterest, loanTerm, startDate){
    //Your custom amortization algorithm here
    return arguments;
}

var mortgage = Calculator.calculate({
    method:   customFunction,
    apr:      3.5, 
    balance:  280350, 
    loanTerm: 60,
    startDate: new Date('December 24 2017')
});

console.log(mortgage); //{"0": 280350, "1": 0.00292, "2": 60, "3": "2017-12-24T05:00:00.000Z"}

Using a custom class

class CustomMethod{
    constructor(balance, periodicInterest, loanTerm, startDate){
        //Your custom amortization algorithm here
        return arguments;
    }
}

var mortgage = Calculator.calculate({
    method:   CustomMethod,
    apr:      3.5, 
    balance:  280350, 
    loanTerm: 60,
    startDate: new Date('December 24 2017')
});

console.log(mortgage); //{"0": 280350, "1": 0.00292, "2": 60, "3": "2017-12-24T05:00:00.000Z"}