UNPKG

emr-types

Version:

Comprehensive TypeScript Types Library for Electronic Medical Record (EMR) Applications - Domain-Driven Design with Zod Validation

148 lines 4.04 kB
/** * Supported Currencies */ export const SUPPORTED_CURRENCIES = { USD: { code: 'USD', name: 'US Dollar', symbol: '$', precision: 2, isActive: true }, VND: { code: 'VND', name: 'Vietnamese Dong', symbol: '₫', precision: 0, isActive: true }, EUR: { code: 'EUR', name: 'Euro', symbol: '€', precision: 2, isActive: true }, GBP: { code: 'GBP', name: 'British Pound', symbol: '£', precision: 2, isActive: true } }; /** * Money Factory Functions */ export const MoneyFactory = { /** * Create money from amount and currency */ create: (amount, currency) => { const currencyInfo = SUPPORTED_CURRENCIES[currency]; if (!currencyInfo) { throw new Error(`Unsupported currency: ${currency}`); } const precision = currencyInfo.precision; const roundedAmount = Math.round(amount * Math.pow(10, precision)); return { amount: roundedAmount, currency, precision, isValid: true, formatted: formatMoney(roundedAmount, currency, precision) }; }, /** * Create money from string representation */ fromString: (amount, currency) => { const numAmount = parseFloat(amount); if (isNaN(numAmount)) { throw new Error(`Invalid amount: ${amount}`); } return MoneyFactory.create(numAmount, currency); }, /** * Create zero money for a currency */ zero: (currency) => { return MoneyFactory.create(0, currency); } }; /** * Money Utility Functions */ export const MoneyUtils = { /** * Add two money amounts (must be same currency) */ add: (money1, money2) => { if (money1.currency !== money2.currency) { throw new Error('Cannot add money with different currencies'); } const totalAmount = money1.amount + money2.amount; return MoneyFactory.create(totalAmount / Math.pow(10, money1.precision), money1.currency); }, /** * Subtract two money amounts (must be same currency) */ subtract: (money1, money2) => { if (money1.currency !== money2.currency) { throw new Error('Cannot subtract money with different currencies'); } const totalAmount = money1.amount - money2.amount; return MoneyFactory.create(totalAmount / Math.pow(10, money1.precision), money1.currency); }, /** * Multiply money by a factor */ multiply: (money, factor) => { const totalAmount = money.amount * factor; return MoneyFactory.create(totalAmount / Math.pow(10, money.precision), money.currency); }, /** * Divide money by a factor */ divide: (money, factor) => { if (factor === 0) { throw new Error('Cannot divide by zero'); } const totalAmount = money.amount / factor; return MoneyFactory.create(totalAmount / Math.pow(10, money.precision), money.currency); }, /** * Check if money is zero */ isZero: (money) => { return money.amount === 0; }, /** * Check if money is positive */ isPositive: (money) => { return money.amount > 0; }, /** * Check if money is negative */ isNegative: (money) => { return money.amount < 0; } }; /** * Format money for display */ function formatMoney(amount, currency, precision) { const currencyInfo = SUPPORTED_CURRENCIES[currency]; if (!currencyInfo) { return `${amount} ${currency}`; } const displayAmount = amount / Math.pow(10, precision); const formatted = displayAmount.toLocaleString('en-US', { minimumFractionDigits: precision, maximumFractionDigits: precision }); return `${currencyInfo.symbol}${formatted}`; } //# sourceMappingURL=Money.js.map