@atlaskit/atlassian-navigation
Version:
A horizontal navigation component for Atlassian apps.
47 lines (45 loc) • 1.62 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.convertHexShorthand = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var hex = '[a-z0-9]';
var shortHandHexColorPattern = new RegExp("#(".concat(hex, ")(").concat(hex, ")(").concat(hex, ")"), 'i');
var repeat = function repeat(str, times) {
return (0, _toConsumableArray2.default)(Array(times)).map(function () {
return str;
}).join('');
};
var isShortHexColor = function isShortHexColor(color) {
return color && color.length === 4;
};
var completeHexColor = function completeHexColor(colors) {
var hex = colors.map(function (color) {
return repeat(color, 2);
}).join('');
return "#".concat(hex);
};
var completeTriplet = function completeTriplet(colors) {
return "#".concat(repeat(colors[1], 6));
};
var isTriplet = function isTriplet(colors) {
return colors[0] === colors[1] && colors[1] === colors[2];
};
var convertHexShorthand = exports.convertHexShorthand = function convertHexShorthand(color) {
if (isShortHexColor(color)) {
// when color = '#ccc', matches is structured as ['#ccc', 'c', 'c', 'c', ...]
var matches = color.match(shortHandHexColorPattern);
if (matches) {
var 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;
};