@cranberry-money/shared-utils
Version:
Shared utility functions for Blueberry platform
72 lines • 3.16 kB
JavaScript
import { DEFAULT_CURRENCY, DESIGN_TOKENS } from '@cranberry-money/shared-constants';
import { formatCurrencyWithCode } from './currency';
import { calculateHoldingAllocations } from './holdings';
export const processAllocationData = (allocations, holdings) => {
const actualAllocations = calculateHoldingAllocations(holdings);
const instrumentMap = new Map();
allocations.forEach(allocation => {
instrumentMap.set(allocation.instrument, {
symbol: allocation.instrumentSymbol,
name: allocation.instrumentName,
targetPercentage: parseFloat(allocation.percentage),
});
});
holdings.forEach(holding => {
const existing = instrumentMap.get(holding.instrument.uuid);
const currentPrice = parseFloat(holding.instrument.currentPrice || '0');
const value = holding.quantity * currentPrice;
instrumentMap.set(holding.instrument.uuid, {
symbol: holding.instrumentSymbol,
name: holding.instrumentName,
targetPercentage: existing?.targetPercentage,
actualPercentage: actualAllocations[holding.instrument.uuid] || 0,
quantity: holding.quantity,
currentPrice,
value,
});
});
const items = [];
let colorIndex = 0;
instrumentMap.forEach((data, instrumentUuid) => {
items.push({
instrumentUuid,
symbol: data.symbol,
name: data.name,
targetPercentage: data.targetPercentage,
actualPercentage: data.actualPercentage,
quantity: data.quantity,
currentPrice: data.currentPrice,
value: data.value,
color: DESIGN_TOKENS.colors.chart[colorIndex++ % DESIGN_TOKENS.colors.chart.length],
hasTarget: data.targetPercentage !== undefined,
hasActual: data.actualPercentage !== undefined && data.actualPercentage > 0,
});
});
return items.sort((a, b) => {
const aTarget = a.targetPercentage || 0;
const bTarget = b.targetPercentage || 0;
if (aTarget !== bTarget)
return bTarget - aTarget;
const aActual = a.actualPercentage || 0;
const bActual = b.actualPercentage || 0;
return bActual - aActual;
});
};
export const calculateTotals = (items) => {
const totalTarget = items.reduce((sum, item) => sum + (item.targetPercentage || 0), 0);
const totalActual = items.reduce((sum, item) => sum + (item.actualPercentage || 0), 0);
const totalValue = items.reduce((sum, item) => sum + (item.value || 0), 0);
const totalQuantity = items.reduce((sum, item) => sum + (item.quantity || 0), 0);
return { totalTarget, totalActual, totalValue, totalQuantity };
};
export const formatPercentage = (value) => {
if (value === undefined)
return '0.0%';
return `${value.toFixed(1)}%`;
};
export const formatDashboardCurrency = (value, currency = DEFAULT_CURRENCY) => {
if (value === undefined || value === null)
return '—';
return formatCurrencyWithCode(value, currency, 'en-AU', 2, 2);
};
//# sourceMappingURL=dashboard.js.map