npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

financial-fns

v0.0.8

Published

Library with financial functions, useful calculations and constants

Downloads

441

Readme

financial-fns

Library with financial functions, useful calculations and constants

NPM version NPM downloads Open issues Open prs


Install

npm install financial-fns

Usage

Use se import or require

ES6 Modules

import { xirr } from 'financial-fns'

const result = xirr(
	[-10000, 2750, 4250, 3250, 2750],
	[
		new Date('2008-01-01'),
		new Date('2008-03-01'),
		new Date('2008-10-30'),
		new Date('2009-02-15'),
		new Date('2009-04-01')
	]
)

console.log('RESULT', result) // RESULT 0.3733625335188315

CommonJS

const { xirr } require('financial-fns')

const result = xirr(
	[-10000, 2750, 4250, 3250, 2750],
	[
		new Date('2008-01-01'),
		new Date('2008-03-01'),
		new Date('2008-10-30'),
		new Date('2009-02-15'),
		new Date('2009-04-01')
	]
)

console.log('RESULT', result) // RESULT 0.3733625335188315

Functions

XIRR

Calculates the internal rate of return (IRR) for a schedule of cash flows that is not necessarily periodic. Excel

  • =XIRR (English)
  • =TIR.NO.PER (Spanish)
xirr(cashFlow, dates, guess)

Example

const result = xirr(
	[-10000, 2750, 4250, 3250, 2750],
	[
		new Date('2008-01-01'),
		new Date('2008-03-01'),
		new Date('2008-10-30'),
		new Date('2009-02-15'),
		new Date('2009-04-01')
	]
)
console.log(result) // 0.3733625335188315

IRR

Calculates the internal rate of return (IRR) for a series of cash flows represented by the numbers in values. Excel:

  • =IRR (English)
  • =TIR (Spanish)
// guess default 0,1
irr(cashFlow, guess)

Example

const result = irr([-10000, 2750, 4250, 3250, 2750])
console.log(result) // 0.1154127831005586

NPV

Calculates the net present value of an investment based on a discount rate and a series of cash flows.

Excel:

  • =NPV (English)
  • =VNA (Spanish)
// rate default 0
npv(cashFlow, rate)

Example

const result = npv(
	[
		-1333.11,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46,
		178.46
	],
	0.05
)
console.log(result) // 248.62588704065456

XNPV

Calculates the net present value of an investment based on a discount rate and a series of cash flows.

Excel:

  • =XNPV (English)
  • =VNA.NO.PER (Spanish)
// rate default 0
xnpv(cashFlow, dates, rate)

Example

const result = xnpv(
	[-10000, 2750, 4250, 3250, 2750],
	[
		new Date('2008-01-01'),
		new Date('2008-03-01'),
		new Date('2008-10-30'),
		new Date('2009-02-15'),
		new Date('2009-04-01')
	],
	0.1
)
console.log(result) // 1994.5100406532633

Rate

Monthly rate

Excel:

  • =RATE (English)
  • =TASA (Spanish)
rate(
	nPeriods,
	payment,
	presentValue,
	(futureValue = 0),
	(dueDateType = 0),
	(guess = 0.1)
)

Example

const monthlyRate = rate(4 * 12, -200, 8000)
console.log(monthlyRate) // 0.0077014724882104105
const annualRate = rate(4 * 12, -200, 8000) * 12
console.log(annualRate) // 0.09241766985852493

NPER

Calculate number of periods

Excel:

  • =NPER (English)
  • =NPER (Spanish)
nper(rate, payment, presentValue, (futureValue = 0), (dueDateType = 0))

Example

const result = nper(0, 500, -25000)
console.log(result) // 50

FV

Calculate the future value of an investment based on a constant interest rate. You can use FV with either periodic, constant payments, or a single lump sum payment. Excel:

  • =FV (English)
  • =VF (Spanish)
fv(rate, totalPeriods, payment, (presentValue = 0), (dueDateType = 0))

Example

const result = fv(0.12 / 12, 12, -1000)
console.log(result) // 12682.503013196972

FV Schedule

Future value (applying a series of compound interest rates) Excel:

  • =FVSCHEDULE (English)
  • =VF.PLAN (Spanish)
fvSchedule(principal, schedule)

Example

const result = fvSchedule(336, [0.02, 0.05, 0.03])
console.log(result) // 370.65168

Effect

Returns the effective annual interest rate, given the nominal annual interest rate and the number of compounding periods per year. Excel:

  • =EFFECT (English)
  • =INT.EFECTIVO (Spanish)
effect(rate, nPeriods)

Example

const result = effect(0.1, 12)
console.log(result) // 0.10471306744129724

Day count by date

Count days between two dates without time

dayCountByDate(dateFrom, dateTo, (abs = false))

Example

const dayCount = dayCountByDate(new Date('2022-06-10'), new Date('2022-02-18'))
console.log(dayCount) // -112

const dayCountAbs = dayCountByDate(
	new Date('2022-06-10'),
	new Date('2022-02-18'),
	true
)
console.log(dayCountAbs) // 112

Day count by dateTime

Count days between two dates without time

dayCountByDateTime(dateFrom, dateTo, (abs = false))

Example

const dayCount = dayCountByDateTime(
	new Date('2022-03-04 22:00:00'),
	new Date('2022-02-27 22:00:00')
)
console.log(dayCount) // -5

const dayCountAbs = dayCountByDateTime(
	new Date('2022-03-04 22:00:00'),
	new Date('2022-02-27 22:00:00'),
	true
)
console.log(dayCountAbs) // 5
const dayCountAbs2 = dayCountByDateTime(
	new Date('2022-03-04 23:00:00'),
	new Date('2022-02-27 22:00:00'),
	true
)
console.log(dayCountAbs2) // 5.04

Get days by frequency

Get total days in a frequency

getDaysByFrequency(frequency)

Example

/**
 * weekly: 7,
 * biWeekly: 14,
 * monthly: 1,
 * everyTwoMonths: 2,
 * everyThreeMonths: 3,
 * everyFourMonths: 4,
 * everySixMonths: 6,
 * yearly: 12,
 */
const monthlyDays = getDaysByFrequency(1)
console.log(monthlyDays) // 30
const biWeekly = getDaysByFrequency(14)
console.log(biWeekly) // 15

Last date of month

Get last date of month.

getLastDateOfMonth(month, year)

Example

/**
 * JS Months from 0 to 11
 */
const lastDate1 = getLastDateOfMonth(1, 2022)
console.log(lastDate) // 28
const lastDate2 = getLastDateOfMonth(1, 2024)
console.log(lastDate) // 29

Round

Round a number to a specified number of decimal places.

round(num, places)

Example

const result = round(135.15000004)
console.log(result) // 135.15
const result2 = round(135.15366007, 4)
console.log(result2) // 135.1537