All files / molecule/avatar/src useBackgroundColor.js

89.47% Statements 17/19
50% Branches 3/6
100% Functions 3/3
93.75% Lines 15/16

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    1x 2x   2x       2x 16x 16x     2x   2x 6x 6x     2x     1x 2x 2x 2x          
import {useMemo} from 'react'
 
const convertStringToHex = str => {
  let hash = 0
 
  Iif (str.length === 0) {
    return
  }
 
  for (let i = 0; i < str.length; i++) {
    hash = str.charCodeAt(i) + ((hash << 5) - hash)
    hash = hash & hash
  }
 
  let color = '#'
 
  for (let j = 0; j < 3; j++) {
    const value = (hash >> (j * 8)) & 255
    color += ('00' + value.toString(16)).substr(-2)
  }
 
  return color
}
 
const useBackgroundColor = ({name, backgroundColor}) => {
  return useMemo(() => {
    Iif (backgroundColor) return backgroundColor
    return name ? convertStringToHex(name) : undefined
  }, [name, backgroundColor])
}
 
export default useBackgroundColor