|
| 1 | +import { formatNumber } from '@angular/common'; |
| 2 | + |
| 3 | +import { Amount } from './amount.model'; |
| 4 | + |
| 5 | +export function formatAmount( |
| 6 | + amount: Amount, |
| 7 | + amountServiceConfiguration: AmountServiceConfiguration, |
| 8 | + locale: string, |
| 9 | + nativeCurrency: string |
| 10 | +) { |
| 11 | + const config = deriveConfiguration(amountServiceConfiguration); |
| 12 | + |
| 13 | + let formattedAmount = formatNumber(amount && amount.amount, locale, config.digitsInfo); |
| 14 | + |
| 15 | + if (config.stripSign) { |
| 16 | + formattedAmount = formattedAmount.replace('-', '').trim(); |
| 17 | + } |
| 18 | + |
| 19 | + const currencyCodeToAppend = deriveCurrencyCode(config, amount, nativeCurrency); |
| 20 | + |
| 21 | + if (!currencyCodeToAppend) { |
| 22 | + return formattedAmount; |
| 23 | + } |
| 24 | + if (config.currencyCodePosition === 'postfix') { |
| 25 | + return formattedAmount + ' ' + currencyCodeToAppend; |
| 26 | + } else { |
| 27 | + return currencyCodeToAppend + ' ' + formattedAmount; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export function deriveCurrencyCode( |
| 32 | + config: AmountServiceConfiguration, |
| 33 | + amount: Amount, |
| 34 | + nativeCurrency: string |
| 35 | +) { |
| 36 | + let currencyCodeToAppend; |
| 37 | + |
| 38 | + if (config.showCurrencyCode) { |
| 39 | + if (config.showCurrencyCode === 'alwaysShowCurrency') { |
| 40 | + currencyCodeToAppend = amount && amount.currencyCode; |
| 41 | + } else if (config.showCurrencyCode === 'showForeignCurrency') { |
| 42 | + currencyCodeToAppend = |
| 43 | + amount && amount.currencyCode !== nativeCurrency ? amount.currencyCode : ''; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return currencyCodeToAppend || ''; |
| 48 | +} |
| 49 | + |
| 50 | +export function deriveConfiguration(configuration: AmountServiceConfiguration) { |
| 51 | + const config: AmountServiceConfiguration = { |
| 52 | + showCurrencyCode: '', |
| 53 | + digitsInfo: '1.2-2', |
| 54 | + stripSign: false, |
| 55 | + }; |
| 56 | + |
| 57 | + return Object.assign({}, config, configuration); |
| 58 | +} |
| 59 | + |
| 60 | +export type ShowCurrencyCode = '' | 'alwaysShowCurrency' | 'showForeignCurrency'; |
| 61 | +export type CurrencyCodePosition = '' | 'prefix' | 'postfix'; |
| 62 | + |
| 63 | +export interface AmountServiceConfiguration { |
| 64 | + /** |
| 65 | + * - '' - don't output CurrencyCode |
| 66 | + * - 'alwaysShowCurrency' - always shows CurrencyCode, regardless of presentation currency |
| 67 | + * - 'showForeignCurrency' - only show CurrencyCode if it differs from the presentation currency |
| 68 | + */ |
| 69 | + showCurrencyCode: ShowCurrencyCode; |
| 70 | + /** |
| 71 | + * The position of the currency code in the formatted amount |
| 72 | + * - 'postfix' - output CurrencyCode after the formatted amount, eg. 1.234,56 EUR |
| 73 | + * - 'prefix' - output CurrencyCode before the formatted amount, eg. DKK 1.234,56 |
| 74 | + */ |
| 75 | + currencyCodePosition?: CurrencyCodePosition; |
| 76 | + /** |
| 77 | + * A string that represents the format of the number. Learn more about the format here: https://angular.io/api/common/DecimalPipe#parameters |
| 78 | + */ |
| 79 | + digitsInfo?: string; |
| 80 | + /** |
| 81 | + * Remove the minus sign from the formatted amount and trim any leading or trailing whitespace. |
| 82 | + */ |
| 83 | + stripSign?: boolean; |
| 84 | + /** |
| 85 | + * The string to return if the amount is empty |
| 86 | + */ |
| 87 | + returnValueOnEmptyAmount?: string; |
| 88 | +} |
0 commit comments