Speeds up hex name shortener even futher.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 5 Oct 2014 14:02:43 +0000 (15:02 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 10 Oct 2014 20:22:45 +0000 (21:22 +0100)
* Skips running hex -> name shortener if there are no hex colors.
* Simplifies toName regexp.

lib/colors/hex-name-shortener.js

index ce4acc6..fe88fd2 100644 (file)
@@ -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;