From b60321383eedeb889e3ff65653e4b74481651202 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Sun, 5 Oct 2014 00:30:45 +0100 Subject: [PATCH] Speeds up HexNameShortener. * Moves regex creation out of loop. * Makes #shorten a simple function. --- lib/colors/hex-name-shortener.js | 25 +++++++++++-------------- lib/selectors/optimizers/simple.js | 5 ++++- test/colors/hex-name-shortener-test.js | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/colors/hex-name-shortener.js b/lib/colors/hex-name-shortener.js index 914b2356..ce4acc62 100644 --- a/lib/colors/hex-name-shortener.js +++ b/lib/colors/hex-name-shortener.js @@ -1,6 +1,4 @@ -function HexNameShortener(value) { - this.value = value; -} +var HexNameShortener = {}; var COLORS = { aliceblue: '#f0f8ff', @@ -164,18 +162,17 @@ for (var name in COLORS) { 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; diff --git a/lib/selectors/optimizers/simple.js b/lib/selectors/optimizers/simple.js index 6b7e59f0..2ce82112 100644 --- a/lib/selectors/optimizers/simple.js +++ b/lib/selectors/optimizers/simple.js @@ -120,6 +120,9 @@ function multipleZerosMinifier(property, value) { } 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(); @@ -155,7 +158,7 @@ function colorMininifier(property, value, compatibility) { }); } - return new HexNameShortener(value).shorten(); + return HexNameShortener.shorten(value); } function reduce(body, options) { diff --git a/test/colors/hex-name-shortener-test.js b/test/colors/hex-name-shortener-test.js index 4060bd3d..e5cab0b1 100644 --- a/test/colors/hex-name-shortener-test.js +++ b/test/colors/hex-name-shortener-test.js @@ -158,7 +158,7 @@ function colorShorteningContext() { function shortened(target) { return function (source) { - assert.equal(new HexNameShortener(source).shorten(), target); + assert.equal(HexNameShortener.shorten(source), target); }; } -- 2.34.1