All files / src/irr/xirr xirr.ts

100% Statements 14/14
0% Branches 0/1
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 251x     1x 1x   1x       2x 7x 2x 2x 2x 2x   7x   2x          
import { irr } from '../irr'
import { RootFinderOptions } from '../../root-finder'
import { XirrInput } from '../definition'
import { transform } from '../transform'
import { zeros } from '../../utils'
 
export function xirr(
  inputs: XirrInput[],
  options: Partial<RootFinderOptions> = {},
): { days: number; rate: number } {
  const transformedInputs = transform(inputs)
  const days = transformedInputs.map(input => input.day)
  const firstDay = Math.min(...days)
  const lastDay = Math.max(...days)
  const totalDays = lastDay - firstDay + 1
  const coefficients: number[] = zeros(totalDays)
 
  transformedInputs.forEach(({ amount, day }) => (coefficients[day] += amount))
 
  return {
    days: totalDays,
    rate: irr(coefficients, options),
  }
}