UNPKG

@atlaskit/atlassian-navigation

Version:

A horizontal navigation component for Atlassian apps.

27 lines (26 loc) 1.01 kB
const hex = '[a-z0-9]'; const shortHandHexColorPattern = new RegExp(`#(${hex})(${hex})(${hex})`, 'i'); const repeat = (str, times) => [...Array(times)].map(() => str).join(''); const isShortHexColor = color => color && color.length === 4; const completeHexColor = colors => { const hex = colors.map(color => repeat(color, 2)).join(''); return `#${hex}`; }; const completeTriplet = colors => `#${repeat(colors[1], 6)}`; const isTriplet = colors => colors[0] === colors[1] && colors[1] === colors[2]; export const convertHexShorthand = color => { if (isShortHexColor(color)) { // when color = '#ccc', matches is structured as ['#ccc', 'c', 'c', 'c', ...] const matches = color.match(shortHandHexColorPattern); if (matches) { const colors = matches.slice(1, 4); if (isTriplet(colors)) { return completeTriplet(colors); } else { return completeHexColor(colors); } } } // return the color as is when it's not hex color shorthand return color; };