All files / src/search-js decorator.ts

100% Statements 18/18
90% Branches 9/10
100% Functions 5/5
100% Lines 18/18

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  7x                         7x 3x     7x 7x 7x   7x 7x   7x       10x               3x 7x 2x   5x 5x 1x 2x     5x    
import { SearchProduct, SearchProductSku } from "@nosto/nosto-js/client"
import { CurrencyConfig, getCurrencyFormatting } from "./currencies"
 
type Prices = Pick<SearchProduct, "price" | "listPrice">
 
type FormattedPrices = {
  priceText?: string
  listPriceText?: string
}
 
/**
 * Exposes currency formatting logic as a SearchProduct decorator
 * Sets priceText and listPriceText fields on product and SKU level
 */
export function priceDecorator(config?: Partial<CurrencyConfig>) {
  const { formatCurrency } = getCurrencyFormatting(config)
 
  function formatPrices<T extends Prices>(obj: T, currency?: string) {
    const formatted: FormattedPrices = {}
    if (obj.price !== undefined) {
      formatted.priceText = formatCurrency(obj.price, currency)
    }
    if (obj.listPrice !== undefined) {
      formatted.listPriceText = formatCurrency(obj.listPrice, currency)
    }
    return Object.assign({}, obj, formatted)
  }
 
  function hasPrices(obj: Prices) {
    return obj.price !== undefined || obj.listPrice !== undefined
  }
 
  type Result = SearchProduct &
    FormattedPrices & {
      skus?: (SearchProductSku & FormattedPrices)[]
    }
 
  return function decorator(hit: SearchProduct): Result {
    if (!hasPrices(hit)) {
      return hit
    }
    const copy = formatPrices(hit, hit.priceCurrencyCode)
    if (copy.skus && copy.skus.some(hasPrices)) {
      copy.skus = copy.skus.map(sku => {
        return !hasPrices(sku) ? sku : formatPrices(sku, hit.priceCurrencyCode)
      })
    }
    return copy
  }
}