1.0.3 • Published 8 years ago

capitaljs v1.0.3

Weekly downloads
14
License
CC0-1.0
Repository
github
Last release
8 years ago

Capital.js Build Status

Installation

In a browser:

<script src="capital.js"></script>

Using npm:

$ npm install capitaljs

Usage

In a browser:

var rate = capitaljs.compoundAnnualGrowthRate({...});

In an AMD loader:

require(['capitaljs'], function(capitaljs) {...});

In Node:

var capitaljs = require('capitaljs');
var rate = capitaljs.compoundAnnualGrowthRate({...});

Or if you just want a single formula (to reduce your browser bundle's filesize):

var cagr = require('capitaljs/compoundAnnualGrowthRate');
var rate = cagr({...});

Current formulae

Amortization

Calculate the monthly payment of an amortizing loan (like a mortgage) and how the loan has amortized after a specified period of time.

Options

OptionFormatDetails
amountnumberTotal amount of the loan
ratenumberAnnual interest rate of the loan
totalTermnumberTotal length of the loan, in months
amortizeTermnumberPoint in time after the start of the loan you’d like to check the amortization, in months

Returns

PropertyFormatDetails
balancenumberTotal amount of principal left to be paid after the number of months specified in amortizeTerm
balanceRoundstringbalance rounded to the nearest cent
interestnumberTotal amount of interest paid on the loan after the number of months specified in amortizeTerm
interestRoundstringinterest rounded to the nearest cent
paymentnumberMonthly payment
paymentRoundstringpayment rounded to the nearest cent
principalnumberTotal amount of principal paid on the loan after the number of months specified in amortizeTerm
principalRoundstringprincipal rounded to the nearest cent

Example

var amortization = require('capitaljs/amortization');

// What’s the amortization like after 5 years for a 30-year, $180,000 mortgage
// with a 4.25% interest rate?
amortization( {
  amount: 180000,
  rate: 4.25,
  totalTerm: 360,
  amortizeTerm: 60
} );

/*
{
  balance: 163453.85387151438,
  balanceRound: "163453.85",
  interest: 36583.362108097754,
  interestRound: "36583.36",
  payment: 885.4918039430557,
  paymentRound: "885.49",
  principal: 16546.146128485594,
  principalRound: "16546.15"
}
*/

Cash flow

Calculate how much money is left after subtracting expenses from income.

Options

OptionFormatDetails
incomepositive number or arrayTotal income or an array with all sources of income
expensespositive number or arrayTotal expenses or an array with all expenses

Returns

PropertyFormatDetails
cashnumberTotal cash left over after subtracting expenses from income
expensesnumberTotal expenses
incomenumberTotal income

Examples

var cashFlow = require('capitaljs/cashFlow');

// How much money is left over given $50,000 of expenses and a $100,000 income?
cashFlow( {
  income: 100000,
  expenses: 50000
} );

/*
{
  cash: 50000,
  expenses: 50000,
  income: 100000
}
*/

// How much money is left over given a bunch of different income sources and
// a bunch of different expenses?
cashFlow( {
  income: [300, 14000, 189],
  expenses: [90, 681, 15980, 670]
} );

/*
{
  cash: -2932,
  expenses: 17421,
  income: 14489
}
*/

Compound annual growth rate

Calculate a compounded annual rate of return on an investment.

Options

OptionFormatDetails
startValuenumberStarting value of the investment
endValuenumberEnding value of the investment
yearsnumberLength of the investment, in years

Returns

PropertyFormatDetails
percentnumberCompound annual growth rate of the investment, as an unrounded percent
rawnumberRaw compound annual growth rate of the investment
roundednumberRaw compound annual growth rate of the investment, rounded to the nearest thousandth

Example

var compoundAnnualGrowthRate = require('capitaljs/compoundAnnualGrowthRate');

// What’s the compound annual growth rate of an investment that started at
// $50,000 and grew to $100,000 in 10 years?
compoundAnnualGrowthRate( {
  startValue: 50000,
  endValue: 100000,
  years: 10
} );

/*
{
  percent: 7.199999999999999,
  raw: 0.07177346253629313,
  rounded: 0.072
}
*/

Inflation-adjusted return

Calculate the return on an investment, accounting for inflation over the lifetime of the investment.

Options

OptionFormatDetails
investmentReturnnumberInvestment’s lifetime return, as a decimal (so “8%” is ".06")
inflationRatenumberInflation rate over the lifetime of the investment, as a decimal (so “3%” is “.03”)

Returns

A single number, the inflation-adjusted return, as a percent rounded to the nearest hundredth

Example

var inflationAdjustedReturn = require('capitaljs/inflationAdjustedReturn');

// Adjusting for a 3% inflation rate, what’s the return for an investment that
// grew by 8%?
inflationAdjustedReturn( {
  investmentReturn: .08,
  inflationRate: .03
} );

// 4.85

Interest accrued

Calculate how much interest has been earned on an initial investment after a period of time.

Options

OptionFormatDetails
principalnumberInitial amount invested
ratenumberPeriodic interest rate, as a percent
periodsnumberYears since the investment was started
compoundingsnumberHow often the interest rate is compounded per year (12 would be monthly, 4 is quarterly, 1 is yearly)

Returns

PropertyFormatDetails
interestnumberTotal interest earned on the investment
totalnumberTotal current value of the investment, rounded to the nearest cent

Examples

var interest = require('capitaljs/interest');

// What’s a $10,000 investment with a 6% interest rate that compounds yearly
// worth after 10 years
interest( {
  principal: 10000,
  rate: 6,
  periods: 10,
  compoundings: 1
} );

/*
{
  interest: 7908.48,
  total: 17908.48
}
*/

// What’s a $10,000 investment with a 6% interest rate that compounds monthly
// worth after 10 years
interest( {
  principal: 10000,
  rate: 6,
  periods: 10,
  compoundings: 12
} );

/*
{
  interest: 8193.970000000001,
  total: 18193.97
}
*/

Return on investment

Calculate the return on an investment.

Options

OptionFormatDetails
earningsnumberFinal value of the investment
initialInvestmentnumberInitial value of the investment

Returns

PropertyFormatDetails
percentnumberReturn on investment, as an unrounded percent
rawnumberRaw return on investment
roundednumberRaw return on investment, rounded to the nearest ten thousandth

Example

var returnOnInvestment = require('capitaljs/returnOnInvestment');

// What’s the return on a $70,000 investment that grew to $89,700?
returnOnInvestment( {
  earnings: 89700,
  initialInvestment: 70000
} );

/*
{
  percent: 28.139999999999997,
  raw: 0.2814285714285714,
  rounded: 0.2814
}
*/

Contributing

Please read the Contributing guidelines.

Running Tests

$ npm test

License

The project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication.

All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.

Software source code previously released under an open source license and then modified by CFPB staff is considered a "joint work" (see 17 USC § 101); it is partially copyrighted, partially public domain, and as a whole is protected by the copyrights of the non-government authors and must be released according to the terms of the original open-source license.

For further details, please see: http://www.consumerfinance.gov/developers/sourcecodepolicy/