Skip to content

Instantly share code, notes, and snippets.

@marcushat
Created March 5, 2016 04:12
Show Gist options
  • Save marcushat/51c5a7947fe696253fa2 to your computer and use it in GitHub Desktop.
Save marcushat/51c5a7947fe696253fa2 to your computer and use it in GitHub Desktop.
var ReasonSwitch = function(reasons) {
this.on = !!reasons.length;
this.reasons = reasons; //reasons
this.bias = 'off'; //implement later, are reasons to stay on or off
this.on = function(reason) {
var reasons = this.reasons;
var switched = false; //default
if(!reasons.length) {
this.on = false;
switched = true;
}
if(reasons.indexOf(reason) === false) {
reasons.push(reason);
this.reasons = reasons;
}
return switched;
};
this.off = function(reason) {
var reasons = this.reasons;
var switched = false; //default
if(reasons.indexOf(reason) !== false) {
var index = reasons.indexOf(reason);
reasons.splice(index, 1);
this.reasons = reasons;
if(!reasons.length) {
this.on = true;
switched = true;
}
}
return switched;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment