( op: string, lhs: MathJsonExpression, rhs: MathJsonExpression )
| 413 | |
| 414 | /** |
| 415 | * Assuming that op is an associative operator, fold lhs or rhs |
| 416 | * if either are the same operator. |
| 417 | */ |
| 418 | export function foldAssociativeOperator( |
| 419 | op: string, |
| 420 | lhs: MathJsonExpression, |
| 421 | rhs: MathJsonExpression |
| 422 | ): MathJsonExpression { |
| 423 | const lhsName = operator(lhs); |
| 424 | const rhsName = operator(rhs); |
| 425 | |
| 426 | if (lhsName === op && rhsName === op) |
| 427 | return [op, ...operands(lhs), ...operands(rhs)]; |
| 428 | |
| 429 | if (lhsName === op) return [op, ...operands(lhs), rhs]; |
| 430 | if (rhsName === op) return [op, lhs, ...operands(rhs)]; |
| 431 | return [op, lhs, rhs]; |
| 432 | } |
no test coverage detected