mirror of
https://github.com/josdejong/mathjs.git
synced 2026-02-01 16:07:46 +00:00
Merge pull request #752 from josdejong/foreach-map
OperatorNode: Use map instead of foreach in many places
This commit is contained in:
commit
aa29d8f92f
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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')) {
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user