From: Jakub Pawlowicz Date: Sun, 5 Oct 2014 14:02:43 +0000 (+0100) Subject: Speeds up hex name shortener even futher. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=67e9147da027a9dd02178fbe1a175fca22bae0b2;p=clean-css.git Speeds up hex name shortener even futher. * Skips running hex -> name shortener if there are no hex colors. * Simplifies toName regexp. --- diff --git a/lib/colors/hex-name-shortener.js b/lib/colors/hex-name-shortener.js index ce4acc62..fe88fd22 100644 --- a/lib/colors/hex-name-shortener.js +++ b/lib/colors/hex-name-shortener.js @@ -163,16 +163,21 @@ for (var name in COLORS) { } var toHexPattern = new RegExp('(' + Object.keys(toHex).join('|') + ')( |,|\\)|$)', 'ig'); -var toNamePattern = new RegExp('(' + Object.keys(toName).join('|') + ')( |,|\\)|$)', 'ig'); +var toNamePattern = new RegExp('(' + Object.keys(toName).join('|') + ')', 'ig'); 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; + var hasHex = value.indexOf('#') > -1; + var shortened = value.replace(toHexPattern, function(match, colorValue, suffix) { + return toHex[colorValue.toLowerCase()] + suffix; + }); + + if (hasHex) { + shortened = shortened.replace(toNamePattern, function(match, colorValue) { + return toName[colorValue.toLowerCase()]; }); + } + + return shortened; }; module.exports = HexNameShortener;