All files / src/app/core/models/price price.helper.ts

97.05% Statements 33/34
88.46% Branches 23/26
100% Functions 9/9
96.96% Lines 32/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109        257x   18x 13x                           7x 7x 1x   6x           15x 10x               29x 24x               15x               3x 1x   2x 1x   1x                         95x 95x   95x 95x         95x 115x 95x             62x 6x   56x 3x   53x 3x   50x 3x        
import { PriceItem } from 'ish-core/models/price-item/price-item.model';
 
import { Price } from './price.model';
 
export class PriceHelper {
  static diff(p1: Price, p2: Price): Price {
    PriceHelper.sanityChecks(p1, p2);
    return {
      type: p1.type,
      currency: p1.currency,
      value: Math.round((p1.value - p2.value) * 100) / 100,
    };
  }
 
  /**
   * Inverts the value of a price
   *
   * @param price The price
   * @returns inverted price
   */
  static invert<T extends Price | PriceItem>(price: T): T {
    Eif (price) {
      if (price.type === 'Money') {
        return { ...price, value: (price as Price).value * -1 };
      }
      return { ...price, gross: (price as PriceItem).gross * -1, net: (price as PriceItem).net * -1 };
    }
  }
 
  // visible-for-testing
  static min(p1: Price, p2: Price): Price {
    PriceHelper.sanityChecks(p1, p2);
    return {
      type: p1.type,
      currency: p1.currency,
      value: Math.round(Math.min(p1.value, p2.value) * 100) / 100,
    };
  }
 
  static sum(p1: Price, p2: Price): Price {
    PriceHelper.sanityChecks(p1, p2);
    return {
      type: p1.type,
      currency: p1.currency,
      value: Math.round((p1.value + p2.value) * 100) / 100,
    };
  }
 
  static empty(currency?: string): Price {
    return {
      type: 'Money',
      value: 0,
      currency,
    };
  }
 
  static getPrice(currency: string, value: number): Price {
    if (!currency) {
      throw new Error('getPrice cannot handle undefined currency');
    }
    if (value === undefined) {
      throw new Error('getPrice cannot handle undefined value');
    }
    return { currency, value, type: 'Money' };
  }
 
  /**
   * Returns the currency symbol for the given currency code in the specified format and locale.
   * This method replaces the deprecated Angular getCurrencySymbol function.
   *
   * @param code The currency code (e.g., 'USD', 'EUR')
   * @param format The format: 'wide' for full name, 'narrow' for narrow symbol, 'symbol' for standard symbol
   * @param locale The locale string (optional, defaults to undefined)
   * @returns The currency symbol or code if formatting fails
   */
  static getCurrencySymbol(code: string, format: 'wide' | 'narrow' | 'symbol', locale?: string): string {
    try {
      const currencyDisplay = format === 'narrow' ? 'narrowSymbol' : 'symbol';
      // Convert underscore locale format (en_US) to hyphen format (en-US) for Intl API
      const normalizedLocale = locale?.replace(/_/g, '-');
      const formatter = new Intl.NumberFormat(normalizedLocale, {
        style: 'currency',
        currency: code,
        currencyDisplay,
      });
      const parts = formatter.formatToParts(0);
      const symbolPart = parts.find(part => part.type === 'currency');
      return symbolPart ? symbolPart.value : code;
    } catch {
      return code;
    }
  }
 
  private static sanityChecks(p1: Price, p2: Price) {
    if (!p1 || !p2) {
      throw new Error('cannot handle undefined inputs');
    }
    if (!Number.isFinite(p1.value) || !Number.isFinite(p2.value)) {
      throw new Error('cannot handle undefined values');
    }
    if (!p1.currency || !p2.currency) {
      throw new Error('cannot handle undefined currency');
    }
    if (p1.currency !== p2.currency) {
      throw new Error('currency mismatch');
    }
  }
}