Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
report.timespan === ReportTimespanT.Yearly
? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
: range(1, getDaysInMonth(report.date.start) + 1).map(day =>
`${day}`.padStart(2, '0')
);
const data = [
new Array(labels.length).fill(0), // income series
new Array(labels.length).fill(0) // expense series
];
for (const tx of transactions) {
if (tx.kind !== Expense && tx.kind !== Income) continue;
const period = format(
toUtcTimestamp(tx.date),
report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
);
data[tx.kind === Income ? 0 : 1][parseInt(period) - 1] += Currency.convert(
Math.abs(tx.amount),
exchangeRate[tx.currency],
base,
tx.currency
);
}
return {
labels,
series: data.map(set =>
set.map(amount => Math.floor(Currency.centsToNumber(amount, base)))
)
};
}
export default function ExpenseIncomeData(
report: ReportStateT,
transactions: TransactionStateT[],
exchangeRate: ExchangeRateT,
base: string
): ReportDataT {
const labels =
report.timespan === ReportTimespanT.Yearly
? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
: range(1, getDaysInMonth(report.date.start) + 1).map(day =>
`${day}`.padStart(2, '0')
);
const data = [
new Array(labels.length).fill(0), // income series
new Array(labels.length).fill(0) // expense series
];
for (const tx of transactions) {
if (tx.kind !== Expense && tx.kind !== Income) continue;
const period = format(
toUtcTimestamp(tx.date),
report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
);
export default function NetWorthData(
report: ReportStateT,
transactions: TransactionStateT[],
exchangeRate: ExchangeRateT,
base: string,
netWorthEnd: number
): ReportDataT {
const labels =
report.timespan === ReportTimespanT.Yearly
? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
: range(1, getDaysInMonth(report.date.start) + 1).map(day =>
`${day}`.padStart(2, '0')
);
const data = [];
let lastPeriod: number | undefined;
for (const tx of transactions) {
if (tx.kind !== Expense && tx.kind !== Income) continue;
const period =
parseInt(
format(
toUtcTimestamp(tx.date),
report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
)
report.timespan === ReportTimespanT.Yearly
? range(0, 12).map(month => format(new Date().setMonth(month), 'MMM'))
: range(1, getDaysInMonth(report.date.start) + 1).map(day =>
`${day}`.padStart(2, '0')
);
const data = [];
let lastPeriod: number | undefined;
for (const tx of transactions) {
if (tx.kind !== Expense && tx.kind !== Income) continue;
const period =
parseInt(
format(
toUtcTimestamp(tx.date),
report.timespan === ReportTimespanT.Yearly ? 'M' : 'D'
)
) - 1;
if (period !== lastPeriod) {
if (lastPeriod === undefined) {
lastPeriod = period + 1;
data[lastPeriod] = netWorthEnd;
}
data[period] = data[lastPeriod];
}
lastPeriod = period;
data[period] -= Currency.convert(
tx.amount,
exchangeRate[tx.currency],
base,