Skip to content

Commit

Permalink
Merge pull request #752 from josdejong/foreach-map
Browse files Browse the repository at this point in the history
OperatorNode: Use map instead of foreach in many places
  • Loading branch information
FSMaxB committed Nov 14, 2016
2 parents abd383f + ab6d926 commit aa29d8f
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions lib/expression/node/OperatorNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,16 @@ function factory (type, config, load, typed, math) {
var associativity = operators.getAssociativity(root, parenthesis);

if ((parenthesis === 'all') || ((args.length > 2) && (root.getIdentifier() !== 'OperatorNode:add') && (root.getIdentifier() !== 'OperatorNode:multiply'))) {
var parens = [];
args.forEach(function (arg) {
var parens = args.map(function (arg) {
switch (arg.getContent().type) { //Nodes that don't need extra parentheses
case 'ArrayNode':
case 'ConstantNode':
case 'SymbolNode':
case 'ParenthesisNode':
parens.push(false);
return false;
break;
default:
parens.push(true);
return true;
}
});
return parens;
Expand Down Expand Up @@ -270,27 +269,21 @@ function factory (type, config, load, typed, math) {

return [lhsParens, rhsParens];
} else if ((args.length > 2) && ((root.getIdentifier() === 'OperatorNode:add') || (root.getIdentifier() === 'OperatorNode:multiply'))) {
var parensArray = [];

args.forEach(function (arg) {
var parens;
var parensArray = args.map(function (arg) {
var argPrecedence = operators.getPrecedence(arg, parenthesis);
var assocWithArg = operators.isAssociativeWith(root, arg, parenthesis);
var argAssociativity = operators.getAssociativity(arg, parenthesis);
if (argPrecedence === null) {
//if the argument has no defined precedence, no parens are needed
parens = false;
return false;
} else if ((precedence === argPrecedence) && (associativity === argAssociativity) && !assocWithArg) {
parens = true;
return true;
} else if (argPrecedence < precedence) {
parens = true;
} else {
parens = false;
return true;
}

parensArray.push(parens);
return false;
});

return parensArray;
}
}
Expand Down Expand Up @@ -339,14 +332,13 @@ function factory (type, config, load, typed, math) {

return lhs + ' ' + this.op + ' ' + rhs;
} else if ((args.length > 2) && ((this.getIdentifier() === 'OperatorNode:add') || (this.getIdentifier() === 'OperatorNode:multiply'))) {
var stringifiedArgs = [];
args.forEach(function (arg, index) {
var stringifiedArgs = args.map(function (arg, index) {
arg = arg.toString(options);
if (parens[index]) { //put in parenthesis?
arg = '(' + arg + ')';
}

stringifiedArgs.push(arg);
return arg;
});

if (this.implicit && (this.getIdentifier() === 'OperatorNode:multiply') && (implicit === 'hide')) {
Expand Down Expand Up @@ -431,13 +423,12 @@ function factory (type, config, load, typed, math) {
}
return lhsTex + op + rhsTex;
} else if ((args.length > 2) && ((this.getIdentifier() === 'OperatorNode:add') || (this.getIdentifier() === 'OperatorNode:multiply'))) {
var texifiedArgs = [];
args.forEach(function (arg, index) {
var texifiedArgs = args.map(function (arg, index) {
arg = arg.toTex(options);
if (parens[index]) {
arg = '\\left(' + arg + '\\right)';
}
texifiedArgs.push(arg);
return arg;
});

if ((this.getIdentifier() === 'OperatorNode:multiply') && this.implicit) {
Expand Down

0 comments on commit aa29d8f

Please sign in to comment.