Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
: 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 ExpenseTagsData(
report: ReportStateT,
transactions: TransactionStateT[],
exchangeRate: ExchangeRateT,
base: string
): ReportDataT {
const data = new Map();
for (const tx of transactions) {
if (tx.kind !== TransationKindT.Expense || !tx.tags) continue;
for (const tag of tx.tags) {
const tagAmount = data.get(tag) || 0;
const amount = Currency.convert(
Math.abs(tx.amount),
exchangeRate[tx.currency],
base,
tx.currency
);
data.set(tag, tagAmount + amount);
}
}
const sorted = new Map([...data.entries()].sort((a, b) => b[1] - a[1]));
return {
labels: [...sorted.keys()],
series: [
[...sorted.values()].map(amount =>
Math.floor(Currency.centsToNumber(amount, base))
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,
tx.currency
);
}
if (lastPeriod && lastPeriod > 0) data[0] = data[lastPeriod];
if (report.date.end < Date.now() && data.length < labels.length) {
data[labels.length - 1] = data[data.length - 1];
}
return {
labels,
series: [
data.map(amount => Math.floor(Currency.centsToNumber(amount, base)))