Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
rundef committed Aug 4, 2017
1 parent 609cecc commit f8966c6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "balance-growth",
"version": "2.0.0",
"version": "2.1.0",
"description": "Calculate monthly balance growth over time",
"main": "./dist/index.js",
"scripts": {
Expand Down
23 changes: 21 additions & 2 deletions src/BalanceAdjuster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export class BalanceAdjuster {
return (d1.year() > d2.year());
}

protected isDayLessThan(d1: moment.Moment, d2: moment.Moment): boolean {
if (d1.year() === d2.year()) {
if (d1.month() === d2.month()) {
return (d1.date() < d2.date());
}
return (d1.month() < d2.month());
}
return (d1.year() < d2.year());
}

protected isMonthGreaterOrEqual(d1: moment.Moment, d2: moment.Moment): boolean {
if (d1.year() === d2.year()) {
return (d1.month() >= d2.month());
Expand Down Expand Up @@ -102,8 +112,17 @@ export class BalanceAdjuster {
transactions: TransactionInTime<moment.Moment>[],
index: number
): BalanceInTime<moment.Moment> {
const balanceDiff = (balances[index].balance - balances[index - 1].balance) +
(-1 * this.getTransactionsAmount(balances[index - 1].date, balances[index].date, transactions));
let curBalance = balances[index].balance;
let transactionsMinDate = balances[index - 1].date.clone();
if (balances[index].date.date() == 1) {
transactionsMinDate = date.clone().startOf('month');
}

curBalance -= this.getTransactionsAmount(transactionsMinDate, balances[index].date, transactions);

const prevBalance = balances[index - 1].balance;
let balanceDiff = curBalance - prevBalance;

const dayDiffPrev = balances[index].date.diff(balances[index - 1].date, 'days');
const dayDiff = date.diff(balances[index - 1].date, 'days');

Expand Down
1 change: 0 additions & 1 deletion src/MonthlyBalanceAdjuster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export class MonthlyBalanceAdjuster extends BalanceAdjuster {
date: moment(b.date, 'YYYY-MM-DD')
};
});

const monthlyBalances: MonthlyBalance[] = [];
let index = 0;
this.loopEachMonth(balances, p.maxDate, (year: number, month: number): void => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
{ "month": "2016-11", "balance": 100, "eomBalance": 360 },
{ "month": "2016-11", "balance": 100, "eomBalance": 160 },
{ "month": "2016-12", "balance": 860, "eomBalance": null }
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{ "month": "2017-01", "balance": 2000, "eomBalance": 2500 },
{ "month": "2017-02", "balance": 3500, "eomBalance": 4000 },
{ "month": "2017-03", "balance": 5000, "eomBalance": 5500 },
{ "month": "2017-04", "balance": 6500, "eomBalance": null }
]
17 changes: 17 additions & 0 deletions tests/unit/json/03-monthly-balances-transactions/input06.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"balances": [
{ "date": "2017-01-01", "balance": 2000 },
{ "date": "2017-01-15", "balance": 3000 },
{ "date": "2017-02-01", "balance": 3500 },
{ "date": "2017-02-15", "balance": 3000 },
{ "date": "2017-03-01", "balance": 5000 },
{ "date": "2017-03-15", "balance": 6000 },
{ "date": "2017-04-01", "balance": 6500 }
],
"transactions": [
{ "date": "2017-01-10", "amount": 1000 },
{ "date": "2017-02-10", "amount": 500 },
{ "date": "2017-02-20", "amount": 500 },
{ "date": "2017-03-10", "amount": 1000 }
]
}
2 changes: 1 addition & 1 deletion tests/unit/json/04-growth/expected04.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{ "month": "2017-01", "balance": 0, "eomBalance": 6046, "growth": 0 },
{ "month": "2017-01", "balance": 0, "eomBalance": -4, "growth": 0 },
{ "month": "2017-02", "balance": 6046, "eomBalance": 5925, "growth": -2 },
{ "month": "2017-03", "balance": 5925, "eomBalance": null, "growth": null }
]

0 comments on commit f8966c6

Please sign in to comment.