a system for building functions in javascript.
//process linking system
//storing functions in js object for easy access
var math = {
"+":function(first, second) {
return first + second;
},
"-":function(first, second) {
return first - second;
},
"*":function(first, second) {
return first * second;
},
"/":function(first, second) {
return first / second;
},
"%":function(first, second) {
return first % second;
}
};
//single functional unit object
var mathprocess = function(operand, oper) {
this.operand = operand;
this.oper = oper;
mathprocess.prototype.apply = function(value) {
return math[this.oper](value, this.operand);
};
};
//parses func strings in format START ;+:3; ;*:7; ;-:4; END
// uses /;(.):(.);/.exec(";+:3;") like patterns
var MathFunc = function(string) {
var units = string.split(" ");
this.oplist = [];
for(var i=0;i<units.length;i++) {
if (/;([+-/*%]):([0-9]+);/.test(units[i])) {
var match = /;(.):(.);/.exec(units[i]);
var process = new mathprocess(parseInt(match[2]), match[1]);
this.oplist.push(process);
}
}
//aplies the list of processes onto the value.
MathFunc.prototype.call = function(value) {
if (typeof value !== 'number') return 0;
for(var i=0;i<this.oplist.length;i++) {
value = oplist[i].apply(value);
}
return value;
};
};