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/modules-modules / utils / formatting.ts
Size: Mime:
function processKeys(obj, keys) {
  if (!obj || !keys || keys.length == 0) return null
  if (keys.length == 1) return obj[keys[0]]

  const key = keys.shift()
  return processKeys(obj[key], keys)
}

// return a key given a key string aka. product.properties.children
export function getValueForKey(obj, keyString, defaultValue) {
  return processKeys(obj, keyString.split('.')) || defaultValue
}

// Format a price to standard fixed
export function formatPrice(price) {
  try {
    let newPrice = Number(price)
    return newPrice.toFixed(2)
  } catch (e) {
    return 'N/A'
  }
}

// Utility to updates state
export function updateState(component, newState) {
  component.setState(Object.assign({}, component.state, newState))
}

export const pricifyFilter = (str: string) => {
  // i.e 500 & Above -> $500 & Above
  // '$' + str.replace(/-|&/, ' - ')

  if (str.includes('&')) {
    return `$${str.split(' & ')[0]} & ${str.split(' & ')[1]}`
  }
  // i.e TOP -> Top
  if (!str.includes('-')) {
    return str
  }
  // i.e 0-50 -> $0 - $50
  return str
    .split('-')
    .map(v => `$${v}`)
    .join(' - ')
}