Skip to content

Instantly share code, notes, and snippets.

@rodoabad
Last active June 10, 2018 08:21
Show Gist options
  • Select an option

  • Save rodoabad/7413e680396429a6b9041540ae9ab5fa to your computer and use it in GitHub Desktop.

Select an option

Save rodoabad/7413e680396429a6b9041540ae9ab5fa to your computer and use it in GitHub Desktop.
import BigNumber from 'bignumber.js';
import moment from 'moment';
const defaultDecimalPlaces = 10;
const findApplicableSplitsAgainstDividendYear = (dividendYear, splits) => {
const formattedDividendYear = Number(moment(dividendYear).format('X'));
return splits.reduce((acc, split) => {
const formattedExDate = Number(moment(split.exDate).format('X'));
return formattedExDate > formattedDividendYear ?
[
...acc,
split.ratio
] :
acc;
}, []);
}
const applySplitsRatiosToMatchingDividendAmount = (dividendAmount, applicableSplits) => applicableSplits.reduce((acc, ratio) => {
const amountWithAppliedSplitRatio = acc.multipliedBy(ratio);
return amountWithAppliedSplitRatio;
}, dividendAmount);
export const adjustDividendAmountForSplits = (dividend, splits) => {
const dividendAmount = new BigNumber(dividend.amount);
const applicableSplits = findApplicableSplitsAgainstDividendYear(dividend.exDate, splits);
const divAmountWithAppliedSplitRatios = applySplitsRatiosToMatchingDividendAmount(dividendAmount, applicableSplits);
return divAmountWithAppliedSplitRatios.decimalPlaces(defaultDecimalPlaces).toNumber();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment