Files
30-seconds-of-code/snippets/extendHex.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

27 lines
697 B
Markdown

---
title: extendHex
tags: string,intermediate
firstSeen: 2017-12-17T17:55:51+02:00
lastUpdated: 2020-09-15T16:28:04+03:00
---
Extends a 3-digit color code to a 6-digit color code.
- Use `Array.prototype.map()`, `String.prototype.split()` and `Array.prototype.join()` to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
- `Array.prototype.slice()` is used to remove `#` from string start since it's added once.
```js
const extendHex = shortHex =>
'#' +
shortHex
.slice(shortHex.startsWith('#') ? 1 : 0)
.split('')
.map(x => x + x)
.join('');
```
```js
extendHex('#03f'); // '#0033ff'
extendHex('05a'); // '#0055aa'
```