Ad
Closures
Basic Language Features
Fundamentals
ES2015
Babel
Functions
Control Flow
const unknown = 'Unknown error'
const catalog = {
  en: {
    ERROR_USER_NOT_FOUND: 'Error: User not found',
    ERROR_500: 'Internal Server Error',
  },
  ru: {
    ERROR_USER_NOT_FOUND: 'Ошибка: Пользователь не найден',
  }
}

const catalogProvider = locale => error => catalog[locale][error] || unknown
Closures
Basic Language Features
Fundamentals
Mathematics
Algorithms
Logic
Numbers
Data Types
  • This function takes colors as arguments in the only RGBA
  • @param {String} positiveColor
  • @param {String} negativeColor
  • @param {String} neutralColor
  • @return {Function} Which takes a percentage as an argument and returns the color depending on the percentage passed
const calculateColor = ({ positive: positiveColor, negative: negativeColor, neutral: neutralColor }) => {
  return percent => {

    const absoluteValueOfNumber = Math.sign(percent)
    const startPercent = 100
    const opacityNeutral = 0.5
    const opacityMinimal = 0.25
    const isOpacityMinimal = val => val <= opacityMinimal

    if (absoluteValueOfNumber === 1) {
      const opacity = percent / startPercent

      if (isOpacityMinimal(opacity)) {
        return assignOpacity(positiveColor, opacityMinimal)
      }

      return assignOpacity(positiveColor, opacity)
    }

    if (absoluteValueOfNumber === -1) {
      const opacity = Math.abs(percent) / startPercent

      if (isOpacityMinimal(opacity)) {
        return assignOpacity(negativeColor, opacityMinimal)
      }

      return assignOpacity(negativeColor, opacity)
    }

    if (absoluteValueOfNumber === 0) {
      return assignOpacity(neutralColor, opacityNeutral)
    }
  }
}

function assignOpacity (color, value) {
  const colorArr = color.split(',').reverse()
  const opacity = colorArr[0]
  const bracket = opacity[opacity.length - 1]
  colorArr[0] = value + bracket
  return colorArr.reverse().join()
}