{"size": 1527, "ext": "js", "max_stars_repo_path": "src/js/mortgage/payments.js", "max_stars_repo_name": "nl-hugo/hypotheek-calculator", "max_stars_repo_stars_event_min_datetime": "2020-01-10T18:53:19.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-19T20:39:47.000Z", "max_issues_repo_path": "src/js/mortgage/payments.js", "max_issues_repo_name": "nl-hugo/hypotheek-calculator", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/js/mortgage/payments.js", "max_forks_repo_name": "nl-hugo/hypotheek-calculator", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.7910447761, "max_line_length": 86, "alphanum_fraction": 0.6358873608}
/**
 *
 */

const periods = 12;

/**
 * Returns the loan's term in months.
 */
function terms(duration) {
  return duration * periods;
}

/**
 * Returns the loan's monthly interest rate.
 */
function monthlyRate(rate) {
  return rate / periods;
}

/**
 * Returns the amount of interest for the value p with rate r
 */
function interest(principal, rate) {
  return principal * monthlyRate(rate);
}

/**
 * Returns the monthly amount to be paid in a annuity mortgage
 * p = principal (initial amount)
 * r = yearly interest rate
 * n = number of years
 */
function annuity(principal, rate, duration) {
  const i = monthlyRate(rate);
  const t = Math.pow(1 + i, terms(duration));
  return i * principal * t / (t - 1);
}

/**
 * Returns the monthly amount to be paid in a linear mortgage
 */
function linear(principal, duration) {
  return principal / terms(duration);
}

/**
 * Returns the montly payments for a mortgage of the specified type.
 */
export default function payments(principal, rate, duration, type) {
  return Array.apply(null, Array(terms(duration))).map((d, i, a) => {
    d = {
      "id" : i,
      "principal" : (i === 0 ? principal : a[i - 1].principal - a[i - 1].amortization)
    };
    d.interest = interest(d.principal, rate);
    if (type === "annuity") {
      d.payment = annuity(principal, rate, duration);
      d.amortization = d.payment - d.interest;
    } else {
      d.amortization = linear(principal, duration);
      d.payment = d.amortization + d.interest;
    }
    return a[i] = d;
  });
}
