See #850 - cleans up optimization level code.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 6 Jan 2017 11:18:47 +0000 (12:18 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 6 Jan 2017 14:02:11 +0000 (15:02 +0100)
Why:

* For better readability.

lib/clean.js
lib/options/optimization-level.js
test/options/optimization-level-test.js

index fbffdad..68b99e8 100644 (file)
@@ -9,7 +9,7 @@ var path = require('path');
 var url = require('url');
 
 var OptimizationLevel = require('./options/optimization-level').OptimizationLevel;
-var optimizationLevelOptionsFrom = require('./options/optimization-level').optimizationLevelOptionsFrom;
+var optimizationLevelFrom = require('./options/optimization-level').optimizationLevelFrom;
 
 var advancedOptimize = require('./optimizer/advanced');
 var basicOptimize = require('./optimizer/basic');
@@ -36,7 +36,7 @@ var CleanCSS = module.exports = function CleanCSS(options) {
     inlineRequest: options.inlineRequest || {},
     inlineTimeout: options.inlineTimeout || DEFAULT_TIMEOUT,
     keepBreaks: options.keepBreaks || false,
-    level: optimizationLevelOptionsFrom(options.level),
+    level: optimizationLevelFrom(options.level),
     rebase: undefined === options.rebase ? true : !!options.rebase,
     rebaseTo: ('rebaseTo' in options) ? path.resolve(options.rebaseTo) : process.cwd(),
     sourceMap: options.sourceMap,
index d8cbfbb..0cfc5e0 100644 (file)
@@ -26,67 +26,71 @@ var ALL_KEYWORD_2 = 'all';
 var OPTION_SEPARATOR = ';';
 var OPTION_VALUE_SEPARATOR = ':';
 
-function optimizationLevelOptionsFrom(source) {
-  var options = override(DEFAULTS, {});
+function optimizationLevelFrom(source) {
+  var level = override(DEFAULTS, {});
+  var Zero = OptimizationLevel.Zero;
+  var One = OptimizationLevel.One;
+  var Two = OptimizationLevel.Two;
+
 
   if (undefined === source) {
-    return options;
+    return level;
   }
 
   if (typeof source == 'string') {
     source = parseInt(source);
   }
 
-  if (typeof source == 'number' && source === 2) {
-    return options;
+  if (typeof source == 'number' && source === parseInt(Two)) {
+    return level;
   }
 
-  if (typeof source == 'number' && source === 1) {
-    delete options[OptimizationLevel.Two];
-    return options;
+  if (typeof source == 'number' && source === parseInt(One)) {
+    delete level[Two];
+    return level;
   }
 
-  if (typeof source == 'number' && source === 0) {
-    delete options[OptimizationLevel.Two];
-    delete options[OptimizationLevel.One];
-    return options;
+  if (typeof source == 'number' && source === parseInt(Zero)) {
+    delete level[Two];
+    delete level[One];
+    return level;
   }
 
   if (typeof source == 'object') {
     source = covertValuesToHashes(source);
   }
 
-  if (OptimizationLevel.One in source && 'roundingPrecision' in source[OptimizationLevel.One]) {
-    source[OptimizationLevel.One].roundingPrecision = roundingPrecisionFrom(source[OptimizationLevel.One].roundingPrecision);
+  if (One in source && 'roundingPrecision' in source[One]) {
+    source[One].roundingPrecision = roundingPrecisionFrom(source[One].roundingPrecision);
   }
 
-  if (OptimizationLevel.Zero in source || OptimizationLevel.One in source || OptimizationLevel.Two in source) {
-    options[0] = override(options[0], source[0]);
+  if (Zero in source || One in source || Two in source) {
+    level[Zero] = override(level[Zero], source[Zero]);
   }
 
-  if (OptimizationLevel.One in source || OptimizationLevel.Two in source) {
-    options[1] = override(options[1], source[1]);
+  if (One in source || Two in source) {
+    level[One] = override(level[One], source[One]);
   } else {
-    delete options[OptimizationLevel.One];
+    delete level[One];
   }
 
-  if (OptimizationLevel.Two in source && ALL_KEYWORD_1 in source[OptimizationLevel.Two]) {
-    options[2] = override(options[2], defaults(OptimizationLevel.Two, normalizeValue(source[OptimizationLevel.Two][ALL_KEYWORD_1])));
-    delete source[2][ALL_KEYWORD_1];
+  if (Two in source && ALL_KEYWORD_1 in source[Two]) {
+    level[Two] = override(level[Two], defaults(Two, normalizeValue(source[Two][ALL_KEYWORD_1])));
+    delete source[Two][ALL_KEYWORD_1];
   }
 
-  if (OptimizationLevel.Two in source && ALL_KEYWORD_2 in source[OptimizationLevel.Two]) {
-    options[2] = override(options[2], defaults(OptimizationLevel.Two, normalizeValue(source[OptimizationLevel.Two][ALL_KEYWORD_2])));
-    delete source[2][ALL_KEYWORD_2];
+  if (Two in source && ALL_KEYWORD_2 in source[Two]) {
+    level[Two] = override(level[Two], defaults(Two, normalizeValue(source[Two][ALL_KEYWORD_2])));
+    delete source[Two][ALL_KEYWORD_2];
   }
 
-  if (OptimizationLevel.Two in source) {
-    options[2] = override(options[2], source[2]);
+  if (Two in source) {
+    level[Two] = override(level[Two], source[Two]);
   } else {
-    delete options[OptimizationLevel.Two];
+    delete level[Two];
   }
 
-  return options;
+  return level;
 }
 
 function defaults(level, value) {
@@ -113,22 +117,22 @@ function normalizeValue(value) {
 
 function covertValuesToHashes(source) {
   var clonedSource = override(source, {});
-  var key;
+  var level;
   var i;
 
   for (i = 0; i <= 2; i++) {
-    key = '' + i;
+    level = '' + i;
 
-    if (key in clonedSource && (clonedSource[key] === undefined || clonedSource[key] === false)) {
-      delete clonedSource[key];
+    if (level in clonedSource && (clonedSource[level] === undefined || clonedSource[level] === false)) {
+      delete clonedSource[level];
     }
 
-    if (key in clonedSource && clonedSource[key] === true) {
-      clonedSource[key] = {};
+    if (level in clonedSource && clonedSource[level] === true) {
+      clonedSource[level] = {};
     }
 
-    if (key in clonedSource && typeof clonedSource[key] == 'string') {
-      clonedSource[key] = covertToHash(clonedSource[key], key);
+    if (level in clonedSource && typeof clonedSource[level] == 'string') {
+      clonedSource[level] = covertToHash(clonedSource[level], level);
     }
   }
 
@@ -156,5 +160,5 @@ function covertToHash(asString, level) {
 
 module.exports = {
   OptimizationLevel: OptimizationLevel,
-  optimizationLevelOptionsFrom: optimizationLevelOptionsFrom,
+  optimizationLevelFrom: optimizationLevelFrom,
 };
index fbf1adf..b4aba82 100644 (file)
@@ -3,13 +3,13 @@ var assert = require('assert');
 var vows = require('vows');
 
 var roundingPrecisionFrom = require('../../lib/utils/rounding-precision').roundingPrecisionFrom;
-var optimizationLevelOptionsFrom = require('../../lib/options/optimization-level').optimizationLevelOptionsFrom;
+var optimizationLevelFrom = require('../../lib/options/optimization-level').optimizationLevelFrom;
 
-vows.describe(optimizationLevelOptionsFrom)
+vows.describe(optimizationLevelFrom)
   .addBatch({
     'undefined': {
       'topic': function () {
-        return optimizationLevelOptionsFrom(undefined);
+        return optimizationLevelFrom(undefined);
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -34,7 +34,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'number - level 0': {
       'topic': function () {
-        return optimizationLevelOptionsFrom(0);
+        return optimizationLevelFrom(0);
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0']);
@@ -45,7 +45,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'number - level 1': {
       'topic': function () {
-        return optimizationLevelOptionsFrom(1);
+        return optimizationLevelFrom(1);
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1']);
@@ -62,7 +62,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'number - level 2': {
       'topic': function () {
-        return optimizationLevelOptionsFrom(2);
+        return optimizationLevelFrom(2);
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -87,7 +87,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'string with value': {
       'topic': function () {
-        return optimizationLevelOptionsFrom('0');
+        return optimizationLevelFrom('0');
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0']);
@@ -98,7 +98,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 1: { specialComments: 0 }, 2: true });
+        return optimizationLevelFrom({ 1: { specialComments: 0 }, 2: true });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -123,7 +123,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with all keyword': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 1: { specialComments: 0 }, 2: { all: false, mediaMerging: true } });
+        return optimizationLevelFrom({ 1: { specialComments: 0 }, 2: { all: false, mediaMerging: true } });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -148,7 +148,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with * keyword': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 1: { specialComments: 0 }, 2: { '*': false, mediaMerging: true } });
+        return optimizationLevelFrom({ 1: { specialComments: 0 }, 2: { '*': false, mediaMerging: true } });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -173,7 +173,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with options as strings': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 1: 'roundingPrecision:3;specialComments:0' });
+        return optimizationLevelFrom({ 1: 'roundingPrecision:3;specialComments:0' });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1']);
@@ -190,7 +190,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with options as strings with boolean values': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 2: 'mediaMerging:false;semanticMerging:true' });
+        return optimizationLevelFrom({ 2: 'mediaMerging:false;semanticMerging:true' });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -215,7 +215,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with options as strings with all keyword': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 2: 'all:false;mediaMerging:true;semanticMerging:true' });
+        return optimizationLevelFrom({ 2: 'all:false;mediaMerging:true;semanticMerging:true' });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -240,7 +240,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with options as strings with * keyword': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 2: '*:false;mediaMerging:true;semanticMerging:true' });
+        return optimizationLevelFrom({ 2: '*:false;mediaMerging:true;semanticMerging:true' });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
@@ -265,7 +265,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with options as undefined/boolean': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 0: undefined, 1: true, 2: undefined });
+        return optimizationLevelFrom({ 0: undefined, 1: true, 2: undefined });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1']);
@@ -282,7 +282,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with roundingPrecision as number': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 1: { roundingPrecision: 4 } });
+        return optimizationLevelFrom({ 1: { roundingPrecision: 4 } });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1']);
@@ -316,7 +316,7 @@ vows.describe(optimizationLevelOptionsFrom)
     },
     'a hash with complex roundingPrecision': {
       'topic': function () {
-        return optimizationLevelOptionsFrom({ 1: 'roundingPrecision:all=5,rem=off,%=1' });
+        return optimizationLevelFrom({ 1: 'roundingPrecision:all=5,rem=off,%=1' });
       },
       'has all options': function (levelOptions) {
         assert.deepEqual(Object.keys(levelOptions), ['0', '1']);