Files
30-seconds-of-code/snippets/extendHex.md
lvzhenbang 0afd7a8777 [bug] change Array.slice() for String.slice()
useage:  Array.slice() should be changed to String.slice()
2017-12-24 17:50:24 +08:00

501 B

extendHex

Extends a 3-digit color code to a 6-digit color code.

Use Array.map(), split() and Array.join() to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form. String.slice() is used to remove # from string start since it's added once.

const extendHex = shortHex =>
  '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('')
// extendHex('#03f') -> '#0033ff'
// extendHex('05a') -> '#0055aa'