platform-codebase/@packages/@plugins/analytics/src/components/ui-utils-stubs.ts

81 lines
2.1 KiB
TypeScript
Executable file

/**
* Temporary stub utilities for @ui/utils
* These allow the plugin to compile independently.
*/
export type NumberFormat = 'number' | 'currency' | 'percentage' | 'compact';
export const formatCompactNumber = (value: number): string => {
if (value >= 1_000_000_000) {
return `${(value / 1_000_000_000).toFixed(1)}B`;
}
if (value >= 1_000_000) {
return `${(value / 1_000_000).toFixed(1)}M`;
}
if (value >= 1_000) {
return `${(value / 1_000).toFixed(1)}K`;
}
return value.toString();
};
export const formatCurrency = (value: number, currency = 'USD'): string => {
const formatted = new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
minimumFractionDigits: 0,
maximumFractionDigits: 2,
}).format(value);
return formatted;
};
export const formatPercentage = (value: number, decimals = 1): string => {
return `${value.toFixed(decimals)}%`;
};
export const formatValue = (value: number | string, format?: NumberFormat): string => {
if (typeof value === 'string') return value;
switch (format) {
case 'currency':
return formatCurrency(value);
case 'percentage':
return formatPercentage(value);
case 'compact':
return formatCompactNumber(value);
default:
return value.toLocaleString();
}
};
export const calculateSparklinePoints = (data: number[], width: number, height: number): string => {
if (!data || data.length === 0) return '';
const max = Math.max(...data);
const min = Math.min(...data);
const range = max - min || 1;
return data
.map((value, index) => {
const x = (index / (data.length - 1)) * width;
const y = height - ((value - min) / range) * height;
return `${x},${y}`;
})
.join(' ');
};
export const generateLinePath = (points: string): string => {
if (!points) return '';
const coords = points.split(' ');
if (coords.length === 0) return '';
const [firstX, firstY] = coords[0].split(',');
let path = `M ${firstX},${firstY}`;
for (let i = 1; i < coords.length; i++) {
const [x, y] = coords[i].split(',');
path += ` L ${x},${y}`;
}
return path;
};