From 67e9147da027a9dd02178fbe1a175fca22bae0b2 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 5 Oct 2014 15:02:43 +0100 Subject: [PATCH] Speeds up hex name shortener even futher. * Skips running hex -> name shortener if there are no hex colors. * Simplifies toName regexp. --- lib/colors/hex-name-shortener.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) 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; -- 2.34.1