-function HexNameShortener(value) {
- this.value = value;
-}
+var HexNameShortener = {};
var COLORS = {
aliceblue: '#f0f8ff',
toHex[name] = hex;
}
-HexNameShortener.prototype.shorten = function () {
- var data = this.value;
-
- [toHex, toName].forEach(function(conversion) {
- var pattern = '(' + Object.keys(conversion).join('|') + ')';
- var colorSwitcher = function(match, colorValue, suffix) {
- return conversion[colorValue.toLowerCase()] + suffix;
- };
- data = data.replace(new RegExp(pattern + '( |,|\\)|$)', 'ig'), colorSwitcher);
- });
+var toHexPattern = new RegExp('(' + Object.keys(toHex).join('|') + ')( |,|\\)|$)', 'ig');
+var toNamePattern = new RegExp('(' + Object.keys(toName).join('|') + ')( |,|\\)|$)', 'ig');
- return data;
+HexNameShortener.shorten = function (value) {
+ return value
+ .replace(toHexPattern, function(match, colorValue, suffix) {
+ return toHex[colorValue.toLowerCase()] + suffix;
+ })
+ .replace(toNamePattern, function(match, colorValue, suffix) {
+ return toName[colorValue.toLowerCase()] + suffix;
+ });
};
module.exports = HexNameShortener;
}
function colorMininifier(property, value, compatibility) {
+ if (value.indexOf('#') === -1 && value.indexOf('rgb') == -1 && value.indexOf('hsl') == -1)
+ return HexNameShortener.shorten(value);
+
value = value
.replace(/rgb\((\-?\d+),(\-?\d+),(\-?\d+)\)/g, function (match, red, green, blue) {
return new RGB(red, green, blue).toHex();
});
}
- return new HexNameShortener(value).shorten();
+ return HexNameShortener.shorten(value);
}
function reduce(body, options) {
function shortened(target) {
return function (source) {
- assert.equal(new HexNameShortener(source).shorten(), target);
+ assert.equal(HexNameShortener.shorten(source), target);
};
}