Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Before switch
_.each(conditions.conditions, function (condition) {
field = this.lastRecord[condition.field];
var isVisible = false;
switch(condition.operator) {
case "+":
isVisible = (x == y);
break;
case "!=":
isVisisble = (x !== y);
break
// Lots more
// of this
// monkey
// business
}
}, this);
// After switch
_.each(conditions.conditions, function (condition) {
field = this.lastRecord[condition.field];
isVisible = operators[condition.operator](field, condition.value);
}, this);
// Operators.js
// A separate reusable file of operators that's nicer looking IMHO
define({
'=': function(x, y) {
return x == y;
},
'!=': function(x, y) {
return x !== y;
},
'>': function(x, y) {
return +x > +y;
},
'<': function(x, y) {
return +x < +y;
},
'defined': function(x) {
return x;
},
'undefined': function(x) {
return !x;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment