Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/utils / src / priceSplitter.ts
Size: Mime:
/**
 * @deprecated - use Money
 */
import { isObj, EMPTY_OBJ } from 'exotic'

interface Price {
  type?: 'Sale' | 'Reg'
  value?: any
  minReg?: string
  maxReg?: string
  reg?: string
  sale?: string
  ismax?: boolean | 'true' | 'false'
  ismin?: boolean | 'true' | 'false'
  minSale?: boolean | 'true' | 'false'
  maxSale?: boolean | 'true' | 'false'
}
interface Pricing {
  currencyCode?: string
  prices?: Price[]
}

/**
 * @version -1 named "initprice"
 */
function transformPrice(pricing: Pricing) {
  if (process.env.NODE_ENV === 'development') {
    if (!isObj(pricing)) {
      throw new Error('pricing given to transformPrice is not an object')
    }
  }
  if (!isObj(pricing)) {
    return EMPTY_OBJ
  }

  const salePrices = {}
  const regPrices = {}
  const currencyCode = pricing.currencyCode || '$'
  if (pricing.prices) {
    for (let index = 0; index < pricing.prices.length; index++) {
      const priceData = pricing.prices[index]
      if (priceData.type === 'Sale') {
        Object.assign(salePrices, salePrice(priceData))
      } else if (priceData.type === 'Reg') {
        Object.assign(regPrices, regPrice(priceData))
      }
    }
  }
  return setPriceData(salePrices, regPrices, currencyCode)
}

export interface FormattedRegPrice {
  minReg?: string
  maxReg?: string
  reg: string
}

/**
 * @todo @@perf @@dupe
 */
const toFloat = (x: any) => parseFloat(x.value).toFixed(2)

/**
 *
 * @param priceData
 */
// @see catalog/product/index get regPrice()
function regPrice(priceData: Price): FormattedRegPrice {
  if (process.env.NODE_ENV === 'development') {
    if (!priceData) {
      throw new Error('MISSING PRICE DATA IN FORMAT REGPRICE')
    }
  }

  priceData.value = toFloat(priceData.value)
  const price: FormattedRegPrice = {
    reg: priceData.value,
  }
  if (priceData.ismin === 'true') {
    price.minReg = priceData.value
  } else if (priceData.ismax === 'true') {
    price.maxReg = priceData.value
  }

  return price
}

export interface FormattedSalePrice {
  minSale?: string
  maxSale?: string
  sale: string
}

/**
 * @see catalog/product/index get salePrice()
 */
function salePrice(priceData: Price) {
  if (process.env.NODE_ENV === 'development') {
    if (!priceData) {
      throw new Error('MISSING PRICE DATA IN FORMAT REGPRICE')
    }
  }

  priceData.value = toFloat(priceData.value)
  const price: FormattedSalePrice = {
    sale: priceData.value,
  }

  if (priceData.ismin === 'true') {
    price.minSale = priceData.value
  } else if (priceData.ismax === 'true') {
    price.maxSale = priceData.value
  }
  price.sale = priceData.value

  return price
}

export interface SetPrice {
  sale?: string
  reg?: string
}

function setPriceData(
  salePrices: Price,
  regPrices: Price,
  currencyCode: string
): SetPrice {
  const price: SetPrice = {}

  if (salePrices) {
    if (salePrices.minSale && salePrices.maxSale) {
      price.sale =
        currencyCode +
        salePrices.maxSale +
        ' - ' +
        currencyCode +
        salePrices.minSale
    } else if (salePrices.sale) {
      price.sale = currencyCode + salePrices.sale
    }
  }

  if (regPrices) {
    if (regPrices.minReg && regPrices.maxReg) {
      price.reg =
        currencyCode +
        regPrices.maxReg +
        ' - ' +
        currencyCode +
        regPrices.minReg
    } else if (regPrices.reg) {
      price.reg = currencyCode + regPrices.reg
    }
  }

  /**
   * @todo @@perf optimizie
   */
  price.reg = price.sale === price.reg ? undefined : price.reg
  price.sale = price.sale ? price.sale : price.reg
  price.reg = price.sale ? price.reg : undefined

  return price
}

export { transformPrice, regPrice, salePrice, setPriceData }
export default transformPrice