Rewrites optimizer's #rebuild as a simple loop.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 6 Oct 2014 09:35:48 +0000 (10:35 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 10 Oct 2014 20:22:45 +0000 (21:22 +0100)
lib/selectors/optimizer.js

index 7b13c8a..9c6e21f 100644 (file)
@@ -11,27 +11,30 @@ function SelectorsOptimizer(options, context) {
 
 function rebuild(tokens, keepBreaks, isFlatBlock) {
   var joinCharacter = isFlatBlock ? ';' : (keepBreaks ? lineBreak : '');
-
-  return tokens
-    .map(function (token) {
-      if (typeof token === 'string')
-        return token;
-      // TODO: broken due to joining/splitting
-      if (token.body.length === 0 || (token.body.length == 1 && token.body[0] === ''))
-        return '';
-
-      if (token.block) {
-        var body = rebuild(token.body, keepBreaks, token.isFlatBlock);
-        return body.length > 0 ?
-          token.block + '{' + body + '}' :
-          '';
-      } else {
-        return token.selector.join(',') + '{' + token.body.join(';') + '}';
-      }
-    })
-    .filter(function (value) { return value.length > 0; })
-    .join(joinCharacter)
-    .trim();
+  var parts = [];
+
+  for (var i = 0, l = tokens.length; i < l; i++) {
+    var token = tokens[i];
+
+    if (typeof token === 'string') {
+      parts.push(token);
+      continue;
+    }
+
+    // TODO: broken due to joining/splitting
+    if (token.body.length === 0 || (token.body.length == 1 && token.body[0] === ''))
+      continue;
+
+    if (token.block) {
+      var body = rebuild(token.body, keepBreaks, token.isFlatBlock);
+      if (body.length > 0)
+        parts.push(token.block + '{' + body + '}');
+    } else {
+      parts.push(token.selector.join(',') + '{' + token.body.join(';') + '}');
+    }
+  }
+
+  return parts.join(joinCharacter);
 }
 
 SelectorsOptimizer.prototype.process = function (data) {
@@ -41,7 +44,7 @@ SelectorsOptimizer.prototype.process = function (data) {
   if (!this.options.noAdvanced)
     new AdvancedOptimizer(this.options, this.context).optimize(tokens);
 
-  return rebuild(tokens, this.options.keepBreaks, false);
+  return rebuild(tokens, this.options.keepBreaks, false).trim();
 };
 
 module.exports = SelectorsOptimizer;