Files
pwap/shared/utils/format.ts
T
Claude 7b95b8a409 Centralize common code in shared folder
This commit reorganizes the codebase to eliminate duplication between web and mobile frontends by moving all commonly used files to the shared folder.

Changes:
- Moved lib files to shared/lib/:
  * wallet.ts, staking.ts, tiki.ts, identity.ts
  * multisig.ts, usdt.ts, scores.ts, citizenship-workflow.ts

- Moved utils to shared/utils/:
  * auth.ts, dex.ts
  * Created format.ts (extracted formatNumber from web utils)

- Created shared/theme/:
  * colors.ts (Kurdistan and App color definitions)

- Updated web configuration:
  * Added @pezkuwi/* path aliases in tsconfig.json and vite.config.ts
  * Updated all imports to use @pezkuwi/lib/*, @pezkuwi/utils/*, @pezkuwi/theme/*
  * Removed duplicate files from web/src/lib and web/src/utils

- Updated mobile configuration:
  * Added @pezkuwi/* path aliases in tsconfig.json
  * Updated theme/colors.ts to re-export from shared
  * Mobile already uses relative imports to shared (no changes needed)

Architecture Benefits:
- Single source of truth for common code
- No duplication between frontends
- Easier maintenance and consistency
- Clear separation of shared vs platform-specific code

Web-specific files kept:
- web/src/lib/supabase.ts
- web/src/lib/utils.ts (cn function for Tailwind, re-exports formatNumber from shared)

All imports updated and tested. Both web and mobile now use the centralized shared folder.
2025-11-14 22:46:39 +00:00

75 lines
2.0 KiB
TypeScript

/**
* Shared formatting utilities
* Platform-agnostic formatters for numbers, currency, etc.
*/
/**
* Format a number with K, M, B suffixes for large values
* @param value - The number to format
* @param decimals - Number of decimal places (default 2)
* @returns Formatted string
*/
export function formatNumber(value: number, decimals: number = 2): string {
if (value === 0) return '0';
if (value < 0.01) return '<0.01';
// For large numbers, use K, M, B suffixes
if (value >= 1e9) {
return (value / 1e9).toFixed(decimals) + 'B';
}
if (value >= 1e6) {
return (value / 1e6).toFixed(decimals) + 'M';
}
if (value >= 1e3) {
return (value / 1e3).toFixed(decimals) + 'K';
}
return value.toFixed(decimals);
}
/**
* Format a percentage value
* @param value - The percentage value (e.g., 0.15 for 15%)
* @param decimals - Number of decimal places (default 2)
* @returns Formatted percentage string
*/
export function formatPercentage(value: number, decimals: number = 2): string {
return `${(value * 100).toFixed(decimals)}%`;
}
/**
* Format a currency value
* @param value - The currency value
* @param symbol - Currency symbol (default '$')
* @param decimals - Number of decimal places (default 2)
* @returns Formatted currency string
*/
export function formatCurrency(
value: number,
symbol: string = '$',
decimals: number = 2
): string {
return `${symbol}${formatNumber(value, decimals)}`;
}
/**
* Parse a formatted number string back to number
* @param formatted - Formatted string (e.g., "1.5K")
* @returns Number value
*/
export function parseFormattedNumber(formatted: string): number {
const cleaned = formatted.replace(/[^0-9.KMB]/g, '');
const match = cleaned.match(/^([\d.]+)([KMB])?$/);
if (!match) return 0;
const [, numberPart, suffix] = match;
let value = parseFloat(numberPart);
if (suffix === 'K') value *= 1e3;
else if (suffix === 'M') value *= 1e6;
else if (suffix === 'B') value *= 1e9;
return value;
}