|
634 | 634 | this.addProperty("A", 1); |
635 | 635 | this.addProperty("B", 1); |
636 | 636 | this.addProperty("OP", "+", "enum", { values: MathOperation.values }); |
637 | | - this._func = function(A,B) { return A + B; }; |
| 637 | + this._func = MathOperation.funcs[this.properties.OP]; |
638 | 638 | this._result = []; //only used for arrays |
639 | 639 | } |
640 | 640 |
|
641 | 641 | MathOperation.values = ["+", "-", "*", "/", "%", "^", "max", "min"]; |
| 642 | + MathOperation.funcs = { |
| 643 | + "+": function(A,B) { return A + B; }, |
| 644 | + "-": function(A,B) { return A - B; }, |
| 645 | + "x": function(A,B) { return A * B; }, |
| 646 | + "X": function(A,B) { return A * B; }, |
| 647 | + "*": function(A,B) { return A * B; }, |
| 648 | + "/": function(A,B) { return A / B; }, |
| 649 | + "%": function(A,B) { return A % B; }, |
| 650 | + "^": function(A,B) { return Math.pow(A, B); }, |
| 651 | + "max": function(A,B) { return Math.max(A, B); }, |
| 652 | + "min": function(A,B) { return Math.min(A, B); } |
| 653 | + }; |
642 | 654 |
|
643 | 655 | MathOperation.title = "Operation"; |
644 | 656 | MathOperation.desc = "Easy math operators"; |
|
666 | 678 | { |
667 | 679 | if (name != "OP") |
668 | 680 | return; |
669 | | - switch (this.properties.OP) { |
670 | | - case "+": this._func = function(A,B) { return A + B; }; break; |
671 | | - case "-": this._func = function(A,B) { return A - B; }; break; |
672 | | - case "x": |
673 | | - case "X": |
674 | | - case "*": this._func = function(A,B) { return A * B; }; break; |
675 | | - case "/": this._func = function(A,B) { return A / B; }; break; |
676 | | - case "%": this._func = function(A,B) { return A % B; }; break; |
677 | | - case "^": this._func = function(A,B) { return Math.pow(A, B); }; break; |
678 | | - case "max": this._func = function(A,B) { return Math.max(A, B); }; break; |
679 | | - case "min": this._func = function(A,B) { return Math.min(A, B); }; break; |
680 | | - default: |
681 | | - console.warn("Unknown operation: " + this.properties.OP); |
682 | | - this._func = function(A) { return A; }; |
683 | | - break; |
| 681 | + this._func = MathOperation.funcs[this.properties.OP]; |
| 682 | + if(!this._func) |
| 683 | + { |
| 684 | + console.warn("Unknown operation: " + this.properties.OP); |
| 685 | + this._func = function(A) { return A; }; |
684 | 686 | } |
685 | 687 | } |
686 | 688 |
|
|
700 | 702 | B = this.properties["B"]; |
701 | 703 | } |
702 | 704 |
|
| 705 | + var func = MathOperation.funcs[this.properties.OP]; |
| 706 | + |
703 | 707 | var result; |
704 | 708 | if(A.constructor === Number) |
705 | 709 | { |
706 | 710 | result = 0; |
707 | | - result = this._func(A,B); |
| 711 | + result = func(A,B); |
708 | 712 | } |
709 | 713 | else if(A.constructor === Array) |
710 | 714 | { |
711 | 715 | result = this._result; |
712 | 716 | result.length = A.length; |
713 | 717 | for(var i = 0; i < A.length; ++i) |
714 | | - result[i] = this._func(A[i],B); |
| 718 | + result[i] = func(A[i],B); |
715 | 719 | } |
716 | 720 | else |
717 | 721 | { |
718 | 722 | result = {}; |
719 | 723 | for(var i in A) |
720 | | - result[i] = this._func(A[i],B); |
| 724 | + result[i] = func(A[i],B); |
721 | 725 | } |
722 | 726 | this.setOutputData(0, result); |
723 | 727 | }; |
|
0 commit comments