|
| 1 | +var debounce = require('debounce'); |
| 2 | +var cost = require('overall-loan-cost'); |
| 3 | +var formatUSD = require('format-usd'); |
| 4 | +var unFormatUSD = require('unformat-usd'); |
| 5 | +var isMoney = require('is-money-usd'); |
| 6 | +var amortize = require('amortize'); |
| 7 | + |
| 8 | +function updateComparisons() { |
| 9 | + |
| 10 | + var $monthly = $('#monthly-payment span'), |
| 11 | + $overall = $('#overall-costs span'), |
| 12 | + loanInfo = getLoanInfo(), |
| 13 | + costs = calcCosts( loanInfo ); |
| 14 | + |
| 15 | + $monthly.text( formatUSD(costs.monthlyPayment) ); |
| 16 | + $overall.text( formatUSD(costs.overallCost) ); |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +function getLoanInfo() { |
| 21 | + |
| 22 | + function _sanitizeMoney( val ) { |
| 23 | + return isMoney( val ) ? unFormatUSD( val ) : 0; |
| 24 | + } |
| 25 | + |
| 26 | + return { |
| 27 | + location: $('#location').val(), |
| 28 | + creditScore: $('#credit-score-select').val(), |
| 29 | + amountBorrowed: _sanitizeMoney( $('#house-price-input').val() - $('#down-payment-input').val() ), |
| 30 | + rateStructure: $('#rate-structure-select').val(), |
| 31 | + termLength: $('#loan-term-select').val(), |
| 32 | + loanType: $('#loan-type-select').val(), |
| 33 | + armType: $('#arm-type-select').val(), |
| 34 | + rate: $('#interest-rate-select').val() |
| 35 | + }; |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Calculate loan costs given loan details provided by the user. |
| 41 | + * @return {object} monthly payment and overall cost of the loan |
| 42 | + */ |
| 43 | +function calcCosts( opts ) { |
| 44 | + |
| 45 | + var loan, |
| 46 | + overall; |
| 47 | + |
| 48 | + opts = opts || {}; |
| 49 | + |
| 50 | + loan = amortize({ |
| 51 | + amount: opts.amountBorrowed, |
| 52 | + rate: opts.rate, |
| 53 | + totalTerm: opts.termLength, |
| 54 | + amortizeTerm: 60 |
| 55 | + }); |
| 56 | + |
| 57 | + overall = cost({ |
| 58 | + amountBorrowed: opts.amountBorrowed, |
| 59 | + rate: opts.rate, |
| 60 | + totalTerm: opts.termLength, |
| 61 | + }); |
| 62 | + |
| 63 | + return { |
| 64 | + monthlyPayment: loan.payment, |
| 65 | + overallCosts: overall.overallCost |
| 66 | + }; |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +$('.comparisons').on( 'change', '.recalc', updateComparisons ); |
0 commit comments