}
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;