diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 2361fb0..0ae77da 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -118,6 +118,13 @@ test( 'calc((100vw - 50em)/2)', ); +test( + 'should reduce additions and subtractions (5)', + testValue, + 'calc(10px - (100vw - 50em) / 2)', + 'calc(10px - (100vw - 50em)/2)', +); + test( 'should ignore value surrounding calc function (1)', testValue, @@ -360,6 +367,34 @@ test( 'calc(-1px + -1em)', ); +test( + 'should reduce substracted expression from zero (1)', + testValue, + 'calc( 0 - (100vw - 10px) / 2 )', + 'calc((-100vw - -10px)/2)', +); + +test( + 'should reduce substracted expression from zero (2)', + testValue, + 'calc( 0px - (100vw - 10px))', + 'calc(-100vw - -10px)', +); + +test( + 'should reduce substracted expression from zero (3)', + testValue, + 'calc( 0px - (100vw - 10px) * 2px )', + 'calc((-100vw - -10px)*2px)', +); + +test( + 'should reduce substracted expression from zero (4)', + testValue, + 'calc( 0px - (100vw + 10px))', + 'calc(-100vw + -10px)', +); + test( 'should reduce nested expression', testValue, diff --git a/src/lib/reducer.js b/src/lib/reducer.js index e29d036..dc50560 100644 --- a/src/lib/reducer.js +++ b/src/lib/reducer.js @@ -34,8 +34,12 @@ function flipValue(node) { if (isValueType(node.type)) { node.value = -node.value; } else if (node.type === 'MathExpression') { - node.left = flipValue(node.left); - node.right = flipValue(node.right); + if (node.operator === '*' || node.operator === '/') { + node.left = flipValue(node.left); + } else { + node.left = flipValue(node.left); + node.right = flipValue(node.right); + } } return node;