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) {
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);
}
}
module.exports = {
OptimizationLevel: OptimizationLevel,
- optimizationLevelOptionsFrom: optimizationLevelOptionsFrom,
+ optimizationLevelFrom: optimizationLevelFrom,
};
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']);
},
'number - level 0': {
'topic': function () {
- return optimizationLevelOptionsFrom(0);
+ return optimizationLevelFrom(0);
},
'has all options': function (levelOptions) {
assert.deepEqual(Object.keys(levelOptions), ['0']);
},
'number - level 1': {
'topic': function () {
- return optimizationLevelOptionsFrom(1);
+ return optimizationLevelFrom(1);
},
'has all options': function (levelOptions) {
assert.deepEqual(Object.keys(levelOptions), ['0', '1']);
},
'number - level 2': {
'topic': function () {
- return optimizationLevelOptionsFrom(2);
+ return optimizationLevelFrom(2);
},
'has all options': function (levelOptions) {
assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']);
},
'string with value': {
'topic': function () {
- return optimizationLevelOptionsFrom('0');
+ return optimizationLevelFrom('0');
},
'has all options': function (levelOptions) {
assert.deepEqual(Object.keys(levelOptions), ['0']);
},
'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']);
},
'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']);
},
'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']);
},
'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']);
},
'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']);
},
'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']);
},
'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']);
},
'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']);
},
'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']);
},
'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']);