Skip to content

Commit

Permalink
feat: replace switch with resMap
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasIcarus authored and omichelsen committed Jul 30, 2019
1 parent 2149483 commit a46fa91
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@
'<='
];

var operatorResMap = {
'>': [1],
'>=': [0, 1],
'=': [0],
'<=': [-1, 0],
'<': [-1]
};

function validateOperator(op) {
if (typeof op !== 'string') {
throw new TypeError('Invalid operator type, expected string but got ' + typeof op);
Expand All @@ -93,20 +101,10 @@
// Validate operator
validateOperator(operator);

// TODO: there might be a better way instead of doing this
switch(operator) {
case '>':
return compareVersions(v1, v2) > 0;
case '>=':
return compareVersions(v1, v2) >= 0;
case '<':
return compareVersions(v1, v2) < 0;
case '<=':
return compareVersions(v1, v2) <= 0;
default:
// Since validateOperator already checks the operator, this case in the switch checks for the '=' operator
return compareVersions(v1, v2) === 0;
}
// since result of compareVersions can only be -1 or 0 or 1
// a simple map can be used to replace switch
var res = compareVersions(v1, v2);
return operatorResMap[operator].indexOf(res) > -1;
}

return compareVersions;
Expand Down

0 comments on commit a46fa91

Please sign in to comment.