@cranberry-money/shared-utils
Version:
Shared utility functions for Blueberry platform
25 lines • 1.58 kB
JavaScript
import { LOCALE_AUSTRALIA, DEFAULT_NUMERIC_ZERO, DEFAULT_EMPTY_STRING, CURRENCY_AUD, } from '@cranberry-money/shared-constants';
import { NUMBER_FORMAT_OPTIONS_CURRENCY } from './currency';
export function formatPortfolioValue(value) {
const numValue = typeof value === 'string' ? parseFloat(value) : value;
return new Intl.NumberFormat(LOCALE_AUSTRALIA, {
...NUMBER_FORMAT_OPTIONS_CURRENCY,
currency: CURRENCY_AUD,
}).format(numValue);
}
export function calculateTotalValue(marketValue, cashBalance) {
const market = typeof marketValue === 'string' ? parseFloat(marketValue || DEFAULT_EMPTY_STRING) : marketValue;
const cash = typeof cashBalance === 'string' ? parseFloat(cashBalance || DEFAULT_EMPTY_STRING) : cashBalance;
return market + cash;
}
export function getMarketAllocation(marketValue, totalValue) {
const market = typeof marketValue === 'string' ? parseFloat(marketValue || DEFAULT_EMPTY_STRING) : marketValue;
const total = typeof totalValue === 'string' ? parseFloat(totalValue || DEFAULT_EMPTY_STRING) : totalValue;
return total > DEFAULT_NUMERIC_ZERO ? (market / total) * 100 : DEFAULT_NUMERIC_ZERO;
}
export function getCashAllocation(cashBalance, totalValue) {
const cash = typeof cashBalance === 'string' ? parseFloat(cashBalance || DEFAULT_EMPTY_STRING) : cashBalance;
const total = typeof totalValue === 'string' ? parseFloat(totalValue || DEFAULT_EMPTY_STRING) : totalValue;
return total > DEFAULT_NUMERIC_ZERO ? (cash / total) * 100 : DEFAULT_NUMERIC_ZERO;
}
//# sourceMappingURL=portfolio.js.map