Skip to content

Commit

Permalink
Fix #113 (handle exponents on coefficients of polynomial terms) (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
shirleymiao authored and evykassirer committed Feb 10, 2017
1 parent b029a41 commit b222cbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/util/removeUnnecessaryParens.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ function removeUnnecessaryParensSearch(node) {
// unncessary parens around operators that can't be simplified further.
// Returns a node.
function removeUnnecessaryParensInOperatorNode(node) {
// Special case: if the node is an exponent node and the base
// is an operator, we should keep the parentheses for the base.
// e.g. (2x)^2 -> (2x)^2 instead of 2x^2
if (node.op === '^' && Node.Type.isParenthesis(node.args[0])) {
const base = node.args[0];
if (Node.Type.isOperator(base.content)) {
base.content = removeUnnecessaryParensSearch(base.content);
node.args[1] = removeUnnecessaryParensSearch(node.args[1]);

return node;
}
}

node.args.forEach((child, i) => {
node.args[i] = removeUnnecessaryParensSearch(child);
});
Expand Down
2 changes: 2 additions & 0 deletions test/util/removeUnnecessaryParens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('removeUnnecessaryParens', function () {
['((4+5)) + ((2^3))', '(4 + 5) + 2^3'],
['(2x^6 + -50 x^2) - (x^4)', '2x^6 - 50x^2 - x^4'],
['(x+4) - (12 + x)', 'x + 4 - (12 + x)'],
['(2x)^2', '(2x)^2'],
['((4+x)-5)^(2)', '(4 + x - 5)^2'],
];
tests.forEach(t => testRemoveUnnecessaryParens(t[0], t[1]));
});

0 comments on commit b222cbf

Please sign in to comment.